Source file src/cmd/go/internal/modload/load.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package modload
     6  
     7  // This file contains the module-mode package loader, as well as some accessory
     8  // functions pertaining to the package import graph.
     9  //
    10  // There are two exported entry points into package loading — LoadPackages and
    11  // ImportFromFiles — both implemented in terms of loadFromRoots, which itself
    12  // manipulates an instance of the loader struct.
    13  //
    14  // Although most of the loading state is maintained in the loader struct,
    15  // one key piece - the build list - is a global, so that it can be modified
    16  // separate from the loading operation, such as during "go get"
    17  // upgrades/downgrades or in "go mod" operations.
    18  // TODO(#40775): It might be nice to make the loader take and return
    19  // a buildList rather than hard-coding use of the global.
    20  //
    21  // Loading is an iterative process. On each iteration, we try to load the
    22  // requested packages and their transitive imports, then try to resolve modules
    23  // for any imported packages that are still missing.
    24  //
    25  // The first step of each iteration identifies a set of “root” packages.
    26  // Normally the root packages are exactly those matching the named pattern
    27  // arguments. However, for the "all" meta-pattern, the final set of packages is
    28  // computed from the package import graph, and therefore cannot be an initial
    29  // input to loading that graph. Instead, the root packages for the "all" pattern
    30  // are those contained in the main module, and allPatternIsRoot parameter to the
    31  // loader instructs it to dynamically expand those roots to the full "all"
    32  // pattern as loading progresses.
    33  //
    34  // The pkgInAll flag on each loadPkg instance tracks whether that
    35  // package is known to match the "all" meta-pattern.
    36  // A package matches the "all" pattern if:
    37  // 	- it is in the main module, or
    38  // 	- it is imported by any test in the main module, or
    39  // 	- it is imported by a tool of the main module, or
    40  // 	- it is imported by another package in "all", or
    41  // 	- the main module specifies a go version ≤ 1.15, and the package is imported
    42  // 	  by a *test of* another package in "all".
    43  //
    44  // When graph pruning is in effect, we want to spot-check the graph-pruning
    45  // invariants — which depend on which packages are known to be in "all" — even
    46  // when we are only loading individual packages, so we set the pkgInAll flag
    47  // regardless of the whether the "all" pattern is a root.
    48  // (This is necessary to maintain the “import invariant” described in
    49  // https://golang.org/design/36460-lazy-module-loading.)
    50  //
    51  // Because "go mod vendor" prunes out the tests of vendored packages, the
    52  // behavior of the "all" pattern with -mod=vendor in Go 1.11–1.15 is the same
    53  // as the "all" pattern (regardless of the -mod flag) in 1.16+.
    54  // The loader uses the GoVersion parameter to determine whether the "all"
    55  // pattern should close over tests (as in Go 1.11–1.15) or stop at only those
    56  // packages transitively imported by the packages and tests in the main module
    57  // ("all" in Go 1.16+ and "go mod vendor" in Go 1.11+).
    58  //
    59  // Note that it is possible for a loaded package NOT to be in "all" even when we
    60  // are loading the "all" pattern. For example, packages that are transitive
    61  // dependencies of other roots named on the command line must be loaded, but are
    62  // not in "all". (The mod_notall test illustrates this behavior.)
    63  // Similarly, if the LoadTests flag is set but the "all" pattern does not close
    64  // over test dependencies, then when we load the test of a package that is in
    65  // "all" but outside the main module, the dependencies of that test will not
    66  // necessarily themselves be in "all". (That configuration does not arise in Go
    67  // 1.11–1.15, but it will be possible in Go 1.16+.)
    68  //
    69  // Loading proceeds from the roots, using a parallel work-queue with a limit on
    70  // the amount of active work (to avoid saturating disks, CPU cores, and/or
    71  // network connections). Each package is added to the queue the first time it is
    72  // imported by another package. When we have finished identifying the imports of
    73  // a package, we add the test for that package if it is needed. A test may be
    74  // needed if:
    75  // 	- the package matches a root pattern and tests of the roots were requested, or
    76  // 	- the package is in the main module and the "all" pattern is requested
    77  // 	  (because the "all" pattern includes the dependencies of tests in the main
    78  // 	  module), or
    79  // 	- the package is in "all" and the definition of "all" we are using includes
    80  // 	  dependencies of tests (as is the case in Go ≤1.15).
    81  //
    82  // After all available packages have been loaded, we examine the results to
    83  // identify any requested or imported packages that are still missing, and if
    84  // so, which modules we could add to the module graph in order to make the
    85  // missing packages available. We add those to the module graph and iterate,
    86  // until either all packages resolve successfully or we cannot identify any
    87  // module that would resolve any remaining missing package.
    88  //
    89  // If the main module is “tidy” (that is, if "go mod tidy" is a no-op for it)
    90  // and all requested packages are in "all", then loading completes in a single
    91  // iteration.
    92  // TODO(bcmills): We should also be able to load in a single iteration if the
    93  // requested packages all come from modules that are themselves tidy, regardless
    94  // of whether those packages are in "all". Today, that requires two iterations
    95  // if those packages are not found in existing dependencies of the main module.
    96  
    97  import (
    98  	"context"
    99  	"errors"
   100  	"fmt"
   101  	"go/build"
   102  	"internal/diff"
   103  	"io/fs"
   104  	"maps"
   105  	"os"
   106  	pathpkg "path"
   107  	"path/filepath"
   108  	"runtime"
   109  	"slices"
   110  	"sort"
   111  	"strings"
   112  	"sync"
   113  	"sync/atomic"
   114  
   115  	"cmd/go/internal/base"
   116  	"cmd/go/internal/cfg"
   117  	"cmd/go/internal/fips140"
   118  	"cmd/go/internal/fsys"
   119  	"cmd/go/internal/gover"
   120  	"cmd/go/internal/imports"
   121  	"cmd/go/internal/modfetch"
   122  	"cmd/go/internal/modindex"
   123  	"cmd/go/internal/mvs"
   124  	"cmd/go/internal/search"
   125  	"cmd/go/internal/str"
   126  	"cmd/internal/par"
   127  
   128  	"golang.org/x/mod/module"
   129  )
   130  
   131  // PackageOpts control the behavior of the LoadPackages function.
   132  type PackageOpts struct {
   133  	// TidyGoVersion is the Go version to which the go.mod file should be updated
   134  	// after packages have been loaded.
   135  	//
   136  	// An empty TidyGoVersion means to use the Go version already specified in the
   137  	// main module's go.mod file, or the latest Go version if there is no main
   138  	// module.
   139  	TidyGoVersion string
   140  
   141  	// Tags are the build tags in effect (as interpreted by the
   142  	// cmd/go/internal/imports package).
   143  	// If nil, treated as equivalent to imports.Tags().
   144  	Tags map[string]bool
   145  
   146  	// Tidy, if true, requests that the build list and go.sum file be reduced to
   147  	// the minimal dependencies needed to reproducibly reload the requested
   148  	// packages.
   149  	Tidy bool
   150  
   151  	// TidyDiff, if true, causes tidy not to modify go.mod or go.sum but
   152  	// instead print the necessary changes as a unified diff. It exits
   153  	// with a non-zero code if the diff is not empty.
   154  	TidyDiff bool
   155  
   156  	// TidyCompatibleVersion is the oldest Go version that must be able to
   157  	// reproducibly reload the requested packages.
   158  	//
   159  	// If empty, the compatible version is the Go version immediately prior to the
   160  	// 'go' version listed in the go.mod file.
   161  	TidyCompatibleVersion string
   162  
   163  	// VendorModulesInGOROOTSrc indicates that if we are within a module in
   164  	// GOROOT/src, packages in the module's vendor directory should be resolved as
   165  	// actual module dependencies (instead of standard-library packages).
   166  	VendorModulesInGOROOTSrc bool
   167  
   168  	// ResolveMissingImports indicates that we should attempt to add module
   169  	// dependencies as needed to resolve imports of packages that are not found.
   170  	//
   171  	// For commands that support the -mod flag, resolving imports may still fail
   172  	// if the flag is set to "readonly" (the default) or "vendor".
   173  	ResolveMissingImports bool
   174  
   175  	// AssumeRootsImported indicates that the transitive dependencies of the root
   176  	// packages should be treated as if those roots will be imported by the main
   177  	// module.
   178  	AssumeRootsImported bool
   179  
   180  	// AllowPackage, if non-nil, is called after identifying the module providing
   181  	// each package. If AllowPackage returns a non-nil error, that error is set
   182  	// for the package, and the imports and test of that package will not be
   183  	// loaded.
   184  	//
   185  	// AllowPackage may be invoked concurrently by multiple goroutines,
   186  	// and may be invoked multiple times for a given package path.
   187  	AllowPackage func(ctx context.Context, path string, mod module.Version) error
   188  
   189  	// LoadTests loads the test dependencies of each package matching a requested
   190  	// pattern. If ResolveMissingImports is also true, test dependencies will be
   191  	// resolved if missing.
   192  	LoadTests bool
   193  
   194  	// UseVendorAll causes the "all" package pattern to be interpreted as if
   195  	// running "go mod vendor" (or building with "-mod=vendor").
   196  	//
   197  	// This is a no-op for modules that declare 'go 1.16' or higher, for which this
   198  	// is the default (and only) interpretation of the "all" pattern in module mode.
   199  	UseVendorAll bool
   200  
   201  	// AllowErrors indicates that LoadPackages should not terminate the process if
   202  	// an error occurs.
   203  	AllowErrors bool
   204  
   205  	// SilencePackageErrors indicates that LoadPackages should not print errors
   206  	// that occur while matching or loading packages, and should not terminate the
   207  	// process if such an error occurs.
   208  	//
   209  	// Errors encountered in the module graph will still be reported.
   210  	//
   211  	// The caller may retrieve the silenced package errors using the Lookup
   212  	// function, and matching errors are still populated in the Errs field of the
   213  	// associated search.Match.)
   214  	SilencePackageErrors bool
   215  
   216  	// SilenceMissingStdImports indicates that LoadPackages should not print
   217  	// errors or terminate the process if an imported package is missing, and the
   218  	// import path looks like it might be in the standard library (perhaps in a
   219  	// future version).
   220  	SilenceMissingStdImports bool
   221  
   222  	// SilenceNoGoErrors indicates that LoadPackages should not print
   223  	// imports.ErrNoGo errors.
   224  	// This allows the caller to invoke LoadPackages (and report other errors)
   225  	// without knowing whether the requested packages exist for the given tags.
   226  	//
   227  	// Note that if a requested package does not exist *at all*, it will fail
   228  	// during module resolution and the error will not be suppressed.
   229  	SilenceNoGoErrors bool
   230  
   231  	// SilenceUnmatchedWarnings suppresses the warnings normally emitted for
   232  	// patterns that did not match any packages.
   233  	SilenceUnmatchedWarnings bool
   234  
   235  	// Resolve the query against this module.
   236  	MainModule module.Version
   237  
   238  	// If Switcher is non-nil, then LoadPackages passes all encountered errors
   239  	// to Switcher.Error and tries Switcher.Switch before base.ExitIfErrors.
   240  	Switcher gover.Switcher
   241  }
   242  
   243  // LoadPackages identifies the set of packages matching the given patterns and
   244  // loads the packages in the import graph rooted at that set.
   245  func LoadPackages(ld *Loader, ctx context.Context, opts PackageOpts, patterns ...string) (matches []*search.Match, loadedPackages []string) {
   246  	if opts.Tags == nil {
   247  		opts.Tags = imports.Tags()
   248  	}
   249  
   250  	patterns = search.CleanPatterns(patterns)
   251  	matches = make([]*search.Match, 0, len(patterns))
   252  	allPatternIsRoot := false
   253  	for _, pattern := range patterns {
   254  		matches = append(matches, search.NewMatch(pattern))
   255  		if pattern == "all" {
   256  			allPatternIsRoot = true
   257  		}
   258  	}
   259  
   260  	updateMatches := func(rs *Requirements, pld *packageLoader) {
   261  		matchWork := par.NewQueue(runtime.GOMAXPROCS(0))
   262  		for _, m := range matches {
   263  			if m.IsLocal() && m.Dirs == nil {
   264  				// only scan the filesystem once
   265  				matchWork.Add(func() {
   266  					matchModRoots := ld.modRoots
   267  					if opts.MainModule != (module.Version{}) {
   268  						matchModRoots = []string{ld.MainModules.ModRoot(opts.MainModule)}
   269  					}
   270  					matchLocalDirs(ld, ctx, matchModRoots, m, rs)
   271  				})
   272  			}
   273  		}
   274  		<-matchWork.Idle()
   275  
   276  		for _, m := range matches {
   277  			switch {
   278  			case m.IsLocal():
   279  				// Make a copy of the directory list and translate to import paths.
   280  				// Note that whether a directory corresponds to an import path
   281  				// changes as the build list is updated, and a directory can change
   282  				// from not being in the build list to being in it and back as
   283  				// the exact version of a particular module increases during
   284  				// the loader iterations.
   285  				m.Pkgs = m.Pkgs[:0]
   286  				if len(m.Dirs) > 0 {
   287  					type result struct {
   288  						pkg string
   289  						err error
   290  					}
   291  					results := make([]result, len(m.Dirs))
   292  					work := par.NewQueue(runtime.GOMAXPROCS(0))
   293  					for i, dir := range m.Dirs {
   294  						work.Add(func() {
   295  							var (
   296  								pkg string
   297  								err error
   298  							)
   299  							absDir := mkAbs(base.Cwd(), dir)
   300  							if m.IsLiteral() {
   301  								pkg, err = resolveLocalPackage(ld, ctx, absDir, rs)
   302  							} else {
   303  								// Wildcard matches have already been filtered to directories
   304  								// that contain packages. Avoid re-reading package files on
   305  								// every loader iteration just to map directory to import path.
   306  								pkg, err = localPackagePath(ld, ctx, absDir, rs)
   307  							}
   308  							results[i] = result{pkg, err}
   309  						})
   310  					}
   311  					<-work.Idle()
   312  
   313  					for _, res := range results {
   314  						pkg, err := res.pkg, res.err
   315  						if err != nil {
   316  							if !m.IsLiteral() && (err == errPkgIsBuiltin || err == errPkgIsGorootSrc) {
   317  								continue // Don't include "builtin" or GOROOT/src in wildcard patterns.
   318  							}
   319  
   320  							// If we're outside of a module, ensure that the failure mode
   321  							// indicates that.
   322  							if !ld.HasModRoot() {
   323  								die(ld)
   324  							}
   325  
   326  							if pld != nil {
   327  								m.AddError(err)
   328  							}
   329  							continue
   330  						}
   331  						m.Pkgs = append(m.Pkgs, pkg)
   332  					}
   333  				}
   334  
   335  			case m.IsLiteral():
   336  				m.Pkgs = []string{m.Pattern()}
   337  
   338  			case strings.Contains(m.Pattern(), "..."):
   339  				m.Errs = m.Errs[:0]
   340  				mg, err := rs.Graph(ld, ctx)
   341  				if err != nil {
   342  					// The module graph is (or may be) incomplete — perhaps we failed to
   343  					// load the requirements of some module. This is an error in matching
   344  					// the patterns to packages, because we may be missing some packages
   345  					// or we may erroneously match packages in the wrong versions of
   346  					// modules. However, for cases like 'go list -e', the error should not
   347  					// necessarily prevent us from loading the packages we could find.
   348  					m.Errs = append(m.Errs, err)
   349  				}
   350  				matchPackages(ld, ctx, m, opts.Tags, includeStd, mg.BuildList())
   351  
   352  			case m.Pattern() == "work":
   353  				matchModules := ld.MainModules.Versions()
   354  				if opts.MainModule != (module.Version{}) {
   355  					matchModules = []module.Version{opts.MainModule}
   356  				}
   357  				matchPackages(ld, ctx, m, opts.Tags, omitStd, matchModules)
   358  
   359  			case m.Pattern() == "all":
   360  				if pld == nil {
   361  					// The initial roots are the packages and tools in the main module.
   362  					// loadFromRoots will expand that to "all".
   363  					m.Errs = m.Errs[:0]
   364  					matchModules := ld.MainModules.Versions()
   365  					if opts.MainModule != (module.Version{}) {
   366  						matchModules = []module.Version{opts.MainModule}
   367  					}
   368  					matchPackages(ld, ctx, m, opts.Tags, omitStd, matchModules)
   369  					for tool := range ld.MainModules.Tools() {
   370  						m.Pkgs = append(m.Pkgs, tool)
   371  					}
   372  				} else {
   373  					// Starting with the packages in the main module,
   374  					// enumerate the full list of "all".
   375  					m.Pkgs = pld.computePatternAll()
   376  				}
   377  
   378  			case m.Pattern() == "std" || m.Pattern() == "cmd":
   379  				if m.Pkgs == nil {
   380  					m.MatchPackages() // Locate the packages within GOROOT/src.
   381  				}
   382  
   383  			case m.Pattern() == "tool":
   384  				for tool := range ld.MainModules.Tools() {
   385  					m.Pkgs = append(m.Pkgs, tool)
   386  				}
   387  			default:
   388  				panic(fmt.Sprintf("internal error: modload missing case for pattern %s", m.Pattern()))
   389  			}
   390  		}
   391  	}
   392  
   393  	initialRS, err := loadModFile(ld, ctx, &opts)
   394  	if err != nil {
   395  		base.Fatal(err)
   396  	}
   397  
   398  	pld := loadFromRoots(ld, ctx, loaderParams{
   399  		PackageOpts:  opts,
   400  		requirements: initialRS,
   401  
   402  		allPatternIsRoot: allPatternIsRoot,
   403  
   404  		listRoots: func(rs *Requirements) (roots []string) {
   405  			updateMatches(rs, nil)
   406  			for _, m := range matches {
   407  				roots = append(roots, m.Pkgs...)
   408  			}
   409  			return roots
   410  		},
   411  	})
   412  
   413  	// One last pass to finalize wildcards.
   414  	updateMatches(pld.requirements, pld)
   415  
   416  	// List errors in matching patterns (such as directory permission
   417  	// errors for wildcard patterns).
   418  	if !pld.SilencePackageErrors {
   419  		for _, match := range matches {
   420  			for _, err := range match.Errs {
   421  				pld.error(err)
   422  			}
   423  		}
   424  	}
   425  	pld.exitIfErrors(ctx)
   426  
   427  	if !opts.SilenceUnmatchedWarnings {
   428  		search.WarnUnmatched(matches)
   429  	}
   430  
   431  	if opts.Tidy {
   432  		if cfg.BuildV {
   433  			mg, _ := pld.requirements.Graph(ld, ctx)
   434  			for _, m := range initialRS.rootModules {
   435  				var unused bool
   436  				if pld.requirements.pruning == unpruned {
   437  					// m is unused if it was dropped from the module graph entirely. If it
   438  					// was only demoted from direct to indirect, it may still be in use via
   439  					// a transitive import.
   440  					unused = mg.Selected(m.Path) == "none"
   441  				} else {
   442  					// m is unused if it was dropped from the roots. If it is still present
   443  					// as a transitive dependency, that transitive dependency is not needed
   444  					// by any package or test in the main module.
   445  					_, ok := pld.requirements.rootSelected(ld, m.Path)
   446  					unused = !ok
   447  				}
   448  				if unused {
   449  					fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
   450  				}
   451  			}
   452  		}
   453  
   454  		keep := keepSums(ld, ctx, pld, pld.requirements, loadedZipSumsOnly)
   455  		compatVersion := pld.TidyCompatibleVersion
   456  		goVersion := pld.requirements.GoVersion(ld)
   457  		if compatVersion == "" {
   458  			if gover.Compare(goVersion, gover.GoStrictVersion) < 0 {
   459  				compatVersion = gover.Prev(goVersion)
   460  			} else {
   461  				// Starting at GoStrictVersion, we no longer maintain compatibility with
   462  				// versions older than what is listed in the go.mod file.
   463  				compatVersion = goVersion
   464  			}
   465  		}
   466  		if gover.Compare(compatVersion, goVersion) > 0 {
   467  			// Each version of the Go toolchain knows how to interpret go.mod and
   468  			// go.sum files produced by all previous versions, so a compatibility
   469  			// version higher than the go.mod version adds nothing.
   470  			compatVersion = goVersion
   471  		}
   472  		if compatPruning := pruningForGoVersion(compatVersion); compatPruning != pld.requirements.pruning {
   473  			compatRS := newRequirements(ld, compatPruning, pld.requirements.rootModules, pld.requirements.direct)
   474  			pld.checkTidyCompatibility(ld, ctx, compatRS, compatVersion)
   475  
   476  			for m := range keepSums(ld, ctx, pld, compatRS, loadedZipSumsOnly) {
   477  				keep[m] = true
   478  			}
   479  		}
   480  
   481  		if opts.TidyDiff {
   482  			cfg.BuildMod = "readonly"
   483  			ld.pkgLoader = pld
   484  			ld.requirements = ld.pkgLoader.requirements
   485  			currentGoMod, updatedGoMod, _, err := UpdateGoModFromReqs(ld, ctx, WriteOpts{})
   486  			if err != nil {
   487  				base.Fatal(err)
   488  			}
   489  			goModDiff := diff.Diff("current/go.mod", currentGoMod, "tidy/go.mod", updatedGoMod)
   490  
   491  			ld.Fetcher().TrimGoSum(keep)
   492  			// Dropping compatibility for 1.16 may result in a strictly smaller go.sum.
   493  			// Update the keep map with only the loaded.requirements.
   494  			if gover.Compare(compatVersion, "1.16") > 0 {
   495  				keep = keepSums(ld, ctx, ld.pkgLoader, ld.requirements, addBuildListZipSums)
   496  			}
   497  			currentGoSum, tidyGoSum := ld.fetcher.TidyGoSum(keep)
   498  			goSumDiff := diff.Diff("current/go.sum", currentGoSum, "tidy/go.sum", tidyGoSum)
   499  
   500  			if len(goModDiff) > 0 {
   501  				fmt.Println(string(goModDiff))
   502  				base.SetExitStatus(1)
   503  			}
   504  			if len(goSumDiff) > 0 {
   505  				fmt.Println(string(goSumDiff))
   506  				base.SetExitStatus(1)
   507  			}
   508  			base.Exit()
   509  		}
   510  
   511  		if !ExplicitWriteGoMod {
   512  			ld.Fetcher().TrimGoSum(keep)
   513  
   514  			// commitRequirements below will also call WriteGoSum, but the "keep" map
   515  			// we have here could be strictly larger: commitRequirements only commits
   516  			// loaded.requirements, but here we may have also loaded (and want to
   517  			// preserve checksums for) additional entities from compatRS, which are
   518  			// only needed for compatibility with ld.TidyCompatibleVersion.
   519  			if err := ld.Fetcher().WriteGoSum(ctx, keep, mustHaveCompleteRequirements(ld)); err != nil {
   520  				base.Fatal(err)
   521  			}
   522  		}
   523  	}
   524  
   525  	if opts.TidyDiff && !opts.Tidy {
   526  		panic("TidyDiff is set but Tidy is not.")
   527  	}
   528  
   529  	// Success! Update go.mod and go.sum (if needed) and return the results.
   530  	// We'll skip updating if ExplicitWriteGoMod is true (the caller has opted
   531  	// to call WriteGoMod itself) or if ResolveMissingImports is false (the
   532  	// command wants to examine the package graph as-is).
   533  	ld.pkgLoader = pld
   534  	ld.requirements = ld.pkgLoader.requirements
   535  
   536  	for _, pkg := range pld.pkgs {
   537  		if !pkg.isTest() {
   538  			loadedPackages = append(loadedPackages, pkg.path)
   539  		}
   540  	}
   541  	sort.Strings(loadedPackages)
   542  
   543  	if !ExplicitWriteGoMod && opts.ResolveMissingImports {
   544  		if err := commitRequirements(ld, ctx, WriteOpts{}); err != nil {
   545  			base.Fatal(err)
   546  		}
   547  	}
   548  
   549  	return matches, loadedPackages
   550  }
   551  
   552  // matchLocalDirs is like m.MatchDirs, but tries to avoid scanning directories
   553  // outside of the standard library and active modules.
   554  func matchLocalDirs(ld *Loader, ctx context.Context, modRoots []string, m *search.Match, rs *Requirements) {
   555  	if !m.IsLocal() {
   556  		panic(fmt.Sprintf("internal error: resolveLocalDirs on non-local pattern %s", m.Pattern()))
   557  	}
   558  
   559  	if i := strings.Index(m.Pattern(), "..."); i >= 0 {
   560  		// The pattern is local, but it is a wildcard. Its packages will
   561  		// only resolve to paths if they are inside of the standard
   562  		// library, the main module, or some dependency of the main
   563  		// module. Verify that before we walk the filesystem: a filesystem
   564  		// walk in a directory like /var or /etc can be very expensive!
   565  		dir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3]))
   566  		absDir := mkAbs(base.Cwd(), dir)
   567  
   568  		modRoot := findModuleRoot(absDir)
   569  		if !slices.Contains(modRoots, modRoot) && search.InDir(absDir, cfg.GOROOTsrc) == "" && pathInModuleCache(ld, ctx, absDir, rs) == "" {
   570  			m.Dirs = []string{}
   571  			scope := "main module or its selected dependencies"
   572  			if ld.inWorkspaceMode() {
   573  				scope = "modules listed in go.work or their selected dependencies"
   574  			}
   575  			m.AddError(fmt.Errorf("directory prefix %s does not contain %s", base.ShortPath(absDir), scope))
   576  			return
   577  		}
   578  	}
   579  
   580  	m.MatchDirs(modRoots)
   581  }
   582  
   583  // resolveLocalPackage resolves a filesystem path to a package path.
   584  func resolveLocalPackage(ld *Loader, ctx context.Context, absDir string, rs *Requirements) (string, error) {
   585  	bp, err := cfg.BuildContext.ImportDir(absDir, 0)
   586  	if err != nil && (bp == nil || len(bp.IgnoredGoFiles) == 0) {
   587  		// golang.org/issue/32917: We should resolve a relative path to a
   588  		// package path only if the relative path actually contains the code
   589  		// for that package.
   590  		//
   591  		// If the named directory does not exist or contains no Go files,
   592  		// the package does not exist.
   593  		// Other errors may affect package loading, but not resolution.
   594  		if _, err := fsys.Stat(absDir); err != nil {
   595  			if os.IsNotExist(err) {
   596  				// Canonicalize OS-specific errors to errDirectoryNotFound so that error
   597  				// messages will be easier for users to search for.
   598  				return "", &fs.PathError{Op: "stat", Path: absDir, Err: errDirectoryNotFound}
   599  			}
   600  			return "", err
   601  		}
   602  		if _, noGo := err.(*build.NoGoError); noGo {
   603  			// A directory that does not contain any Go source files — even ignored
   604  			// ones! — is not a Go package, and we can't resolve it to a package
   605  			// path because that path could plausibly be provided by some other
   606  			// module.
   607  			//
   608  			// Any other error indicates that the package “exists” (at least in the
   609  			// sense that it cannot exist in any other module), but has some other
   610  			// problem (such as a syntax error).
   611  			return "", err
   612  		}
   613  	}
   614  
   615  	return localPackagePath(ld, ctx, absDir, rs)
   616  }
   617  
   618  func mkAbs(wd, path string) string {
   619  	if filepath.IsAbs(path) {
   620  		return filepath.Clean(path)
   621  	}
   622  	return filepath.Join(wd, path)
   623  }
   624  
   625  // localPackagePath resolves an absolute filesystem path to a package path.
   626  // The caller must have already verified that absDir contains a package.
   627  func localPackagePath(ld *Loader, ctx context.Context, absDir string, rs *Requirements) (string, error) {
   628  	for _, mod := range ld.MainModules.Versions() {
   629  		modRoot := ld.MainModules.ModRoot(mod)
   630  		if modRoot != "" && absDir == modRoot {
   631  			if absDir == cfg.GOROOTsrc {
   632  				return "", errPkgIsGorootSrc
   633  			}
   634  			return ld.MainModules.PathPrefix(mod), nil
   635  		}
   636  	}
   637  
   638  	// Note: The checks for @ here are just to avoid misinterpreting
   639  	// the module cache directories (formerly GOPATH/src/mod/foo@v1.5.2/bar).
   640  	// It's not strictly necessary but helpful to keep the checks.
   641  	var pkgNotFoundErr error
   642  	pkgNotFoundLongestPrefix := ""
   643  	for _, mainModule := range ld.MainModules.Versions() {
   644  		modRoot := ld.MainModules.ModRoot(mainModule)
   645  		if modRoot != "" && str.HasFilePathPrefix(absDir, modRoot) && !strings.Contains(absDir[len(modRoot):], "@") {
   646  			suffix := filepath.ToSlash(str.TrimFilePathPrefix(absDir, modRoot))
   647  			if pkg, found := strings.CutPrefix(suffix, "vendor/"); found {
   648  				if cfg.BuildMod != "vendor" {
   649  					return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir)
   650  				}
   651  
   652  				readVendorList(VendorDir(ld))
   653  				if _, ok := vendorPkgModule[pkg]; !ok {
   654  					return "", fmt.Errorf("directory %s is not a package listed in vendor/modules.txt", absDir)
   655  				}
   656  				return pkg, nil
   657  			}
   658  
   659  			mainModulePrefix := ld.MainModules.PathPrefix(mainModule)
   660  			if mainModulePrefix == "" {
   661  				pkg := suffix
   662  				if pkg == "builtin" {
   663  					// "builtin" is a pseudo-package with a real source file.
   664  					// It's not included in "std", so it shouldn't resolve from "."
   665  					// within module "std" either.
   666  					return "", errPkgIsBuiltin
   667  				}
   668  				return pkg, nil
   669  			}
   670  
   671  			pkg := pathpkg.Join(mainModulePrefix, suffix)
   672  			if _, ok, err := dirInModule(pkg, mainModulePrefix, modRoot, true); err != nil {
   673  				return "", err
   674  			} else if !ok {
   675  				// This main module could contain the directory but doesn't. Other main
   676  				// modules might contain the directory, so wait till we finish the loop
   677  				// to see if another main module contains directory. But if not,
   678  				// return an error.
   679  				if len(mainModulePrefix) > len(pkgNotFoundLongestPrefix) {
   680  					pkgNotFoundLongestPrefix = mainModulePrefix
   681  					pkgNotFoundErr = &PackageNotInModuleError{MainModules: []module.Version{mainModule}, Pattern: pkg}
   682  				}
   683  				continue
   684  			}
   685  			return pkg, nil
   686  		}
   687  	}
   688  	if pkgNotFoundErr != nil {
   689  		return "", pkgNotFoundErr
   690  	}
   691  
   692  	if sub := search.InDir(absDir, cfg.GOROOTsrc); sub != "" && sub != "." && !strings.Contains(sub, "@") {
   693  		pkg := filepath.ToSlash(sub)
   694  		if pkg == "builtin" {
   695  			return "", errPkgIsBuiltin
   696  		}
   697  		return pkg, nil
   698  	}
   699  
   700  	pkg := pathInModuleCache(ld, ctx, absDir, rs)
   701  	if pkg == "" {
   702  		dirstr := fmt.Sprintf("directory %s", base.ShortPath(absDir))
   703  		if dirstr == "directory ." {
   704  			dirstr = "current directory"
   705  		}
   706  		if ld.inWorkspaceMode() {
   707  			if mr := findModuleRoot(absDir); mr != "" {
   708  				return "", fmt.Errorf("%s is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using:\n\tgo work use %s", dirstr, base.ShortPath(mr))
   709  			}
   710  			return "", fmt.Errorf("%s outside modules listed in go.work or their selected dependencies", dirstr)
   711  		}
   712  		return "", fmt.Errorf("%s outside main module or its selected dependencies", dirstr)
   713  	}
   714  	return pkg, nil
   715  }
   716  
   717  var (
   718  	errDirectoryNotFound = errors.New("directory not found")
   719  	errPkgIsGorootSrc    = errors.New("GOROOT/src is not an importable package")
   720  	errPkgIsBuiltin      = errors.New(`"builtin" is a pseudo-package, not an importable package`)
   721  )
   722  
   723  // pathInModuleCache returns the import path of the directory dir,
   724  // if dir is in the module cache copy of a module in our build list.
   725  func pathInModuleCache(ld *Loader, ctx context.Context, dir string, rs *Requirements) string {
   726  	tryMod := func(m module.Version) (string, bool) {
   727  		if gover.IsToolchain(m.Path) {
   728  			return "", false
   729  		}
   730  		var root string
   731  		var err error
   732  		if repl := Replacement(ld, m); repl.Path != "" && repl.Version == "" {
   733  			root = repl.Path
   734  			if !filepath.IsAbs(root) {
   735  				root = filepath.Join(replaceRelativeTo(ld), root)
   736  			}
   737  		} else if repl.Path != "" {
   738  			root, err = modfetch.DownloadDir(ctx, repl)
   739  		} else {
   740  			root, err = modfetch.DownloadDir(ctx, m)
   741  		}
   742  		if err != nil {
   743  			return "", false
   744  		}
   745  
   746  		sub := search.InDir(dir, root)
   747  		if sub == "" {
   748  			return "", false
   749  		}
   750  		sub = filepath.ToSlash(sub)
   751  		if strings.Contains(sub, "/vendor/") || strings.HasPrefix(sub, "vendor/") || strings.Contains(sub, "@") {
   752  			return "", false
   753  		}
   754  
   755  		return pathpkg.Join(m.Path, filepath.ToSlash(sub)), true
   756  	}
   757  
   758  	if rs.pruning == pruned {
   759  		for _, m := range rs.rootModules {
   760  			if v, _ := rs.rootSelected(ld, m.Path); v != m.Version {
   761  				continue // m is a root, but we have a higher root for the same path.
   762  			}
   763  			if importPath, ok := tryMod(m); ok {
   764  				// checkMultiplePaths ensures that a module can be used for at most one
   765  				// requirement, so this must be it.
   766  				return importPath
   767  			}
   768  		}
   769  	}
   770  
   771  	// None of the roots contained dir, or the graph is unpruned (so we don't want
   772  	// to distinguish between roots and transitive dependencies). Either way,
   773  	// check the full graph to see if the directory is a non-root dependency.
   774  	//
   775  	// If the roots are not consistent with the full module graph, the selected
   776  	// versions of root modules may differ from what we already checked above.
   777  	// Re-check those paths too.
   778  
   779  	mg, _ := rs.Graph(ld, ctx)
   780  	var importPath string
   781  	for _, m := range mg.BuildList() {
   782  		var found bool
   783  		importPath, found = tryMod(m)
   784  		if found {
   785  			break
   786  		}
   787  	}
   788  	return importPath
   789  }
   790  
   791  // ImportFromFiles adds modules to the build list as needed
   792  // to satisfy the imports in the named Go source files.
   793  //
   794  // Errors in missing dependencies are silenced.
   795  //
   796  // TODO(bcmills): Silencing errors seems off. Take a closer look at this and
   797  // figure out what the error-reporting actually ought to be.
   798  func ImportFromFiles(ld *Loader, ctx context.Context, gofiles []string) {
   799  	rs := LoadModFile(ld, ctx)
   800  
   801  	tags := imports.Tags()
   802  	imports, testImports, err := imports.ScanFiles(gofiles, tags)
   803  	if err != nil {
   804  		base.Fatal(err)
   805  	}
   806  
   807  	ld.pkgLoader = loadFromRoots(ld, ctx, loaderParams{
   808  		PackageOpts: PackageOpts{
   809  			Tags:                  tags,
   810  			ResolveMissingImports: true,
   811  			SilencePackageErrors:  true,
   812  		},
   813  		requirements: rs,
   814  		listRoots: func(*Requirements) (roots []string) {
   815  			roots = append(roots, imports...)
   816  			roots = append(roots, testImports...)
   817  			return roots
   818  		},
   819  	})
   820  	ld.requirements = ld.pkgLoader.requirements
   821  
   822  	if !ExplicitWriteGoMod {
   823  		if err := commitRequirements(ld, ctx, WriteOpts{}); err != nil {
   824  			base.Fatal(err)
   825  		}
   826  	}
   827  }
   828  
   829  // DirImportPath returns the effective import path for dir,
   830  // provided it is within a main module, or else returns ".".
   831  func (mms *MainModuleSet) DirImportPath(ld *Loader, ctx context.Context, dir string) (path string, m module.Version) {
   832  	if !ld.HasModRoot() {
   833  		return ".", module.Version{}
   834  	}
   835  	LoadModFile(ld, ctx) // Sets targetPrefix.
   836  
   837  	if !filepath.IsAbs(dir) {
   838  		dir = filepath.Join(base.Cwd(), dir)
   839  	} else {
   840  		dir = filepath.Clean(dir)
   841  	}
   842  
   843  	var longestPrefix string
   844  	var longestPrefixPath string
   845  	var longestPrefixVersion module.Version
   846  	for _, v := range mms.Versions() {
   847  		modRoot := mms.ModRoot(v)
   848  		if dir == modRoot {
   849  			return mms.PathPrefix(v), v
   850  		}
   851  		if str.HasFilePathPrefix(dir, modRoot) {
   852  			pathPrefix := ld.MainModules.PathPrefix(v)
   853  			if pathPrefix > longestPrefix {
   854  				longestPrefix = pathPrefix
   855  				longestPrefixVersion = v
   856  				suffix := filepath.ToSlash(str.TrimFilePathPrefix(dir, modRoot))
   857  				if strings.HasPrefix(suffix, "vendor/") {
   858  					longestPrefixPath = suffix[len("vendor/"):]
   859  					continue
   860  				}
   861  				longestPrefixPath = pathpkg.Join(mms.PathPrefix(v), suffix)
   862  			}
   863  		}
   864  	}
   865  	if len(longestPrefix) > 0 {
   866  		return longestPrefixPath, longestPrefixVersion
   867  	}
   868  
   869  	return ".", module.Version{}
   870  }
   871  
   872  // PackageModule returns the module providing the package named by the import path.
   873  func (ld *Loader) PackageModule(path string) module.Version {
   874  	pkg, ok := ld.pkgLoader.pkgCache.Get(path)
   875  	if !ok {
   876  		return module.Version{}
   877  	}
   878  	return pkg.mod
   879  }
   880  
   881  // Lookup returns the source directory, import path, and any loading error for
   882  // the package at path as imported from the package in parentDir.
   883  // Lookup requires that one of the Load functions in this package has already
   884  // been called.
   885  func Lookup(ld *Loader, parentPath string, parentIsStd bool, path string) (dir, realPath string, err error) {
   886  	if path == "" {
   887  		panic("Lookup called with empty package path")
   888  	}
   889  
   890  	if parentIsStd {
   891  		path = ld.pkgLoader.stdVendor(ld, parentPath, path)
   892  	}
   893  	pkg, ok := ld.pkgLoader.pkgCache.Get(path)
   894  	if !ok {
   895  		// The loader should have found all the relevant paths.
   896  		// There are a few exceptions, though:
   897  		//	- during go list without -test, the p.Resolve calls to process p.TestImports and p.XTestImports
   898  		//	  end up here to canonicalize the import paths.
   899  		//	- during any load, non-loaded packages like "unsafe" end up here.
   900  		//	- during any load, build-injected dependencies like "runtime/cgo" end up here.
   901  		//	- because we ignore appengine/* in the module loader,
   902  		//	  the dependencies of any actual appengine/* library end up here.
   903  		dir := findStandardImportPath(path)
   904  		if dir != "" {
   905  			return dir, path, nil
   906  		}
   907  		return "", "", errMissing
   908  	}
   909  	return pkg.dir, pkg.path, pkg.err
   910  }
   911  
   912  // A packageLoader manages the process of loading information about
   913  // the required packages for a particular build,
   914  // checking that the packages are available in the module set,
   915  // and updating the module set if needed.
   916  type packageLoader struct {
   917  	loaderParams
   918  
   919  	// allClosesOverTests indicates whether the "all" pattern includes
   920  	// dependencies of tests outside the main module (as in Go 1.11–1.15).
   921  	// (Otherwise — as in Go 1.16+ — the "all" pattern includes only the packages
   922  	// transitively *imported by* the packages and tests in the main module.)
   923  	allClosesOverTests bool
   924  
   925  	// skipImportModFiles indicates whether we may skip loading go.mod files
   926  	// for imported packages (as in 'go mod tidy' in Go 1.17–1.20).
   927  	skipImportModFiles bool
   928  
   929  	work *par.Queue
   930  
   931  	// reset on each iteration
   932  	roots    []*loadPkg
   933  	pkgCache *par.Cache[string, *loadPkg]
   934  	pkgs     []*loadPkg // transitive closure of loaded packages and tests; populated in buildStacks
   935  }
   936  
   937  // loaderParams configure the packages loaded by, and the properties reported
   938  // by, a loader instance.
   939  type loaderParams struct {
   940  	PackageOpts
   941  	requirements *Requirements
   942  
   943  	allPatternIsRoot bool // Is the "all" pattern an additional root?
   944  
   945  	listRoots func(rs *Requirements) []string
   946  }
   947  
   948  func (pld *packageLoader) reset() {
   949  	select {
   950  	case <-pld.work.Idle():
   951  	default:
   952  		panic("loader.reset when not idle")
   953  	}
   954  
   955  	pld.roots = nil
   956  	pld.pkgCache = new(par.Cache[string, *loadPkg])
   957  	pld.pkgs = nil
   958  }
   959  
   960  // error reports an error via either os.Stderr or base.Error,
   961  // according to whether ld.AllowErrors is set.
   962  func (pld *packageLoader) error(err error) {
   963  	if pld.AllowErrors {
   964  		fmt.Fprintf(os.Stderr, "go: %v\n", err)
   965  	} else if pld.Switcher != nil {
   966  		pld.Switcher.Error(err)
   967  	} else {
   968  		base.Error(err)
   969  	}
   970  }
   971  
   972  // switchIfErrors switches toolchains if a switch is needed.
   973  func (pld *packageLoader) switchIfErrors(ctx context.Context) {
   974  	if pld.Switcher != nil {
   975  		pld.Switcher.Switch(ctx)
   976  	}
   977  }
   978  
   979  // exitIfErrors switches toolchains if a switch is needed
   980  // or else exits if any errors have been reported.
   981  func (pld *packageLoader) exitIfErrors(ctx context.Context) {
   982  	pld.switchIfErrors(ctx)
   983  	base.ExitIfErrors()
   984  }
   985  
   986  // goVersion reports the Go version that should be used for the loader's
   987  // requirements: ld.TidyGoVersion if set, or ld.requirements.GoVersion()
   988  // otherwise.
   989  func (pld *packageLoader) goVersion(ld *Loader) string {
   990  	if pld.TidyGoVersion != "" {
   991  		return pld.TidyGoVersion
   992  	}
   993  	return pld.requirements.GoVersion(ld)
   994  }
   995  
   996  // A loadPkg records information about a single loaded package.
   997  type loadPkg struct {
   998  	// Populated at construction time:
   999  	path   string // import path
  1000  	testOf *loadPkg
  1001  
  1002  	// Populated at construction time and updated by (*packageLoader).applyPkgFlags:
  1003  	flags atomicLoadPkgFlags
  1004  
  1005  	// Populated by (*packageLoader).load:
  1006  	mod         module.Version // module providing package
  1007  	dir         string         // directory containing source code
  1008  	err         error          // error loading package
  1009  	imports     []*loadPkg     // packages imported by this one
  1010  	testImports []string       // test-only imports, saved for use by pkg.test.
  1011  	inStd       bool
  1012  	altMods     []module.Version // modules that could have contained the package but did not
  1013  
  1014  	// Populated by (*packageLoader).pkgTest:
  1015  	testOnce sync.Once
  1016  	test     *loadPkg
  1017  
  1018  	// Populated by postprocessing in (*packageLoader).buildStacks:
  1019  	stack *loadPkg // package importing this one in minimal import stack for this pkg
  1020  }
  1021  
  1022  // loadPkgFlags is a set of flags tracking metadata about a package.
  1023  type loadPkgFlags int8
  1024  
  1025  const (
  1026  	// pkgInAll indicates that the package is in the "all" package pattern,
  1027  	// regardless of whether we are loading the "all" package pattern.
  1028  	//
  1029  	// When the pkgInAll flag and pkgImportsLoaded flags are both set, the caller
  1030  	// who set the last of those flags must propagate the pkgInAll marking to all
  1031  	// of the imports of the marked package.
  1032  	//
  1033  	// A test is marked with pkgInAll if that test would promote the packages it
  1034  	// imports to be in "all" (such as when the test is itself within the main
  1035  	// module, or when ld.allClosesOverTests is true).
  1036  	pkgInAll loadPkgFlags = 1 << iota
  1037  
  1038  	// pkgIsRoot indicates that the package matches one of the root package
  1039  	// patterns requested by the caller.
  1040  	//
  1041  	// If LoadTests is set, then when pkgIsRoot and pkgImportsLoaded are both set,
  1042  	// the caller who set the last of those flags must populate a test for the
  1043  	// package (in the pkg.test field).
  1044  	//
  1045  	// If the "all" pattern is included as a root, then non-test packages in "all"
  1046  	// are also roots (and must be marked pkgIsRoot).
  1047  	pkgIsRoot
  1048  
  1049  	// pkgFromRoot indicates that the package is in the transitive closure of
  1050  	// imports starting at the roots. (Note that every package marked as pkgIsRoot
  1051  	// is also trivially marked pkgFromRoot.)
  1052  	pkgFromRoot
  1053  
  1054  	// pkgImportsLoaded indicates that the imports and testImports fields of a
  1055  	// loadPkg have been populated.
  1056  	pkgImportsLoaded
  1057  )
  1058  
  1059  // has reports whether all of the flags in cond are set in f.
  1060  func (f loadPkgFlags) has(cond loadPkgFlags) bool {
  1061  	return f&cond == cond
  1062  }
  1063  
  1064  // An atomicLoadPkgFlags stores a loadPkgFlags for which individual flags can be
  1065  // added atomically.
  1066  type atomicLoadPkgFlags struct {
  1067  	bits atomic.Int32
  1068  }
  1069  
  1070  // update sets the given flags in af (in addition to any flags already set).
  1071  //
  1072  // update returns the previous flag state so that the caller may determine which
  1073  // flags were newly-set.
  1074  func (af *atomicLoadPkgFlags) update(flags loadPkgFlags) (old loadPkgFlags) {
  1075  	for {
  1076  		old := af.bits.Load()
  1077  		new := old | int32(flags)
  1078  		if new == old || af.bits.CompareAndSwap(old, new) {
  1079  			return loadPkgFlags(old)
  1080  		}
  1081  	}
  1082  }
  1083  
  1084  // has reports whether all of the flags in cond are set in af.
  1085  func (af *atomicLoadPkgFlags) has(cond loadPkgFlags) bool {
  1086  	return loadPkgFlags(af.bits.Load())&cond == cond
  1087  }
  1088  
  1089  // isTest reports whether pkg is a test of another package.
  1090  func (pkg *loadPkg) isTest() bool {
  1091  	return pkg.testOf != nil
  1092  }
  1093  
  1094  // fromExternalModule reports whether pkg was loaded from a module other than
  1095  // the main module.
  1096  func (pkg *loadPkg) fromExternalModule(ld *Loader) bool {
  1097  	if pkg.mod.Path == "" {
  1098  		return false // loaded from the standard library, not a module
  1099  	}
  1100  	return !ld.MainModules.Contains(pkg.mod.Path)
  1101  }
  1102  
  1103  var errMissing = errors.New("cannot find package")
  1104  
  1105  // loadFromRoots attempts to load the build graph needed to process a set of
  1106  // root packages and their dependencies.
  1107  //
  1108  // The set of root packages is returned by the params.listRoots function, and
  1109  // expanded to the full set of packages by tracing imports (and possibly tests)
  1110  // as needed.
  1111  func loadFromRoots(ld *Loader, ctx context.Context, params loaderParams) *packageLoader {
  1112  	pld := &packageLoader{
  1113  		loaderParams: params,
  1114  		work:         par.NewQueue(runtime.GOMAXPROCS(0)),
  1115  	}
  1116  
  1117  	if pld.requirements.pruning == unpruned {
  1118  		// If the module graph does not support pruning, we assume that we will need
  1119  		// the full module graph in order to load package dependencies.
  1120  		//
  1121  		// This might not be strictly necessary, but it matches the historical
  1122  		// behavior of the 'go' command and keeps the go.mod file more consistent in
  1123  		// case of erroneous hand-edits — which are less likely to be detected by
  1124  		// spot-checks in modules that do not maintain the expanded go.mod
  1125  		// requirements needed for graph pruning.
  1126  		var err error
  1127  		pld.requirements, _, err = expandGraph(ld, ctx, pld.requirements)
  1128  		if err != nil {
  1129  			pld.error(err)
  1130  		}
  1131  	}
  1132  	pld.exitIfErrors(ctx)
  1133  
  1134  	updateGoVersion := func() {
  1135  		goVersion := pld.goVersion(ld)
  1136  
  1137  		if pld.requirements.pruning != workspace {
  1138  			var err error
  1139  			pld.requirements, err = convertPruning(ld, ctx, pld.requirements, pruningForGoVersion(goVersion))
  1140  			if err != nil {
  1141  				pld.error(err)
  1142  				pld.exitIfErrors(ctx)
  1143  			}
  1144  		}
  1145  
  1146  		// If the module's Go version omits go.sum entries for go.mod files for test
  1147  		// dependencies of external packages, avoid loading those files in the first
  1148  		// place.
  1149  		pld.skipImportModFiles = pld.Tidy && gover.Compare(goVersion, gover.TidyGoModSumVersion) < 0
  1150  
  1151  		// If the module's go version explicitly predates the change in "all" for
  1152  		// graph pruning, continue to use the older interpretation.
  1153  		pld.allClosesOverTests = gover.Compare(goVersion, gover.NarrowAllVersion) < 0 && !pld.UseVendorAll
  1154  	}
  1155  
  1156  	for {
  1157  		pld.reset()
  1158  		updateGoVersion()
  1159  
  1160  		// Load the root packages and their imports.
  1161  		// Note: the returned roots can change on each iteration,
  1162  		// since the expansion of package patterns depends on the
  1163  		// build list we're using.
  1164  		rootPkgs := pld.listRoots(pld.requirements)
  1165  
  1166  		if pld.requirements.pruning == pruned && cfg.BuildMod == "mod" {
  1167  			// Before we start loading transitive imports of packages, locate all of
  1168  			// the root packages and promote their containing modules to root modules
  1169  			// dependencies. If their go.mod files are tidy (the common case) and the
  1170  			// set of root packages does not change then we can select the correct
  1171  			// versions of all transitive imports on the first try and complete
  1172  			// loading in a single iteration.
  1173  			changedBuildList := pld.preloadRootModules(ld, ctx, rootPkgs)
  1174  			if changedBuildList {
  1175  				// The build list has changed, so the set of root packages may have also
  1176  				// changed. Start over to pick up the changes. (Preloading roots is much
  1177  				// cheaper than loading the full import graph, so we would rather pay
  1178  				// for an extra iteration of preloading than potentially end up
  1179  				// discarding the result of a full iteration of loading.)
  1180  				continue
  1181  			}
  1182  		}
  1183  
  1184  		inRoots := map[*loadPkg]bool{}
  1185  		for _, path := range rootPkgs {
  1186  			root := pld.pkg(ld, ctx, path, pkgIsRoot)
  1187  			if !inRoots[root] {
  1188  				pld.roots = append(pld.roots, root)
  1189  				inRoots[root] = true
  1190  			}
  1191  		}
  1192  
  1193  		// ld.pkg adds imported packages to the work queue and calls applyPkgFlags,
  1194  		// which adds tests (and test dependencies) as needed.
  1195  		//
  1196  		// When all of the work in the queue has completed, we'll know that the
  1197  		// transitive closure of dependencies has been loaded.
  1198  		<-pld.work.Idle()
  1199  
  1200  		pld.buildStacks()
  1201  
  1202  		changed, err := pld.updateRequirements(ld, ctx)
  1203  		if err != nil {
  1204  			pld.error(err)
  1205  			break
  1206  		}
  1207  		if changed {
  1208  			// Don't resolve missing imports until the module graph has stabilized.
  1209  			// If the roots are still changing, they may turn out to specify a
  1210  			// requirement on the missing package(s), and we would rather use a
  1211  			// version specified by a new root than add a new dependency on an
  1212  			// unrelated version.
  1213  			continue
  1214  		}
  1215  
  1216  		if !pld.ResolveMissingImports || (!ld.HasModRoot() && !ld.allowMissingModuleImports) {
  1217  			// We've loaded as much as we can without resolving missing imports.
  1218  			break
  1219  		}
  1220  
  1221  		modAddedBy, err := pld.resolveMissingImports(ld, ctx)
  1222  		if err != nil {
  1223  			pld.error(err)
  1224  			break
  1225  		}
  1226  		if len(modAddedBy) == 0 {
  1227  			// The roots are stable, and we've resolved all of the missing packages
  1228  			// that we can.
  1229  			break
  1230  		}
  1231  
  1232  		toAdd := make([]module.Version, 0, len(modAddedBy))
  1233  		for m := range modAddedBy {
  1234  			toAdd = append(toAdd, m)
  1235  		}
  1236  		gover.ModSort(toAdd) // to make errors deterministic
  1237  
  1238  		// We ran updateRequirements before resolving missing imports and it didn't
  1239  		// make any changes, so we know that the requirement graph is already
  1240  		// consistent with ld.pkgs: we don't need to pass ld.pkgs to updateRoots
  1241  		// again. (That would waste time looking for changes that we have already
  1242  		// applied.)
  1243  		var noPkgs []*loadPkg
  1244  		// We also know that we're going to call updateRequirements again next
  1245  		// iteration so we don't need to also update it here. (That would waste time
  1246  		// computing a "direct" map that we'll have to recompute later anyway.)
  1247  		direct := pld.requirements.direct
  1248  		rs, err := updateRoots(ld, ctx, direct, pld.requirements, noPkgs, toAdd, pld.AssumeRootsImported)
  1249  		if err != nil {
  1250  			// If an error was found in a newly added module, report the package
  1251  			// import stack instead of the module requirement stack. Packages
  1252  			// are more descriptive.
  1253  			if err, ok := err.(*mvs.BuildListError); ok {
  1254  				if pkg := modAddedBy[err.Module()]; pkg != nil {
  1255  					pld.error(fmt.Errorf("%s: %w", pkg.stackText(), err.Err))
  1256  					break
  1257  				}
  1258  			}
  1259  			pld.error(err)
  1260  			break
  1261  		}
  1262  		if slices.Equal(rs.rootModules, pld.requirements.rootModules) {
  1263  			// Something is deeply wrong. resolveMissingImports gave us a non-empty
  1264  			// set of modules to add to the graph, but adding those modules had no
  1265  			// effect — either they were already in the graph, or updateRoots did not
  1266  			// add them as requested.
  1267  			panic(fmt.Sprintf("internal error: adding %v to module graph had no effect on root requirements (%v)", toAdd, rs.rootModules))
  1268  		}
  1269  		pld.requirements = rs
  1270  	}
  1271  	pld.exitIfErrors(ctx)
  1272  
  1273  	// Tidy the build list, if applicable, before we report errors.
  1274  	// (The process of tidying may remove errors from irrelevant dependencies.)
  1275  	if pld.Tidy {
  1276  		rs, err := tidyRoots(ld, ctx, pld.requirements, pld.pkgs)
  1277  		if err != nil {
  1278  			pld.error(err)
  1279  		} else {
  1280  			if pld.TidyGoVersion != "" {
  1281  				// Attempt to switch to the requested Go version. We have been using its
  1282  				// pruning and semantics all along, but there may have been — and may
  1283  				// still be — requirements on higher versions in the graph.
  1284  				tidy := overrideRoots(ld, ctx, rs, []module.Version{{Path: "go", Version: pld.TidyGoVersion}})
  1285  				mg, err := tidy.Graph(ld, ctx)
  1286  				if err != nil {
  1287  					pld.error(err)
  1288  				}
  1289  				if v := mg.Selected("go"); v == pld.TidyGoVersion {
  1290  					rs = tidy
  1291  				} else {
  1292  					conflict := Conflict{
  1293  						Path: mg.g.FindPath(func(m module.Version) bool {
  1294  							return m.Path == "go" && m.Version == v
  1295  						})[1:],
  1296  						Constraint: module.Version{Path: "go", Version: pld.TidyGoVersion},
  1297  					}
  1298  					msg := conflict.Summary()
  1299  					if cfg.BuildV {
  1300  						msg = conflict.String()
  1301  					}
  1302  					pld.error(errors.New(msg))
  1303  				}
  1304  			}
  1305  
  1306  			if pld.requirements.pruning == pruned {
  1307  				// We continuously add tidy roots to ld.requirements during loading, so
  1308  				// at this point the tidy roots (other than possibly the "go" version
  1309  				// edited above) should be a subset of the roots of ld.requirements,
  1310  				// ensuring that no new dependencies are brought inside the
  1311  				// graph-pruning horizon.
  1312  				// If that is not the case, there is a bug in the loading loop above.
  1313  				for _, m := range rs.rootModules {
  1314  					if m.Path == "go" && pld.TidyGoVersion != "" {
  1315  						continue
  1316  					}
  1317  					if v, ok := pld.requirements.rootSelected(ld, m.Path); !ok || v != m.Version {
  1318  						pld.error(fmt.Errorf("internal error: a requirement on %v is needed but was not added during package loading (selected %s)", m, v))
  1319  					}
  1320  				}
  1321  			}
  1322  
  1323  			pld.requirements = rs
  1324  		}
  1325  
  1326  		pld.exitIfErrors(ctx)
  1327  	}
  1328  
  1329  	// Report errors, if any.
  1330  	for _, pkg := range pld.pkgs {
  1331  		if pkg.err == nil {
  1332  			continue
  1333  		}
  1334  
  1335  		// Add importer information to checksum errors.
  1336  		if sumErr, ok := errors.AsType[*ImportMissingSumError](pkg.err); ok {
  1337  			if importer := pkg.stack; importer != nil {
  1338  				sumErr.importer = importer.path
  1339  				sumErr.importerVersion = importer.mod.Version
  1340  				sumErr.importerIsTest = importer.testOf != nil
  1341  			}
  1342  		}
  1343  
  1344  		if stdErr, ok := errors.AsType[*ImportMissingError](pkg.err); ok && stdErr.isStd {
  1345  			// Add importer go version information to import errors of standard
  1346  			// library packages arising from newer releases.
  1347  			if importer := pkg.stack; importer != nil {
  1348  				if v, ok := rawGoVersion.Load(importer.mod); ok && gover.Compare(gover.Local(), v.(string)) < 0 {
  1349  					stdErr.importerGoVersion = v.(string)
  1350  				}
  1351  			}
  1352  			if pld.SilenceMissingStdImports {
  1353  				continue
  1354  			}
  1355  		}
  1356  		if pld.SilencePackageErrors {
  1357  			continue
  1358  		}
  1359  		if pld.SilenceNoGoErrors && errors.Is(pkg.err, imports.ErrNoGo) {
  1360  			continue
  1361  		}
  1362  
  1363  		pld.error(fmt.Errorf("%s: %w", pkg.stackText(), pkg.err))
  1364  	}
  1365  
  1366  	pld.checkMultiplePaths(ld)
  1367  	return pld
  1368  }
  1369  
  1370  // updateRequirements ensures that ld.requirements is consistent with the
  1371  // information gained from ld.pkgs.
  1372  //
  1373  // In particular:
  1374  //
  1375  //   - Modules that provide packages directly imported from the main module are
  1376  //     marked as direct, and are promoted to explicit roots. If a needed root
  1377  //     cannot be promoted due to -mod=readonly or -mod=vendor, the importing
  1378  //     package is marked with an error.
  1379  //
  1380  //   - If ld scanned the "all" pattern independent of build constraints, it is
  1381  //     guaranteed to have seen every direct import. Module dependencies that did
  1382  //     not provide any directly-imported package are then marked as indirect.
  1383  //
  1384  //   - Root dependencies are updated to their selected versions.
  1385  //
  1386  // The "changed" return value reports whether the update changed the selected
  1387  // version of any module that either provided a loaded package or may now
  1388  // provide a package that was previously unresolved.
  1389  func (pld *packageLoader) updateRequirements(ld *Loader, ctx context.Context) (changed bool, err error) {
  1390  	rs := pld.requirements
  1391  
  1392  	// direct contains the set of modules believed to provide packages directly
  1393  	// imported by the main module.
  1394  	var direct map[string]bool
  1395  
  1396  	// If we didn't scan all of the imports from the main module, or didn't use
  1397  	// imports.AnyTags, then we didn't necessarily load every package that
  1398  	// contributes “direct” imports — so we can't safely mark existing direct
  1399  	// dependencies in ld.requirements as indirect-only. Propagate them as direct.
  1400  	loadedDirect := pld.allPatternIsRoot && maps.Equal(pld.Tags, imports.AnyTags())
  1401  	if loadedDirect {
  1402  		direct = make(map[string]bool)
  1403  	} else {
  1404  		// TODO(bcmills): It seems like a shame to allocate and copy a map here when
  1405  		// it will only rarely actually vary from rs.direct. Measure this cost and
  1406  		// maybe avoid the copy.
  1407  		direct = make(map[string]bool, len(rs.direct))
  1408  		for mPath := range rs.direct {
  1409  			direct[mPath] = true
  1410  		}
  1411  	}
  1412  
  1413  	var maxTooNew *gover.TooNewError
  1414  	for _, pkg := range pld.pkgs {
  1415  		if pkg.err != nil {
  1416  			if tooNew, ok := errors.AsType[*gover.TooNewError](pkg.err); ok {
  1417  				if maxTooNew == nil || gover.Compare(tooNew.GoVersion, maxTooNew.GoVersion) > 0 {
  1418  					maxTooNew = tooNew
  1419  				}
  1420  			}
  1421  		}
  1422  		if pkg.mod.Version != "" || !ld.MainModules.Contains(pkg.mod.Path) {
  1423  			continue
  1424  		}
  1425  
  1426  		for _, dep := range pkg.imports {
  1427  			if !dep.fromExternalModule(ld) {
  1428  				continue
  1429  			}
  1430  
  1431  			if ld.inWorkspaceMode() {
  1432  				// In workspace mode / workspace pruning mode, the roots are the main modules
  1433  				// rather than the main module's direct dependencies. The check below on the selected
  1434  				// roots does not apply.
  1435  				if cfg.BuildMod == "vendor" {
  1436  					// In workspace vendor mode, we don't need to load the requirements of the workspace
  1437  					// modules' dependencies so the check below doesn't work. But that's okay, because
  1438  					// checking whether modules are required directly for the purposes of pruning is
  1439  					// less important in vendor mode: if we were able to load the package, we have
  1440  					// everything we need  to build the package, and dependencies' tests are pruned out
  1441  					// of the vendor directory anyway.
  1442  					continue
  1443  				}
  1444  				if mg, err := rs.Graph(ld, ctx); err != nil {
  1445  					return false, err
  1446  				} else if _, ok := mg.RequiredBy(dep.mod); !ok {
  1447  					// dep.mod is not an explicit dependency, but needs to be.
  1448  					// See comment on error returned below.
  1449  					pkg.err = &DirectImportFromImplicitDependencyError{
  1450  						ImporterPath: pkg.path,
  1451  						ImportedPath: dep.path,
  1452  						Module:       dep.mod,
  1453  					}
  1454  				}
  1455  			} else if pkg.err == nil && cfg.BuildMod != "mod" {
  1456  				if v, ok := rs.rootSelected(ld, dep.mod.Path); !ok || v != dep.mod.Version {
  1457  					// dep.mod is not an explicit dependency, but needs to be.
  1458  					// Because we are not in "mod" mode, we will not be able to update it.
  1459  					// Instead, mark the importing package with an error.
  1460  					//
  1461  					// TODO(#41688): The resulting error message fails to include the file
  1462  					// position of the import statement (because that information is not
  1463  					// tracked by the module loader). Figure out how to plumb the import
  1464  					// position through.
  1465  					pkg.err = &DirectImportFromImplicitDependencyError{
  1466  						ImporterPath: pkg.path,
  1467  						ImportedPath: dep.path,
  1468  						Module:       dep.mod,
  1469  					}
  1470  					// cfg.BuildMod does not allow us to change dep.mod to be a direct
  1471  					// dependency, so don't mark it as such.
  1472  					continue
  1473  				}
  1474  			}
  1475  
  1476  			// dep is a package directly imported by a package or test in the main
  1477  			// module and loaded from some other module (not the standard library).
  1478  			// Mark its module as a direct dependency.
  1479  			direct[dep.mod.Path] = true
  1480  		}
  1481  	}
  1482  	if maxTooNew != nil {
  1483  		return false, maxTooNew
  1484  	}
  1485  
  1486  	var addRoots []module.Version
  1487  	if pld.Tidy {
  1488  		// When we are tidying a module with a pruned dependency graph, we may need
  1489  		// to add roots to preserve the versions of indirect, test-only dependencies
  1490  		// that are upgraded above or otherwise missing from the go.mod files of
  1491  		// direct dependencies. (For example, the direct dependency might be a very
  1492  		// stable codebase that predates modules and thus lacks a go.mod file, or
  1493  		// the author of the direct dependency may have forgotten to commit a change
  1494  		// to the go.mod file, or may have made an erroneous hand-edit that causes
  1495  		// it to be untidy.)
  1496  		//
  1497  		// Promoting an indirect dependency to a root adds the next layer of its
  1498  		// dependencies to the module graph, which may increase the selected
  1499  		// versions of other modules from which we have already loaded packages.
  1500  		// So after we promote an indirect dependency to a root, we need to reload
  1501  		// packages, which means another iteration of loading.
  1502  		//
  1503  		// As an extra wrinkle, the upgrades due to promoting a root can cause
  1504  		// previously-resolved packages to become unresolved. For example, the
  1505  		// module providing an unstable package might be upgraded to a version
  1506  		// that no longer contains that package. If we then resolve the missing
  1507  		// package, we might add yet another root that upgrades away some other
  1508  		// dependency. (The tests in mod_tidy_convergence*.txt illustrate some
  1509  		// particularly worrisome cases.)
  1510  		//
  1511  		// To ensure that this process of promoting, adding, and upgrading roots
  1512  		// eventually terminates, during iteration we only ever add modules to the
  1513  		// root set — we only remove irrelevant roots at the very end of
  1514  		// iteration, after we have already added every root that we plan to need
  1515  		// in the (eventual) tidy root set.
  1516  		//
  1517  		// Since we do not remove any roots during iteration, even if they no
  1518  		// longer provide any imported packages, the selected versions of the
  1519  		// roots can only increase and the set of roots can only expand. The set
  1520  		// of extant root paths is finite and the set of versions of each path is
  1521  		// finite, so the iteration *must* reach a stable fixed-point.
  1522  		tidy, err := tidyRoots(ld, ctx, rs, pld.pkgs)
  1523  		if err != nil {
  1524  			return false, err
  1525  		}
  1526  		addRoots = tidy.rootModules
  1527  	}
  1528  
  1529  	rs, err = updateRoots(ld, ctx, direct, rs, pld.pkgs, addRoots, pld.AssumeRootsImported)
  1530  	if err != nil {
  1531  		// We don't actually know what even the root requirements are supposed to be,
  1532  		// so we can't proceed with loading. Return the error to the caller
  1533  		return false, err
  1534  	}
  1535  
  1536  	if rs.GoVersion(ld) != pld.requirements.GoVersion(ld) {
  1537  		// A change in the selected Go version may or may not affect the set of
  1538  		// loaded packages, but in some cases it can change the meaning of the "all"
  1539  		// pattern, the level of pruning in the module graph, and even the set of
  1540  		// packages present in the standard library. If it has changed, it's best to
  1541  		// reload packages once more to be sure everything is stable.
  1542  		changed = true
  1543  	} else if rs != pld.requirements && !slices.Equal(rs.rootModules, pld.requirements.rootModules) {
  1544  		// The roots of the module graph have changed in some way (not just the
  1545  		// "direct" markings). Check whether the changes affected any of the loaded
  1546  		// packages.
  1547  		mg, err := rs.Graph(ld, ctx)
  1548  		if err != nil {
  1549  			return false, err
  1550  		}
  1551  		for _, pkg := range pld.pkgs {
  1552  			if pkg.fromExternalModule(ld) && mg.Selected(pkg.mod.Path) != pkg.mod.Version {
  1553  				changed = true
  1554  				break
  1555  			}
  1556  			if pkg.err != nil {
  1557  				// Promoting a module to a root may resolve an import that was
  1558  				// previously missing (by pulling in a previously-prune dependency that
  1559  				// provides it) or ambiguous (by promoting exactly one of the
  1560  				// alternatives to a root and ignoring the second-level alternatives) or
  1561  				// otherwise errored out (by upgrading from a version that cannot be
  1562  				// fetched to one that can be).
  1563  				//
  1564  				// Instead of enumerating all of the possible errors, we'll just check
  1565  				// whether importFromModules returns nil for the package.
  1566  				// False-positives are ok: if we have a false-positive here, we'll do an
  1567  				// extra iteration of package loading this time, but we'll still
  1568  				// converge when the root set stops changing.
  1569  				//
  1570  				// In some sense, we can think of this as ‘upgraded the module providing
  1571  				// pkg.path from "none" to a version higher than "none"’.
  1572  				if _, _, _, _, err = importFromModules(ld, ctx, pkg.path, rs, nil, pld.skipImportModFiles); err == nil {
  1573  					changed = true
  1574  					break
  1575  				}
  1576  			}
  1577  		}
  1578  	}
  1579  
  1580  	pld.requirements = rs
  1581  	return changed, nil
  1582  }
  1583  
  1584  // resolveMissingImports returns a set of modules that could be added as
  1585  // dependencies in order to resolve missing packages from pkgs.
  1586  //
  1587  // The newly-resolved packages are added to the addedModuleFor map, and
  1588  // resolveMissingImports returns a map from each new module version to
  1589  // the first missing package that module would resolve.
  1590  func (pld *packageLoader) resolveMissingImports(ld *Loader, ctx context.Context) (modAddedBy map[module.Version]*loadPkg, err error) {
  1591  	type pkgMod struct {
  1592  		pkg *loadPkg
  1593  		mod *module.Version
  1594  	}
  1595  	var pkgMods []pkgMod
  1596  	for _, pkg := range pld.pkgs {
  1597  		if pkg.err == nil {
  1598  			continue
  1599  		}
  1600  		if pkg.isTest() {
  1601  			// If we are missing a test, we are also missing its non-test version, and
  1602  			// we should only add the missing import once.
  1603  			continue
  1604  		}
  1605  		if _, ok := errors.AsType[*ImportMissingError](pkg.err); !ok {
  1606  			// Leave other errors for Import or load.Packages to report.
  1607  			continue
  1608  		}
  1609  
  1610  		pkg := pkg
  1611  		var mod module.Version
  1612  		pld.work.Add(func() {
  1613  			var err error
  1614  			mod, err = queryImport(ld, ctx, pkg.path, pld.requirements)
  1615  			if err != nil {
  1616  				if ime, ok := errors.AsType[*ImportMissingError](err); ok {
  1617  					for curstack := pkg.stack; curstack != nil; curstack = curstack.stack {
  1618  						if ld.MainModules.Contains(curstack.mod.Path) {
  1619  							ime.ImportingMainModule = curstack.mod
  1620  							ime.modRoot = ld.MainModules.ModRoot(ime.ImportingMainModule)
  1621  							break
  1622  						}
  1623  					}
  1624  				}
  1625  				// pkg.err was already non-nil, so we can reasonably attribute the error
  1626  				// for pkg to either the original error or the one returned by
  1627  				// queryImport. The existing error indicates only that we couldn't find
  1628  				// the package, whereas the query error also explains why we didn't fix
  1629  				// the problem — so we prefer the latter.
  1630  				pkg.err = err
  1631  			}
  1632  
  1633  			// err is nil, but we intentionally leave pkg.err non-nil and pkg.mod
  1634  			// unset: we still haven't satisfied other invariants of a
  1635  			// successfully-loaded package, such as scanning and loading the imports
  1636  			// of that package. If we succeed in resolving the new dependency graph,
  1637  			// the caller can reload pkg and update the error at that point.
  1638  			//
  1639  			// Even then, the package might not be loaded from the version we've
  1640  			// identified here. The module may be upgraded by some other dependency,
  1641  			// or by a transitive dependency of mod itself, or — less likely — the
  1642  			// package may be rejected by an AllowPackage hook or rendered ambiguous
  1643  			// by some other newly-added or newly-upgraded dependency.
  1644  		})
  1645  
  1646  		pkgMods = append(pkgMods, pkgMod{pkg: pkg, mod: &mod})
  1647  	}
  1648  	<-pld.work.Idle()
  1649  
  1650  	modAddedBy = map[module.Version]*loadPkg{}
  1651  
  1652  	var (
  1653  		maxTooNew    *gover.TooNewError
  1654  		maxTooNewPkg *loadPkg
  1655  	)
  1656  	for _, pm := range pkgMods {
  1657  		if tooNew, ok := errors.AsType[*gover.TooNewError](pm.pkg.err); ok {
  1658  			if maxTooNew == nil || gover.Compare(tooNew.GoVersion, maxTooNew.GoVersion) > 0 {
  1659  				maxTooNew = tooNew
  1660  				maxTooNewPkg = pm.pkg
  1661  			}
  1662  		}
  1663  	}
  1664  	if maxTooNew != nil {
  1665  		fmt.Fprintf(os.Stderr, "go: toolchain upgrade needed to resolve %s\n", maxTooNewPkg.path)
  1666  		return nil, maxTooNew
  1667  	}
  1668  
  1669  	for _, pm := range pkgMods {
  1670  		pkg, mod := pm.pkg, *pm.mod
  1671  		if mod.Path == "" {
  1672  			continue
  1673  		}
  1674  
  1675  		fmt.Fprintf(os.Stderr, "go: found %s in %s %s\n", pkg.path, mod.Path, mod.Version)
  1676  		if modAddedBy[mod] == nil {
  1677  			modAddedBy[mod] = pkg
  1678  		}
  1679  	}
  1680  
  1681  	return modAddedBy, nil
  1682  }
  1683  
  1684  // pkg locates the *loadPkg for path, creating and queuing it for loading if
  1685  // needed, and updates its state to reflect the given flags.
  1686  //
  1687  // The imports of the returned *loadPkg will be loaded asynchronously in the
  1688  // ld.work queue, and its test (if requested) will also be populated once
  1689  // imports have been resolved. When ld.work goes idle, all transitive imports of
  1690  // the requested package (and its test, if requested) will have been loaded.
  1691  func (pld *packageLoader) pkg(ld *Loader, ctx context.Context, path string, flags loadPkgFlags) *loadPkg {
  1692  	if flags.has(pkgImportsLoaded) {
  1693  		panic("internal error: (*packageLoader).pkg called with pkgImportsLoaded flag set")
  1694  	}
  1695  
  1696  	pkg := pld.pkgCache.Do(path, func() *loadPkg {
  1697  		pkg := &loadPkg{
  1698  			path: path,
  1699  		}
  1700  		pld.applyPkgFlags(ld, ctx, pkg, flags)
  1701  
  1702  		pld.work.Add(func() { pld.load(ld, ctx, pkg) })
  1703  		return pkg
  1704  	})
  1705  
  1706  	pld.applyPkgFlags(ld, ctx, pkg, flags)
  1707  	return pkg
  1708  }
  1709  
  1710  // applyPkgFlags updates pkg.flags to set the given flags and propagate the
  1711  // (transitive) effects of those flags, possibly loading or enqueueing further
  1712  // packages as a result.
  1713  func (pld *packageLoader) applyPkgFlags(ld *Loader, ctx context.Context, pkg *loadPkg, flags loadPkgFlags) {
  1714  	if flags == 0 {
  1715  		return
  1716  	}
  1717  
  1718  	if flags.has(pkgInAll) && pld.allPatternIsRoot && !pkg.isTest() {
  1719  		// This package matches a root pattern by virtue of being in "all".
  1720  		flags |= pkgIsRoot
  1721  	}
  1722  	if flags.has(pkgIsRoot) {
  1723  		flags |= pkgFromRoot
  1724  	}
  1725  
  1726  	old := pkg.flags.update(flags)
  1727  	new := old | flags
  1728  	if new == old || !new.has(pkgImportsLoaded) {
  1729  		// We either didn't change the state of pkg, or we don't know anything about
  1730  		// its dependencies yet. Either way, we can't usefully load its test or
  1731  		// update its dependencies.
  1732  		return
  1733  	}
  1734  
  1735  	if !pkg.isTest() {
  1736  		// Check whether we should add (or update the flags for) a test for pkg.
  1737  		// ld.pkgTest is idempotent and extra invocations are inexpensive,
  1738  		// so it's ok if we call it more than is strictly necessary.
  1739  		wantTest := false
  1740  		switch {
  1741  		case pld.allPatternIsRoot && ld.MainModules.Contains(pkg.mod.Path):
  1742  			// We are loading the "all" pattern, which includes packages imported by
  1743  			// tests in the main module. This package is in the main module, so we
  1744  			// need to identify the imports of its test even if LoadTests is not set.
  1745  			//
  1746  			// (We will filter out the extra tests explicitly in computePatternAll.)
  1747  			wantTest = true
  1748  
  1749  		case pld.allPatternIsRoot && pld.allClosesOverTests && new.has(pkgInAll):
  1750  			// This variant of the "all" pattern includes imports of tests of every
  1751  			// package that is itself in "all", and pkg is in "all", so its test is
  1752  			// also in "all" (as above).
  1753  			wantTest = true
  1754  
  1755  		case pld.LoadTests && new.has(pkgIsRoot):
  1756  			// LoadTest explicitly requests tests of “the root packages”.
  1757  			wantTest = true
  1758  		}
  1759  
  1760  		if wantTest {
  1761  			var testFlags loadPkgFlags
  1762  			if ld.MainModules.Contains(pkg.mod.Path) || (pld.allClosesOverTests && new.has(pkgInAll)) {
  1763  				// Tests of packages in the main module are in "all", in the sense that
  1764  				// they cause the packages they import to also be in "all". So are tests
  1765  				// of packages in "all" if "all" closes over test dependencies.
  1766  				testFlags |= pkgInAll
  1767  			}
  1768  			pld.pkgTest(ld, ctx, pkg, testFlags)
  1769  		}
  1770  	}
  1771  
  1772  	if new.has(pkgInAll) && !old.has(pkgInAll|pkgImportsLoaded) {
  1773  		// We have just marked pkg with pkgInAll, or we have just loaded its
  1774  		// imports, or both. Now is the time to propagate pkgInAll to the imports.
  1775  		for _, dep := range pkg.imports {
  1776  			pld.applyPkgFlags(ld, ctx, dep, pkgInAll)
  1777  		}
  1778  	}
  1779  
  1780  	if new.has(pkgFromRoot) && !old.has(pkgFromRoot|pkgImportsLoaded) {
  1781  		for _, dep := range pkg.imports {
  1782  			pld.applyPkgFlags(ld, ctx, dep, pkgFromRoot)
  1783  		}
  1784  	}
  1785  }
  1786  
  1787  // preloadRootModules loads the module requirements needed to identify the
  1788  // selected version of each module providing a package in rootPkgs,
  1789  // adding new root modules to the module graph if needed.
  1790  func (pld *packageLoader) preloadRootModules(ld *Loader, ctx context.Context, rootPkgs []string) (changedBuildList bool) {
  1791  	needc := make(chan map[module.Version]bool, 1)
  1792  	needc <- map[module.Version]bool{}
  1793  	for _, path := range rootPkgs {
  1794  		path := path
  1795  		pld.work.Add(func() {
  1796  			// First, try to identify the module containing the package using only roots.
  1797  			//
  1798  			// If the main module is tidy and the package is in "all" — or if we're
  1799  			// lucky — we can identify all of its imports without actually loading the
  1800  			// full module graph.
  1801  			m, _, _, _, err := importFromModules(ld, ctx, path, pld.requirements, nil, pld.skipImportModFiles)
  1802  			if err != nil {
  1803  				if _, ok := errors.AsType[*ImportMissingError](err); ok && pld.ResolveMissingImports {
  1804  					// This package isn't provided by any selected module.
  1805  					// If we can find it, it will be a new root dependency.
  1806  					m, err = queryImport(ld, ctx, path, pld.requirements)
  1807  				}
  1808  				if err != nil {
  1809  					// We couldn't identify the root module containing this package.
  1810  					// Leave it unresolved; we will report it during loading.
  1811  					return
  1812  				}
  1813  			}
  1814  			if m.Path == "" {
  1815  				// The package is in std or cmd. We don't need to change the root set.
  1816  				return
  1817  			}
  1818  
  1819  			v, ok := pld.requirements.rootSelected(ld, m.Path)
  1820  			if !ok || v != m.Version {
  1821  				// We found the requested package in m, but m is not a root, so
  1822  				// loadModGraph will not load its requirements. We need to promote the
  1823  				// module to a root to ensure that any other packages this package
  1824  				// imports are resolved from correct dependency versions.
  1825  				//
  1826  				// (This is the “argument invariant” from
  1827  				// https://golang.org/design/36460-lazy-module-loading.)
  1828  				need := <-needc
  1829  				need[m] = true
  1830  				needc <- need
  1831  			}
  1832  		})
  1833  	}
  1834  	<-pld.work.Idle()
  1835  
  1836  	need := <-needc
  1837  	if len(need) == 0 {
  1838  		return false // No roots to add.
  1839  	}
  1840  
  1841  	toAdd := make([]module.Version, 0, len(need))
  1842  	for m := range need {
  1843  		toAdd = append(toAdd, m)
  1844  	}
  1845  	gover.ModSort(toAdd)
  1846  
  1847  	rs, err := updateRoots(ld, ctx, pld.requirements.direct, pld.requirements, nil, toAdd, pld.AssumeRootsImported)
  1848  	if err != nil {
  1849  		// We are missing some root dependency, and for some reason we can't load
  1850  		// enough of the module dependency graph to add the missing root. Package
  1851  		// loading is doomed to fail, so fail quickly.
  1852  		pld.error(err)
  1853  		pld.exitIfErrors(ctx)
  1854  		return false
  1855  	}
  1856  	if slices.Equal(rs.rootModules, pld.requirements.rootModules) {
  1857  		// Something is deeply wrong. resolveMissingImports gave us a non-empty
  1858  		// set of modules to add to the graph, but adding those modules had no
  1859  		// effect — either they were already in the graph, or updateRoots did not
  1860  		// add them as requested.
  1861  		panic(fmt.Sprintf("internal error: adding %v to module graph had no effect on root requirements (%v)", toAdd, rs.rootModules))
  1862  	}
  1863  
  1864  	pld.requirements = rs
  1865  	return true
  1866  }
  1867  
  1868  // load loads an individual package.
  1869  func (pld *packageLoader) load(ld *Loader, ctx context.Context, pkg *loadPkg) {
  1870  	var mg *ModuleGraph
  1871  	if pld.requirements.pruning == unpruned {
  1872  		var err error
  1873  		mg, err = pld.requirements.Graph(ld, ctx)
  1874  		if err != nil {
  1875  			// We already checked the error from Graph in loadFromRoots and/or
  1876  			// updateRequirements, so we ignored the error on purpose and we should
  1877  			// keep trying to push past it.
  1878  			//
  1879  			// However, because mg may be incomplete (and thus may select inaccurate
  1880  			// versions), we shouldn't use it to load packages. Instead, we pass a nil
  1881  			// *ModuleGraph, which will cause mg to first try loading from only the
  1882  			// main module and root dependencies.
  1883  			mg = nil
  1884  		}
  1885  	}
  1886  
  1887  	var modroot string
  1888  	pkg.mod, modroot, pkg.dir, pkg.altMods, pkg.err = importFromModules(ld, ctx, pkg.path, pld.requirements, mg, pld.skipImportModFiles)
  1889  	if ld.MainModules.Tools()[pkg.path] {
  1890  		// Tools declared by main modules are always in "all".
  1891  		// We apply the package flags before returning so that missing
  1892  		// tool dependencies report an error https://go.dev/issue/70582
  1893  		pld.applyPkgFlags(ld, ctx, pkg, pkgInAll)
  1894  	}
  1895  	if pkg.dir == "" {
  1896  		return
  1897  	}
  1898  	if ld.MainModules.Contains(pkg.mod.Path) {
  1899  		// Go ahead and mark pkg as in "all". This provides the invariant that a
  1900  		// package that is *only* imported by other packages in "all" is always
  1901  		// marked as such before loading its imports.
  1902  		//
  1903  		// We don't actually rely on that invariant at the moment, but it may
  1904  		// improve efficiency somewhat and makes the behavior a bit easier to reason
  1905  		// about (by reducing churn on the flag bits of dependencies), and costs
  1906  		// essentially nothing (these atomic flag ops are essentially free compared
  1907  		// to scanning source code for imports).
  1908  		pld.applyPkgFlags(ld, ctx, pkg, pkgInAll)
  1909  	}
  1910  	if pld.AllowPackage != nil {
  1911  		if err := pld.AllowPackage(ctx, pkg.path, pkg.mod); err != nil {
  1912  			pkg.err = err
  1913  		}
  1914  	}
  1915  
  1916  	pkg.inStd = (search.IsStandardImportPath(pkg.path) && search.InDir(pkg.dir, cfg.GOROOTsrc) != "")
  1917  
  1918  	var imports, testImports []string
  1919  
  1920  	if cfg.BuildContext.Compiler == "gccgo" && pkg.inStd {
  1921  		// We can't scan standard packages for gccgo.
  1922  	} else {
  1923  		var err error
  1924  		imports, testImports, err = scanDir(modroot, pkg.dir, pld.Tags)
  1925  		if err != nil {
  1926  			pkg.err = err
  1927  			return
  1928  		}
  1929  	}
  1930  
  1931  	pkg.imports = make([]*loadPkg, 0, len(imports))
  1932  	var importFlags loadPkgFlags
  1933  	if pkg.flags.has(pkgInAll) {
  1934  		importFlags = pkgInAll
  1935  	}
  1936  	for _, path := range imports {
  1937  		if pkg.inStd {
  1938  			// Imports from packages in "std" and "cmd" should resolve using
  1939  			// GOROOT/src/vendor even when "std" is not the main module.
  1940  			path = pld.stdVendor(ld, pkg.path, path)
  1941  		}
  1942  		pkg.imports = append(pkg.imports, pld.pkg(ld, ctx, path, importFlags))
  1943  	}
  1944  	pkg.testImports = testImports
  1945  
  1946  	pld.applyPkgFlags(ld, ctx, pkg, pkgImportsLoaded)
  1947  }
  1948  
  1949  // pkgTest locates the test of pkg, creating it if needed, and updates its state
  1950  // to reflect the given flags.
  1951  //
  1952  // pkgTest requires that the imports of pkg have already been loaded (flagged
  1953  // with pkgImportsLoaded).
  1954  func (pld *packageLoader) pkgTest(ld *Loader, ctx context.Context, pkg *loadPkg, testFlags loadPkgFlags) *loadPkg {
  1955  	if pkg.isTest() {
  1956  		panic("pkgTest called on a test package")
  1957  	}
  1958  
  1959  	createdTest := false
  1960  	pkg.testOnce.Do(func() {
  1961  		pkg.test = &loadPkg{
  1962  			path:   pkg.path,
  1963  			testOf: pkg,
  1964  			mod:    pkg.mod,
  1965  			dir:    pkg.dir,
  1966  			err:    pkg.err,
  1967  			inStd:  pkg.inStd,
  1968  		}
  1969  		pld.applyPkgFlags(ld, ctx, pkg.test, testFlags)
  1970  		createdTest = true
  1971  	})
  1972  
  1973  	test := pkg.test
  1974  	if createdTest {
  1975  		test.imports = make([]*loadPkg, 0, len(pkg.testImports))
  1976  		var importFlags loadPkgFlags
  1977  		if test.flags.has(pkgInAll) {
  1978  			importFlags = pkgInAll
  1979  		}
  1980  		for _, path := range pkg.testImports {
  1981  			if pkg.inStd {
  1982  				path = pld.stdVendor(ld, test.path, path)
  1983  			}
  1984  			test.imports = append(test.imports, pld.pkg(ld, ctx, path, importFlags))
  1985  		}
  1986  		pkg.testImports = nil
  1987  		pld.applyPkgFlags(ld, ctx, test, pkgImportsLoaded)
  1988  	} else {
  1989  		pld.applyPkgFlags(ld, ctx, test, testFlags)
  1990  	}
  1991  
  1992  	return test
  1993  }
  1994  
  1995  // stdVendor returns the canonical import path for the package with the given
  1996  // path when imported from the standard-library package at parentPath.
  1997  func (pld *packageLoader) stdVendor(ld *Loader, parentPath, path string) string {
  1998  	if p, _, ok := fips140.ResolveImport(path); ok {
  1999  		return p
  2000  	}
  2001  	if search.IsStandardImportPath(path) {
  2002  		return path
  2003  	}
  2004  
  2005  	if str.HasPathPrefix(parentPath, "cmd") {
  2006  		if !pld.VendorModulesInGOROOTSrc || !ld.MainModules.Contains("cmd") {
  2007  			vendorPath := pathpkg.Join("cmd", "vendor", path)
  2008  
  2009  			if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
  2010  				return vendorPath
  2011  			}
  2012  		}
  2013  	} else if !pld.VendorModulesInGOROOTSrc || !ld.MainModules.Contains("std") || str.HasPathPrefix(parentPath, "vendor") {
  2014  		// If we are outside of the 'std' module, resolve imports from within 'std'
  2015  		// to the vendor directory.
  2016  		//
  2017  		// Do the same for importers beginning with the prefix 'vendor/' even if we
  2018  		// are *inside* of the 'std' module: the 'vendor/' packages that resolve
  2019  		// globally from GOROOT/src/vendor (and are listed as part of 'go list std')
  2020  		// are distinct from the real module dependencies, and cannot import
  2021  		// internal packages from the real module.
  2022  		//
  2023  		// (Note that although the 'vendor/' packages match the 'std' *package*
  2024  		// pattern, they are not part of the std *module*, and do not affect
  2025  		// 'go mod tidy' and similar module commands when working within std.)
  2026  		vendorPath := pathpkg.Join("vendor", path)
  2027  		if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
  2028  			return vendorPath
  2029  		}
  2030  	}
  2031  
  2032  	// Not vendored: resolve from modules.
  2033  	return path
  2034  }
  2035  
  2036  // computePatternAll returns the list of packages matching pattern "all",
  2037  // starting with a list of the import paths for the packages in the main module.
  2038  func (pld *packageLoader) computePatternAll() (all []string) {
  2039  	for _, pkg := range pld.pkgs {
  2040  		if module.CheckImportPath(pkg.path) != nil {
  2041  			// Don't add packages with invalid paths. This means that
  2042  			// we don't try to load invalid imports of the main modules'
  2043  			// packages. We will still report an errors invalid imports
  2044  			// when we load the importing package.
  2045  			continue
  2046  		}
  2047  		if pkg.flags.has(pkgInAll) && !pkg.isTest() {
  2048  			all = append(all, pkg.path)
  2049  		}
  2050  	}
  2051  	sort.Strings(all)
  2052  	return all
  2053  }
  2054  
  2055  // checkMultiplePaths verifies that a given module path is used as itself
  2056  // or as a replacement for another module, but not both at the same time.
  2057  //
  2058  // (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
  2059  func (pld *packageLoader) checkMultiplePaths(ld *Loader) {
  2060  	if cached := pld.requirements.graph.Load(); cached != nil {
  2061  		if mg := cached.mg; mg != nil {
  2062  			// The check depends only on the build list and workspace replace
  2063  			// directives, both fixed for the lifetime of mg, so skip it on
  2064  			// subsequent calls sharing the same graph.
  2065  			mg.checkPathsOnce.Do(func() {
  2066  				checkMultiplePathsUncached(ld, pld, mg.BuildList())
  2067  			})
  2068  			return
  2069  		}
  2070  	}
  2071  	checkMultiplePathsUncached(ld, pld, pld.requirements.rootModules)
  2072  }
  2073  
  2074  func checkMultiplePathsUncached(ld *Loader, pld *packageLoader, mods []module.Version) {
  2075  	firstPath := map[module.Version]string{}
  2076  	for _, mod := range mods {
  2077  		src := resolveReplacement(ld, mod)
  2078  		if prev, ok := firstPath[src]; !ok {
  2079  			firstPath[src] = mod.Path
  2080  		} else if prev != mod.Path {
  2081  			pld.error(fmt.Errorf("%s@%s used for two different module paths (%s and %s)", src.Path, src.Version, prev, mod.Path))
  2082  		}
  2083  	}
  2084  }
  2085  
  2086  // checkTidyCompatibility emits an error if any package would be loaded from a
  2087  // different module under rs than under ld.requirements.
  2088  func (pld *packageLoader) checkTidyCompatibility(ld *Loader, ctx context.Context, rs *Requirements, compatVersion string) {
  2089  	goVersion := rs.GoVersion(ld)
  2090  	suggestUpgrade := false
  2091  	suggestEFlag := false
  2092  	suggestFixes := func() {
  2093  		if pld.AllowErrors {
  2094  			// The user is explicitly ignoring these errors, so don't bother them with
  2095  			// other options.
  2096  			return
  2097  		}
  2098  
  2099  		// We print directly to os.Stderr because this information is advice about
  2100  		// how to fix errors, not actually an error itself.
  2101  		// (The actual errors should have been logged already.)
  2102  
  2103  		fmt.Fprintln(os.Stderr)
  2104  
  2105  		goFlag := ""
  2106  		if goVersion != ld.MainModules.GoVersion(ld) {
  2107  			goFlag = " -go=" + goVersion
  2108  		}
  2109  
  2110  		compatFlag := ""
  2111  		if compatVersion != gover.Prev(goVersion) {
  2112  			compatFlag = " -compat=" + compatVersion
  2113  		}
  2114  		if suggestUpgrade {
  2115  			eDesc := ""
  2116  			eFlag := ""
  2117  			if suggestEFlag {
  2118  				eDesc = ", leaving some packages unresolved"
  2119  				eFlag = " -e"
  2120  			}
  2121  			fmt.Fprintf(os.Stderr, "To upgrade to the versions selected by go %s%s:\n\tgo mod tidy%s -go=%s && go mod tidy%s -go=%s%s\n", compatVersion, eDesc, eFlag, compatVersion, eFlag, goVersion, compatFlag)
  2122  		} else if suggestEFlag {
  2123  			// If some packages are missing but no package is upgraded, then we
  2124  			// shouldn't suggest upgrading to the Go 1.16 versions explicitly — that
  2125  			// wouldn't actually fix anything for Go 1.16 users, and *would* break
  2126  			// something for Go 1.17 users.
  2127  			fmt.Fprintf(os.Stderr, "To proceed despite packages unresolved in go %s:\n\tgo mod tidy -e%s%s\n", compatVersion, goFlag, compatFlag)
  2128  		}
  2129  
  2130  		fmt.Fprintf(os.Stderr, "If reproducibility with go %s is not needed:\n\tgo mod tidy%s -compat=%s\n", compatVersion, goFlag, goVersion)
  2131  
  2132  		fmt.Fprintf(os.Stderr, "For information about 'go mod tidy' compatibility, see:\n\thttps://go.dev/ref/mod#graph-pruning\n")
  2133  	}
  2134  
  2135  	mg, err := rs.Graph(ld, ctx)
  2136  	if err != nil {
  2137  		pld.error(fmt.Errorf("error loading go %s module graph: %w", compatVersion, err))
  2138  		pld.switchIfErrors(ctx)
  2139  		suggestFixes()
  2140  		pld.exitIfErrors(ctx)
  2141  		return
  2142  	}
  2143  
  2144  	// Re-resolve packages in parallel.
  2145  	//
  2146  	// We re-resolve each package — rather than just checking versions — to ensure
  2147  	// that we have fetched module source code (and, importantly, checksums for
  2148  	// that source code) for all modules that are necessary to ensure that imports
  2149  	// are unambiguous. That also produces clearer diagnostics, since we can say
  2150  	// exactly what happened to the package if it became ambiguous or disappeared
  2151  	// entirely.
  2152  	//
  2153  	// We re-resolve the packages in parallel because this process involves disk
  2154  	// I/O to check for package sources, and because the process of checking for
  2155  	// ambiguous imports may require us to download additional modules that are
  2156  	// otherwise pruned out in Go 1.17 — we don't want to block progress on other
  2157  	// packages while we wait for a single new download.
  2158  	type mismatch struct {
  2159  		mod module.Version
  2160  		err error
  2161  	}
  2162  	mismatchMu := make(chan map[*loadPkg]mismatch, 1)
  2163  	mismatchMu <- map[*loadPkg]mismatch{}
  2164  	for _, pkg := range pld.pkgs {
  2165  		if pkg.mod.Path == "" && pkg.err == nil {
  2166  			// This package is from the standard library (which does not vary based on
  2167  			// the module graph).
  2168  			continue
  2169  		}
  2170  
  2171  		pkg := pkg
  2172  		pld.work.Add(func() {
  2173  			mod, _, _, _, err := importFromModules(ld, ctx, pkg.path, rs, mg, pld.skipImportModFiles)
  2174  			if mod != pkg.mod {
  2175  				mismatches := <-mismatchMu
  2176  				mismatches[pkg] = mismatch{mod: mod, err: err}
  2177  				mismatchMu <- mismatches
  2178  			}
  2179  		})
  2180  	}
  2181  	<-pld.work.Idle()
  2182  
  2183  	mismatches := <-mismatchMu
  2184  	if len(mismatches) == 0 {
  2185  		// Since we're running as part of 'go mod tidy', the roots of the module
  2186  		// graph should contain only modules that are relevant to some package in
  2187  		// the package graph. We checked every package in the package graph and
  2188  		// didn't find any mismatches, so that must mean that all of the roots of
  2189  		// the module graph are also consistent.
  2190  		//
  2191  		// If we're wrong, Go 1.16 in -mod=readonly mode will error out with
  2192  		// "updates to go.mod needed", which would be very confusing. So instead,
  2193  		// we'll double-check that our reasoning above actually holds — if it
  2194  		// doesn't, we'll emit an internal error and hopefully the user will report
  2195  		// it as a bug.
  2196  		for _, m := range pld.requirements.rootModules {
  2197  			if v := mg.Selected(m.Path); v != m.Version {
  2198  				fmt.Fprintln(os.Stderr)
  2199  				base.Fatalf("go: internal error: failed to diagnose selected-version mismatch for module %s: go %s selects %s, but go %s selects %s\n\tPlease report this at https://go.dev/issue.", m.Path, goVersion, m.Version, compatVersion, v)
  2200  			}
  2201  		}
  2202  		return
  2203  	}
  2204  
  2205  	// Iterate over the packages (instead of the mismatches map) to emit errors in
  2206  	// deterministic order.
  2207  	for _, pkg := range pld.pkgs {
  2208  		mismatch, ok := mismatches[pkg]
  2209  		if !ok {
  2210  			continue
  2211  		}
  2212  
  2213  		if pkg.isTest() {
  2214  			// We already did (or will) report an error for the package itself,
  2215  			// so don't report a duplicate (and more verbose) error for its test.
  2216  			if _, ok := mismatches[pkg.testOf]; !ok {
  2217  				base.Fatalf("go: internal error: mismatch recorded for test %s, but not its non-test package", pkg.path)
  2218  			}
  2219  			continue
  2220  		}
  2221  
  2222  		switch {
  2223  		case mismatch.err != nil:
  2224  			// pkg resolved successfully, but errors out using the requirements in rs.
  2225  			//
  2226  			// This could occur because the import is provided by a single root (and
  2227  			// is thus unambiguous in a main module with a pruned module graph) and
  2228  			// also one or more transitive dependencies (and is ambiguous with an
  2229  			// unpruned graph).
  2230  			//
  2231  			// It could also occur because some transitive dependency upgrades the
  2232  			// module that previously provided the package to a version that no
  2233  			// longer does, or to a version for which the module source code (but
  2234  			// not the go.mod file in isolation) has a checksum error.
  2235  			if _, ok := errors.AsType[*ImportMissingError](mismatch.err); ok {
  2236  				selected := module.Version{
  2237  					Path:    pkg.mod.Path,
  2238  					Version: mg.Selected(pkg.mod.Path),
  2239  				}
  2240  				pld.error(fmt.Errorf("%s loaded from %v,\n\tbut go %s would fail to locate it in %s", pkg.stackText(), pkg.mod, compatVersion, selected))
  2241  			} else {
  2242  				if _, ok := errors.AsType[*AmbiguousImportError](mismatch.err); ok {
  2243  					// TODO: Is this check needed?
  2244  				}
  2245  				pld.error(fmt.Errorf("%s loaded from %v,\n\tbut go %s would fail to locate it:\n\t%v", pkg.stackText(), pkg.mod, compatVersion, mismatch.err))
  2246  			}
  2247  
  2248  			suggestEFlag = true
  2249  
  2250  			// Even if we press ahead with the '-e' flag, the older version will
  2251  			// error out in readonly mode if it thinks the go.mod file contains
  2252  			// any *explicit* dependency that is not at its selected version,
  2253  			// even if that dependency is not relevant to any package being loaded.
  2254  			//
  2255  			// We check for that condition here. If all of the roots are consistent
  2256  			// the '-e' flag suffices, but otherwise we need to suggest an upgrade.
  2257  			if !suggestUpgrade {
  2258  				for _, m := range pld.requirements.rootModules {
  2259  					if v := mg.Selected(m.Path); v != m.Version {
  2260  						suggestUpgrade = true
  2261  						break
  2262  					}
  2263  				}
  2264  			}
  2265  
  2266  		case pkg.err != nil:
  2267  			// pkg had an error in with a pruned module graph (presumably suppressed
  2268  			// with the -e flag), but the error went away using an unpruned graph.
  2269  			//
  2270  			// This is possible, if, say, the import is unresolved in the pruned graph
  2271  			// (because the "latest" version of each candidate module either is
  2272  			// unavailable or does not contain the package), but is resolved in the
  2273  			// unpruned graph due to a newer-than-latest dependency that is normally
  2274  			// pruned out.
  2275  			//
  2276  			// This could also occur if the source code for the module providing the
  2277  			// package in the pruned graph has a checksum error, but the unpruned
  2278  			// graph upgrades that module to a version with a correct checksum.
  2279  			//
  2280  			// pkg.err should have already been logged elsewhere — along with a
  2281  			// stack trace — so log only the import path and non-error info here.
  2282  			suggestUpgrade = true
  2283  			pld.error(fmt.Errorf("%s failed to load from any module,\n\tbut go %s would load it from %v", pkg.path, compatVersion, mismatch.mod))
  2284  
  2285  		case pkg.mod != mismatch.mod:
  2286  			// The package is loaded successfully by both Go versions, but from a
  2287  			// different module in each. This could lead to subtle (and perhaps even
  2288  			// unnoticed!) variations in behavior between builds with different
  2289  			// toolchains.
  2290  			suggestUpgrade = true
  2291  			pld.error(fmt.Errorf("%s loaded from %v,\n\tbut go %s would select %v\n", pkg.stackText(), pkg.mod, compatVersion, mismatch.mod.Version))
  2292  
  2293  		default:
  2294  			base.Fatalf("go: internal error: mismatch recorded for package %s, but no differences found", pkg.path)
  2295  		}
  2296  	}
  2297  
  2298  	pld.switchIfErrors(ctx)
  2299  	suggestFixes()
  2300  	pld.exitIfErrors(ctx)
  2301  }
  2302  
  2303  // scanDir is like imports.ScanDir but elides known magic imports from the list,
  2304  // so that we do not go looking for packages that don't really exist.
  2305  //
  2306  // The standard magic import is "C", for cgo.
  2307  //
  2308  // The only other known magic imports are appengine and appengine/*.
  2309  // These are so old that they predate "go get" and did not use URL-like paths.
  2310  // Most code today now uses google.golang.org/appengine instead,
  2311  // but not all code has been so updated. When we mostly ignore build tags
  2312  // during "go vendor", we look into "// +build appengine" files and
  2313  // may see these legacy imports. We drop them so that the module
  2314  // search does not look for modules to try to satisfy them.
  2315  func scanDir(modroot string, dir string, tags map[string]bool) (imports_, testImports []string, err error) {
  2316  	if ip, mierr := modindex.GetPackage(modroot, dir); mierr == nil {
  2317  		imports_, testImports, err = ip.ScanDir(tags)
  2318  		goto Happy
  2319  	} else if !errors.Is(mierr, modindex.ErrNotIndexed) {
  2320  		return nil, nil, mierr
  2321  	}
  2322  
  2323  	imports_, testImports, err = imports.ScanDir(dir, tags)
  2324  Happy:
  2325  
  2326  	filter := func(x []string) []string {
  2327  		w := 0
  2328  		for _, pkg := range x {
  2329  			if pkg != "C" && pkg != "appengine" && !strings.HasPrefix(pkg, "appengine/") &&
  2330  				pkg != "appengine_internal" && !strings.HasPrefix(pkg, "appengine_internal/") {
  2331  				x[w] = pkg
  2332  				w++
  2333  			}
  2334  		}
  2335  		return x[:w]
  2336  	}
  2337  
  2338  	return filter(imports_), filter(testImports), err
  2339  }
  2340  
  2341  // buildStacks computes minimal import stacks for each package,
  2342  // for use in error messages. When it completes, packages that
  2343  // are part of the original root set have pkg.stack == nil,
  2344  // and other packages have pkg.stack pointing at the next
  2345  // package up the import stack in their minimal chain.
  2346  // As a side effect, buildStacks also constructs ld.pkgs,
  2347  // the list of all packages loaded.
  2348  func (pld *packageLoader) buildStacks() {
  2349  	if len(pld.pkgs) > 0 {
  2350  		panic("buildStacks")
  2351  	}
  2352  	for _, pkg := range pld.roots {
  2353  		pkg.stack = pkg // sentinel to avoid processing in next loop
  2354  		pld.pkgs = append(pld.pkgs, pkg)
  2355  	}
  2356  	for i := 0; i < len(pld.pkgs); i++ { // not range: appending to ld.pkgs in loop
  2357  		pkg := pld.pkgs[i]
  2358  		for _, next := range pkg.imports {
  2359  			if next.stack == nil {
  2360  				next.stack = pkg
  2361  				pld.pkgs = append(pld.pkgs, next)
  2362  			}
  2363  		}
  2364  		if next := pkg.test; next != nil && next.stack == nil {
  2365  			next.stack = pkg
  2366  			pld.pkgs = append(pld.pkgs, next)
  2367  		}
  2368  	}
  2369  	for _, pkg := range pld.roots {
  2370  		pkg.stack = nil
  2371  	}
  2372  }
  2373  
  2374  // stackText builds the import stack text to use when
  2375  // reporting an error in pkg. It has the general form
  2376  //
  2377  //	root imports
  2378  //		other imports
  2379  //		other2 tested by
  2380  //		other2.test imports
  2381  //		pkg
  2382  func (pkg *loadPkg) stackText() string {
  2383  	var stack []*loadPkg
  2384  	for p := pkg; p != nil; p = p.stack {
  2385  		stack = append(stack, p)
  2386  	}
  2387  
  2388  	var buf strings.Builder
  2389  	for i := len(stack) - 1; i >= 0; i-- {
  2390  		p := stack[i]
  2391  		fmt.Fprint(&buf, p.path)
  2392  		if p.testOf != nil {
  2393  			fmt.Fprint(&buf, ".test")
  2394  		}
  2395  		if i > 0 {
  2396  			if stack[i-1].testOf == p {
  2397  				fmt.Fprint(&buf, " tested by\n\t")
  2398  			} else {
  2399  				fmt.Fprint(&buf, " imports\n\t")
  2400  			}
  2401  		}
  2402  	}
  2403  	return buf.String()
  2404  }
  2405  
  2406  // why returns the text to use in "go mod why" output about the given package.
  2407  // It is less ornate than the stackText but contains the same information.
  2408  func (pkg *loadPkg) why() string {
  2409  	var buf strings.Builder
  2410  	var stack []*loadPkg
  2411  	for p := pkg; p != nil; p = p.stack {
  2412  		stack = append(stack, p)
  2413  	}
  2414  
  2415  	for i := len(stack) - 1; i >= 0; i-- {
  2416  		p := stack[i]
  2417  		if p.testOf != nil {
  2418  			fmt.Fprintf(&buf, "%s.test\n", p.testOf.path)
  2419  		} else {
  2420  			fmt.Fprintf(&buf, "%s\n", p.path)
  2421  		}
  2422  	}
  2423  	return buf.String()
  2424  }
  2425  
  2426  // Why returns the "go mod why" output stanza for the given package,
  2427  // without the leading # comment.
  2428  // The package graph must have been loaded already, usually by LoadPackages.
  2429  // If there is no reason for the package to be in the current build,
  2430  // Why returns an empty string.
  2431  func (ld *Loader) Why(path string) string {
  2432  	pkg, ok := ld.pkgLoader.pkgCache.Get(path)
  2433  	if !ok {
  2434  		return ""
  2435  	}
  2436  	return pkg.why()
  2437  }
  2438  
  2439  // WhyDepth returns the number of steps in the Why listing.
  2440  // If there is no reason for the package to be in the current build,
  2441  // WhyDepth returns 0.
  2442  func (ld *Loader) WhyDepth(path string) int {
  2443  	n := 0
  2444  	pkg, _ := ld.pkgLoader.pkgCache.Get(path)
  2445  	for p := pkg; p != nil; p = p.stack {
  2446  		n++
  2447  	}
  2448  	return n
  2449  }
  2450  

View as plain text