github.com/octohelm/cuemod@v0.9.4/internal/cmd/go/internals/modload/load.go (about)

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