Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/go/internal/modload

     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 we implement lazy loading, we will record the modules providing packages
    44  // in "all" even when we are only loading individual packages, so we set the
    45  // pkgInAll flag regardless of the whether the "all" pattern is a root.
    46  // (This is necessary to maintain the “import invariant” described in
    47  // https://golang.org/design/36460-lazy-module-loading.)
    48  //
    49  // Because "go mod vendor" prunes out the tests of vendored packages, the
    50  // behavior of the "all" pattern with -mod=vendor in Go 1.11–1.15 is the same
    51  // as the "all" pattern (regardless of the -mod flag) in 1.16+.
    52  // The allClosesOverTests parameter to the loader indicates whether the "all"
    53  // pattern should close over tests (as in Go 1.11–1.15) or stop at only those
    54  // packages transitively imported by the packages and tests in the main module
    55  // ("all" in Go 1.16+ and "go mod vendor" in Go 1.11+).
    56  //
    57  // Note that it is possible for a loaded package NOT to be in "all" even when we
    58  // are loading the "all" pattern. For example, packages that are transitive
    59  // dependencies of other roots named on the command line must be loaded, but are
    60  // not in "all". (The mod_notall test illustrates this behavior.)
    61  // Similarly, if the LoadTests flag is set but the "all" pattern does not close
    62  // over test dependencies, then when we load the test of a package that is in
    63  // "all" but outside the main module, the dependencies of that test will not
    64  // necessarily themselves be in "all". (That configuration does not arise in Go
    65  // 1.11–1.15, but it will be possible in Go 1.16+.)
    66  //
    67  // Loading proceeds from the roots, using a parallel work-queue with a limit on
    68  // the amount of active work (to avoid saturating disks, CPU cores, and/or
    69  // network connections). Each package is added to the queue the first time it is
    70  // imported by another package. When we have finished identifying the imports of
    71  // a package, we add the test for that package if it is needed. A test may be
    72  // needed if:
    73  // 	- the package matches a root pattern and tests of the roots were requested, or
    74  // 	- the package is in the main module and the "all" pattern is requested
    75  // 	  (because the "all" pattern includes the dependencies of tests in the main
    76  // 	  module), or
    77  // 	- the package is in "all" and the definition of "all" we are using includes
    78  // 	  dependencies of tests (as is the case in Go ≤1.15).
    79  //
    80  // After all available packages have been loaded, we examine the results to
    81  // identify any requested or imported packages that are still missing, and if
    82  // so, which modules we could add to the module graph in order to make the
    83  // missing packages available. We add those to the module graph and iterate,
    84  // until either all packages resolve successfully or we cannot identify any
    85  // module that would resolve any remaining missing package.
    86  //
    87  // If the main module is “tidy” (that is, if "go mod tidy" is a no-op for it)
    88  // and all requested packages are in "all", then loading completes in a single
    89  // iteration.
    90  // TODO(bcmills): We should also be able to load in a single iteration if the
    91  // requested packages all come from modules that are themselves tidy, regardless
    92  // of whether those packages are in "all". Today, that requires two iterations
    93  // if those packages are not found in existing dependencies of the main module.
    94  
    95  import (
    96  	"bytes"
    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  	"cmd/go/internal/base"
   114  	"cmd/go/internal/cfg"
   115  	"cmd/go/internal/fsys"
   116  	"cmd/go/internal/imports"
   117  	"cmd/go/internal/modfetch"
   118  	"cmd/go/internal/mvs"
   119  	"cmd/go/internal/par"
   120  	"cmd/go/internal/search"
   121  	"cmd/go/internal/str"
   122  
   123  	"golang.org/x/mod/module"
   124  )
   125  
   126  // loaded is the most recently-used package loader.
   127  // It holds details about individual packages.
   128  var loaded *loader
   129  
   130  // PackageOpts control the behavior of the LoadPackages function.
   131  type PackageOpts struct {
   132  	// Tags are the build tags in effect (as interpreted by the
   133  	// cmd/go/internal/imports package).
   134  	// If nil, treated as equivalent to imports.Tags().
   135  	Tags map[string]bool
   136  
   137  	// ResolveMissingImports indicates that we should attempt to add module
   138  	// dependencies as needed to resolve imports of packages that are not found.
   139  	//
   140  	// For commands that support the -mod flag, resolving imports may still fail
   141  	// if the flag is set to "readonly" (the default) or "vendor".
   142  	ResolveMissingImports bool
   143  
   144  	// AllowPackage, if non-nil, is called after identifying the module providing
   145  	// each package. If AllowPackage returns a non-nil error, that error is set
   146  	// for the package, and the imports and test of that package will not be
   147  	// loaded.
   148  	//
   149  	// AllowPackage may be invoked concurrently by multiple goroutines,
   150  	// and may be invoked multiple times for a given package path.
   151  	AllowPackage func(ctx context.Context, path string, mod module.Version) error
   152  
   153  	// LoadTests loads the test dependencies of each package matching a requested
   154  	// pattern. If ResolveMissingImports is also true, test dependencies will be
   155  	// resolved if missing.
   156  	LoadTests bool
   157  
   158  	// UseVendorAll causes the "all" package pattern to be interpreted as if
   159  	// running "go mod vendor" (or building with "-mod=vendor").
   160  	//
   161  	// This is a no-op for modules that declare 'go 1.16' or higher, for which this
   162  	// is the default (and only) interpretation of the "all" pattern in module mode.
   163  	UseVendorAll bool
   164  
   165  	// AllowErrors indicates that LoadPackages should not terminate the process if
   166  	// an error occurs.
   167  	AllowErrors bool
   168  
   169  	// SilenceErrors indicates that LoadPackages should not print errors
   170  	// that occur while loading packages. SilenceErrors implies AllowErrors.
   171  	SilenceErrors bool
   172  
   173  	// SilenceMissingStdImports indicates that LoadPackages should not print
   174  	// errors or terminate the process if an imported package is missing, and the
   175  	// import path looks like it might be in the standard library (perhaps in a
   176  	// future version).
   177  	SilenceMissingStdImports bool
   178  
   179  	// SilenceUnmatchedWarnings suppresses the warnings normally emitted for
   180  	// patterns that did not match any packages.
   181  	SilenceUnmatchedWarnings bool
   182  }
   183  
   184  // LoadPackages identifies the set of packages matching the given patterns and
   185  // loads the packages in the import graph rooted at that set.
   186  func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (matches []*search.Match, loadedPackages []string) {
   187  	LoadModFile(ctx)
   188  	if opts.Tags == nil {
   189  		opts.Tags = imports.Tags()
   190  	}
   191  
   192  	patterns = search.CleanPatterns(patterns)
   193  	matches = make([]*search.Match, 0, len(patterns))
   194  	allPatternIsRoot := false
   195  	for _, pattern := range patterns {
   196  		matches = append(matches, search.NewMatch(pattern))
   197  		if pattern == "all" {
   198  			allPatternIsRoot = true
   199  		}
   200  	}
   201  
   202  	updateMatches := func(ld *loader) {
   203  		for _, m := range matches {
   204  			switch {
   205  			case m.IsLocal():
   206  				// Evaluate list of file system directories on first iteration.
   207  				if m.Dirs == nil {
   208  					matchLocalDirs(m)
   209  				}
   210  
   211  				// Make a copy of the directory list and translate to import paths.
   212  				// Note that whether a directory corresponds to an import path
   213  				// changes as the build list is updated, and a directory can change
   214  				// from not being in the build list to being in it and back as
   215  				// the exact version of a particular module increases during
   216  				// the loader iterations.
   217  				m.Pkgs = m.Pkgs[:0]
   218  				for _, dir := range m.Dirs {
   219  					pkg, err := resolveLocalPackage(dir)
   220  					if err != nil {
   221  						if !m.IsLiteral() && (err == errPkgIsBuiltin || err == errPkgIsGorootSrc) {
   222  							continue // Don't include "builtin" or GOROOT/src in wildcard patterns.
   223  						}
   224  
   225  						// If we're outside of a module, ensure that the failure mode
   226  						// indicates that.
   227  						ModRoot()
   228  
   229  						if ld != nil {
   230  							m.AddError(err)
   231  						}
   232  						continue
   233  					}
   234  					m.Pkgs = append(m.Pkgs, pkg)
   235  				}
   236  
   237  			case m.IsLiteral():
   238  				m.Pkgs = []string{m.Pattern()}
   239  
   240  			case strings.Contains(m.Pattern(), "..."):
   241  				m.Errs = m.Errs[:0]
   242  				matchPackages(ctx, m, opts.Tags, includeStd, buildList)
   243  
   244  			case m.Pattern() == "all":
   245  				if ld == nil {
   246  					// The initial roots are the packages in the main module.
   247  					// loadFromRoots will expand that to "all".
   248  					m.Errs = m.Errs[:0]
   249  					matchPackages(ctx, m, opts.Tags, omitStd, []module.Version{Target})
   250  				} else {
   251  					// Starting with the packages in the main module,
   252  					// enumerate the full list of "all".
   253  					m.Pkgs = ld.computePatternAll()
   254  				}
   255  
   256  			case m.Pattern() == "std" || m.Pattern() == "cmd":
   257  				if m.Pkgs == nil {
   258  					m.MatchPackages() // Locate the packages within GOROOT/src.
   259  				}
   260  
   261  			default:
   262  				panic(fmt.Sprintf("internal error: modload missing case for pattern %s", m.Pattern()))
   263  			}
   264  		}
   265  	}
   266  
   267  	loaded = loadFromRoots(loaderParams{
   268  		PackageOpts: opts,
   269  
   270  		allClosesOverTests: index.allPatternClosesOverTests() && !opts.UseVendorAll,
   271  		allPatternIsRoot:   allPatternIsRoot,
   272  
   273  		listRoots: func() (roots []string) {
   274  			updateMatches(nil)
   275  			for _, m := range matches {
   276  				roots = append(roots, m.Pkgs...)
   277  			}
   278  			return roots
   279  		},
   280  	})
   281  
   282  	// One last pass to finalize wildcards.
   283  	updateMatches(loaded)
   284  
   285  	// Report errors, if any.
   286  	checkMultiplePaths()
   287  	for _, pkg := range loaded.pkgs {
   288  		if pkg.err != nil {
   289  			if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
   290  				if importer := pkg.stack; importer != nil {
   291  					sumErr.importer = importer.path
   292  					sumErr.importerVersion = importer.mod.Version
   293  					sumErr.importerIsTest = importer.testOf != nil
   294  				}
   295  			}
   296  			silence := opts.SilenceErrors
   297  			if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) &&
   298  				stdErr.isStd && opts.SilenceMissingStdImports {
   299  				silence = true
   300  			}
   301  
   302  			if !silence {
   303  				if opts.AllowErrors {
   304  					fmt.Fprintf(os.Stderr, "%s: %v\n", pkg.stackText(), pkg.err)
   305  				} else {
   306  					base.Errorf("%s: %v", pkg.stackText(), pkg.err)
   307  				}
   308  			}
   309  		}
   310  		if !pkg.isTest() {
   311  			loadedPackages = append(loadedPackages, pkg.path)
   312  		}
   313  	}
   314  	if !opts.SilenceErrors {
   315  		// Also list errors in matching patterns (such as directory permission
   316  		// errors for wildcard patterns).
   317  		for _, match := range matches {
   318  			for _, err := range match.Errs {
   319  				if opts.AllowErrors {
   320  					fmt.Fprintf(os.Stderr, "%v\n", err)
   321  				} else {
   322  					base.Errorf("%v", err)
   323  				}
   324  			}
   325  		}
   326  	}
   327  	base.ExitIfErrors()
   328  
   329  	if !opts.SilenceUnmatchedWarnings {
   330  		search.WarnUnmatched(matches)
   331  	}
   332  
   333  	// Success! Update go.mod (if needed) and return the results.
   334  	WriteGoMod()
   335  	sort.Strings(loadedPackages)
   336  	return matches, loadedPackages
   337  }
   338  
   339  // matchLocalDirs is like m.MatchDirs, but tries to avoid scanning directories
   340  // outside of the standard library and active modules.
   341  func matchLocalDirs(m *search.Match) {
   342  	if !m.IsLocal() {
   343  		panic(fmt.Sprintf("internal error: resolveLocalDirs on non-local pattern %s", m.Pattern()))
   344  	}
   345  
   346  	if i := strings.Index(m.Pattern(), "..."); i >= 0 {
   347  		// The pattern is local, but it is a wildcard. Its packages will
   348  		// only resolve to paths if they are inside of the standard
   349  		// library, the main module, or some dependency of the main
   350  		// module. Verify that before we walk the filesystem: a filesystem
   351  		// walk in a directory like /var or /etc can be very expensive!
   352  		dir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3]))
   353  		absDir := dir
   354  		if !filepath.IsAbs(dir) {
   355  			absDir = filepath.Join(base.Cwd, dir)
   356  		}
   357  		if search.InDir(absDir, cfg.GOROOTsrc) == "" && search.InDir(absDir, ModRoot()) == "" && pathInModuleCache(absDir) == "" {
   358  			m.Dirs = []string{}
   359  			m.AddError(fmt.Errorf("directory prefix %s outside available modules", base.ShortPath(absDir)))
   360  			return
   361  		}
   362  	}
   363  
   364  	m.MatchDirs()
   365  }
   366  
   367  // resolveLocalPackage resolves a filesystem path to a package path.
   368  func resolveLocalPackage(dir string) (string, error) {
   369  	var absDir string
   370  	if filepath.IsAbs(dir) {
   371  		absDir = filepath.Clean(dir)
   372  	} else {
   373  		absDir = filepath.Join(base.Cwd, dir)
   374  	}
   375  
   376  	bp, err := cfg.BuildContext.ImportDir(absDir, 0)
   377  	if err != nil && (bp == nil || len(bp.IgnoredGoFiles) == 0) {
   378  		// golang.org/issue/32917: We should resolve a relative path to a
   379  		// package path only if the relative path actually contains the code
   380  		// for that package.
   381  		//
   382  		// If the named directory does not exist or contains no Go files,
   383  		// the package does not exist.
   384  		// Other errors may affect package loading, but not resolution.
   385  		if _, err := fsys.Stat(absDir); err != nil {
   386  			if os.IsNotExist(err) {
   387  				// Canonicalize OS-specific errors to errDirectoryNotFound so that error
   388  				// messages will be easier for users to search for.
   389  				return "", &fs.PathError{Op: "stat", Path: absDir, Err: errDirectoryNotFound}
   390  			}
   391  			return "", err
   392  		}
   393  		if _, noGo := err.(*build.NoGoError); noGo {
   394  			// A directory that does not contain any Go source files — even ignored
   395  			// ones! — is not a Go package, and we can't resolve it to a package
   396  			// path because that path could plausibly be provided by some other
   397  			// module.
   398  			//
   399  			// Any other error indicates that the package “exists” (at least in the
   400  			// sense that it cannot exist in any other module), but has some other
   401  			// problem (such as a syntax error).
   402  			return "", err
   403  		}
   404  	}
   405  
   406  	if modRoot != "" && absDir == modRoot {
   407  		if absDir == cfg.GOROOTsrc {
   408  			return "", errPkgIsGorootSrc
   409  		}
   410  		return targetPrefix, nil
   411  	}
   412  
   413  	// Note: The checks for @ here are just to avoid misinterpreting
   414  	// the module cache directories (formerly GOPATH/src/mod/foo@v1.5.2/bar).
   415  	// It's not strictly necessary but helpful to keep the checks.
   416  	if modRoot != "" && strings.HasPrefix(absDir, modRoot+string(filepath.Separator)) && !strings.Contains(absDir[len(modRoot):], "@") {
   417  		suffix := filepath.ToSlash(absDir[len(modRoot):])
   418  		if strings.HasPrefix(suffix, "/vendor/") {
   419  			if cfg.BuildMod != "vendor" {
   420  				return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir)
   421  			}
   422  
   423  			readVendorList()
   424  			pkg := strings.TrimPrefix(suffix, "/vendor/")
   425  			if _, ok := vendorPkgModule[pkg]; !ok {
   426  				return "", fmt.Errorf("directory %s is not a package listed in vendor/modules.txt", absDir)
   427  			}
   428  			return pkg, nil
   429  		}
   430  
   431  		if targetPrefix == "" {
   432  			pkg := strings.TrimPrefix(suffix, "/")
   433  			if pkg == "builtin" {
   434  				// "builtin" is a pseudo-package with a real source file.
   435  				// It's not included in "std", so it shouldn't resolve from "."
   436  				// within module "std" either.
   437  				return "", errPkgIsBuiltin
   438  			}
   439  			return pkg, nil
   440  		}
   441  
   442  		pkg := targetPrefix + suffix
   443  		if _, ok, err := dirInModule(pkg, targetPrefix, modRoot, true); err != nil {
   444  			return "", err
   445  		} else if !ok {
   446  			return "", &PackageNotInModuleError{Mod: Target, Pattern: pkg}
   447  		}
   448  		return pkg, nil
   449  	}
   450  
   451  	if sub := search.InDir(absDir, cfg.GOROOTsrc); sub != "" && sub != "." && !strings.Contains(sub, "@") {
   452  		pkg := filepath.ToSlash(sub)
   453  		if pkg == "builtin" {
   454  			return "", errPkgIsBuiltin
   455  		}
   456  		return pkg, nil
   457  	}
   458  
   459  	pkg := pathInModuleCache(absDir)
   460  	if pkg == "" {
   461  		return "", fmt.Errorf("directory %s outside available modules", base.ShortPath(absDir))
   462  	}
   463  	return pkg, nil
   464  }
   465  
   466  var (
   467  	errDirectoryNotFound = errors.New("directory not found")
   468  	errPkgIsGorootSrc    = errors.New("GOROOT/src is not an importable package")
   469  	errPkgIsBuiltin      = errors.New(`"builtin" is a pseudo-package, not an importable package`)
   470  )
   471  
   472  // pathInModuleCache returns the import path of the directory dir,
   473  // if dir is in the module cache copy of a module in our build list.
   474  func pathInModuleCache(dir string) string {
   475  	tryMod := func(m module.Version) (string, bool) {
   476  		var root string
   477  		var err error
   478  		if repl := Replacement(m); repl.Path != "" && repl.Version == "" {
   479  			root = repl.Path
   480  			if !filepath.IsAbs(root) {
   481  				root = filepath.Join(ModRoot(), root)
   482  			}
   483  		} else if repl.Path != "" {
   484  			root, err = modfetch.DownloadDir(repl)
   485  		} else {
   486  			root, err = modfetch.DownloadDir(m)
   487  		}
   488  		if err != nil {
   489  			return "", false
   490  		}
   491  
   492  		sub := search.InDir(dir, root)
   493  		if sub == "" {
   494  			return "", false
   495  		}
   496  		sub = filepath.ToSlash(sub)
   497  		if strings.Contains(sub, "/vendor/") || strings.HasPrefix(sub, "vendor/") || strings.Contains(sub, "@") {
   498  			return "", false
   499  		}
   500  
   501  		return path.Join(m.Path, filepath.ToSlash(sub)), true
   502  	}
   503  
   504  	for _, m := range buildList[1:] {
   505  		if importPath, ok := tryMod(m); ok {
   506  			// checkMultiplePaths ensures that a module can be used for at most one
   507  			// requirement, so this must be it.
   508  			return importPath
   509  		}
   510  	}
   511  	return ""
   512  }
   513  
   514  // ImportFromFiles adds modules to the build list as needed
   515  // to satisfy the imports in the named Go source files.
   516  func ImportFromFiles(ctx context.Context, gofiles []string) {
   517  	LoadModFile(ctx)
   518  
   519  	tags := imports.Tags()
   520  	imports, testImports, err := imports.ScanFiles(gofiles, tags)
   521  	if err != nil {
   522  		base.Fatalf("go: %v", err)
   523  	}
   524  
   525  	loaded = loadFromRoots(loaderParams{
   526  		PackageOpts: PackageOpts{
   527  			Tags:                  tags,
   528  			ResolveMissingImports: true,
   529  		},
   530  		allClosesOverTests: index.allPatternClosesOverTests(),
   531  		listRoots: func() (roots []string) {
   532  			roots = append(roots, imports...)
   533  			roots = append(roots, testImports...)
   534  			return roots
   535  		},
   536  	})
   537  	WriteGoMod()
   538  }
   539  
   540  // DirImportPath returns the effective import path for dir,
   541  // provided it is within the main module, or else returns ".".
   542  func DirImportPath(dir string) string {
   543  	if !HasModRoot() {
   544  		return "."
   545  	}
   546  	LoadModFile(context.TODO())
   547  
   548  	if !filepath.IsAbs(dir) {
   549  		dir = filepath.Join(base.Cwd, dir)
   550  	} else {
   551  		dir = filepath.Clean(dir)
   552  	}
   553  
   554  	if dir == modRoot {
   555  		return targetPrefix
   556  	}
   557  	if strings.HasPrefix(dir, modRoot+string(filepath.Separator)) {
   558  		suffix := filepath.ToSlash(dir[len(modRoot):])
   559  		if strings.HasPrefix(suffix, "/vendor/") {
   560  			return strings.TrimPrefix(suffix, "/vendor/")
   561  		}
   562  		return targetPrefix + suffix
   563  	}
   564  	return "."
   565  }
   566  
   567  // TargetPackages returns the list of packages in the target (top-level) module
   568  // matching pattern, which may be relative to the working directory, under all
   569  // build tag settings.
   570  func TargetPackages(ctx context.Context, pattern string) *search.Match {
   571  	// TargetPackages is relative to the main module, so ensure that the main
   572  	// module is a thing that can contain packages.
   573  	LoadModFile(ctx)
   574  	ModRoot()
   575  
   576  	m := search.NewMatch(pattern)
   577  	matchPackages(ctx, m, imports.AnyTags(), omitStd, []module.Version{Target})
   578  	return m
   579  }
   580  
   581  // ImportMap returns the actual package import path
   582  // for an import path found in source code.
   583  // If the given import path does not appear in the source code
   584  // for the packages that have been loaded, ImportMap returns the empty string.
   585  func ImportMap(path string) string {
   586  	pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
   587  	if !ok {
   588  		return ""
   589  	}
   590  	return pkg.path
   591  }
   592  
   593  // PackageDir returns the directory containing the source code
   594  // for the package named by the import path.
   595  func PackageDir(path string) string {
   596  	pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
   597  	if !ok {
   598  		return ""
   599  	}
   600  	return pkg.dir
   601  }
   602  
   603  // PackageModule returns the module providing the package named by the import path.
   604  func PackageModule(path string) module.Version {
   605  	pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
   606  	if !ok {
   607  		return module.Version{}
   608  	}
   609  	return pkg.mod
   610  }
   611  
   612  // PackageImports returns the imports for the package named by the import path.
   613  // Test imports will be returned as well if tests were loaded for the package
   614  // (i.e., if "all" was loaded or if LoadTests was set and the path was matched
   615  // by a command line argument). PackageImports will return nil for
   616  // unknown package paths.
   617  func PackageImports(path string) (imports, testImports []string) {
   618  	pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
   619  	if !ok {
   620  		return nil, nil
   621  	}
   622  	imports = make([]string, len(pkg.imports))
   623  	for i, p := range pkg.imports {
   624  		imports[i] = p.path
   625  	}
   626  	if pkg.test != nil {
   627  		testImports = make([]string, len(pkg.test.imports))
   628  		for i, p := range pkg.test.imports {
   629  			testImports[i] = p.path
   630  		}
   631  	}
   632  	return imports, testImports
   633  }
   634  
   635  // Lookup returns the source directory, import path, and any loading error for
   636  // the package at path as imported from the package in parentDir.
   637  // Lookup requires that one of the Load functions in this package has already
   638  // been called.
   639  func Lookup(parentPath string, parentIsStd bool, path string) (dir, realPath string, err error) {
   640  	if path == "" {
   641  		panic("Lookup called with empty package path")
   642  	}
   643  
   644  	if parentIsStd {
   645  		path = loaded.stdVendor(parentPath, path)
   646  	}
   647  	pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
   648  	if !ok {
   649  		// The loader should have found all the relevant paths.
   650  		// There are a few exceptions, though:
   651  		//	- during go list without -test, the p.Resolve calls to process p.TestImports and p.XTestImports
   652  		//	  end up here to canonicalize the import paths.
   653  		//	- during any load, non-loaded packages like "unsafe" end up here.
   654  		//	- during any load, build-injected dependencies like "runtime/cgo" end up here.
   655  		//	- because we ignore appengine/* in the module loader,
   656  		//	  the dependencies of any actual appengine/* library end up here.
   657  		dir := findStandardImportPath(path)
   658  		if dir != "" {
   659  			return dir, path, nil
   660  		}
   661  		return "", "", errMissing
   662  	}
   663  	return pkg.dir, pkg.path, pkg.err
   664  }
   665  
   666  // A loader manages the process of loading information about
   667  // the required packages for a particular build,
   668  // checking that the packages are available in the module set,
   669  // and updating the module set if needed.
   670  type loader struct {
   671  	loaderParams
   672  
   673  	work *par.Queue
   674  
   675  	// reset on each iteration
   676  	roots    []*loadPkg
   677  	pkgCache *par.Cache // package path (string) → *loadPkg
   678  	pkgs     []*loadPkg // transitive closure of loaded packages and tests; populated in buildStacks
   679  
   680  	// computed at end of iterations
   681  	direct map[string]bool // imported directly by main module
   682  }
   683  
   684  // loaderParams configure the packages loaded by, and the properties reported
   685  // by, a loader instance.
   686  type loaderParams struct {
   687  	PackageOpts
   688  
   689  	allClosesOverTests bool // Does the "all" pattern include the transitive closure of tests of packages in "all"?
   690  	allPatternIsRoot   bool // Is the "all" pattern an additional root?
   691  
   692  	listRoots func() []string
   693  }
   694  
   695  func (ld *loader) reset() {
   696  	select {
   697  	case <-ld.work.Idle():
   698  	default:
   699  		panic("loader.reset when not idle")
   700  	}
   701  
   702  	ld.roots = nil
   703  	ld.pkgCache = new(par.Cache)
   704  	ld.pkgs = nil
   705  }
   706  
   707  // A loadPkg records information about a single loaded package.
   708  type loadPkg struct {
   709  	// Populated at construction time:
   710  	path   string // import path
   711  	testOf *loadPkg
   712  
   713  	// Populated at construction time and updated by (*loader).applyPkgFlags:
   714  	flags atomicLoadPkgFlags
   715  
   716  	// Populated by (*loader).load:
   717  	mod         module.Version // module providing package
   718  	dir         string         // directory containing source code
   719  	err         error          // error loading package
   720  	imports     []*loadPkg     // packages imported by this one
   721  	testImports []string       // test-only imports, saved for use by pkg.test.
   722  	inStd       bool
   723  
   724  	// Populated by (*loader).pkgTest:
   725  	testOnce sync.Once
   726  	test     *loadPkg
   727  
   728  	// Populated by postprocessing in (*loader).buildStacks:
   729  	stack *loadPkg // package importing this one in minimal import stack for this pkg
   730  }
   731  
   732  // loadPkgFlags is a set of flags tracking metadata about a package.
   733  type loadPkgFlags int8
   734  
   735  const (
   736  	// pkgInAll indicates that the package is in the "all" package pattern,
   737  	// regardless of whether we are loading the "all" package pattern.
   738  	//
   739  	// When the pkgInAll flag and pkgImportsLoaded flags are both set, the caller
   740  	// who set the last of those flags must propagate the pkgInAll marking to all
   741  	// of the imports of the marked package.
   742  	//
   743  	// A test is marked with pkgInAll if that test would promote the packages it
   744  	// imports to be in "all" (such as when the test is itself within the main
   745  	// module, or when ld.allClosesOverTests is true).
   746  	pkgInAll loadPkgFlags = 1 << iota
   747  
   748  	// pkgIsRoot indicates that the package matches one of the root package
   749  	// patterns requested by the caller.
   750  	//
   751  	// If LoadTests is set, then when pkgIsRoot and pkgImportsLoaded are both set,
   752  	// the caller who set the last of those flags must populate a test for the
   753  	// package (in the pkg.test field).
   754  	//
   755  	// If the "all" pattern is included as a root, then non-test packages in "all"
   756  	// are also roots (and must be marked pkgIsRoot).
   757  	pkgIsRoot
   758  
   759  	// pkgImportsLoaded indicates that the imports and testImports fields of a
   760  	// loadPkg have been populated.
   761  	pkgImportsLoaded
   762  )
   763  
   764  // has reports whether all of the flags in cond are set in f.
   765  func (f loadPkgFlags) has(cond loadPkgFlags) bool {
   766  	return f&cond == cond
   767  }
   768  
   769  // An atomicLoadPkgFlags stores a loadPkgFlags for which individual flags can be
   770  // added atomically.
   771  type atomicLoadPkgFlags struct {
   772  	bits int32
   773  }
   774  
   775  // update sets the given flags in af (in addition to any flags already set).
   776  //
   777  // update returns the previous flag state so that the caller may determine which
   778  // flags were newly-set.
   779  func (af *atomicLoadPkgFlags) update(flags loadPkgFlags) (old loadPkgFlags) {
   780  	for {
   781  		old := atomic.LoadInt32(&af.bits)
   782  		new := old | int32(flags)
   783  		if new == old || atomic.CompareAndSwapInt32(&af.bits, old, new) {
   784  			return loadPkgFlags(old)
   785  		}
   786  	}
   787  }
   788  
   789  // has reports whether all of the flags in cond are set in af.
   790  func (af *atomicLoadPkgFlags) has(cond loadPkgFlags) bool {
   791  	return loadPkgFlags(atomic.LoadInt32(&af.bits))&cond == cond
   792  }
   793  
   794  // isTest reports whether pkg is a test of another package.
   795  func (pkg *loadPkg) isTest() bool {
   796  	return pkg.testOf != nil
   797  }
   798  
   799  var errMissing = errors.New("cannot find package")
   800  
   801  // loadFromRoots attempts to load the build graph needed to process a set of
   802  // root packages and their dependencies.
   803  //
   804  // The set of root packages is returned by the params.listRoots function, and
   805  // expanded to the full set of packages by tracing imports (and possibly tests)
   806  // as needed.
   807  func loadFromRoots(params loaderParams) *loader {
   808  	ld := &loader{
   809  		loaderParams: params,
   810  		work:         par.NewQueue(runtime.GOMAXPROCS(0)),
   811  	}
   812  
   813  	var err error
   814  	reqs := &mvsReqs{buildList: buildList}
   815  	buildList, err = mvs.BuildList(Target, reqs)
   816  	if err != nil {
   817  		base.Fatalf("go: %v", err)
   818  	}
   819  
   820  	addedModuleFor := make(map[string]bool)
   821  	for {
   822  		ld.reset()
   823  
   824  		// Load the root packages and their imports.
   825  		// Note: the returned roots can change on each iteration,
   826  		// since the expansion of package patterns depends on the
   827  		// build list we're using.
   828  		inRoots := map[*loadPkg]bool{}
   829  		for _, path := range ld.listRoots() {
   830  			root := ld.pkg(path, pkgIsRoot)
   831  			if !inRoots[root] {
   832  				ld.roots = append(ld.roots, root)
   833  				inRoots[root] = true
   834  			}
   835  		}
   836  
   837  		// ld.pkg adds imported packages to the work queue and calls applyPkgFlags,
   838  		// which adds tests (and test dependencies) as needed.
   839  		//
   840  		// When all of the work in the queue has completed, we'll know that the
   841  		// transitive closure of dependencies has been loaded.
   842  		<-ld.work.Idle()
   843  
   844  		ld.buildStacks()
   845  
   846  		if !ld.ResolveMissingImports || (!HasModRoot() && !allowMissingModuleImports) {
   847  			// We've loaded as much as we can without resolving missing imports.
   848  			break
   849  		}
   850  		modAddedBy := ld.resolveMissingImports(addedModuleFor)
   851  		if len(modAddedBy) == 0 {
   852  			break
   853  		}
   854  
   855  		// Recompute buildList with all our additions.
   856  		reqs = &mvsReqs{buildList: buildList}
   857  		buildList, err = mvs.BuildList(Target, reqs)
   858  		if err != nil {
   859  			// If an error was found in a newly added module, report the package
   860  			// import stack instead of the module requirement stack. Packages
   861  			// are more descriptive.
   862  			if err, ok := err.(*mvs.BuildListError); ok {
   863  				if pkg := modAddedBy[err.Module()]; pkg != nil {
   864  					base.Fatalf("go: %s: %v", pkg.stackText(), err.Err)
   865  				}
   866  			}
   867  			base.Fatalf("go: %v", err)
   868  		}
   869  	}
   870  	base.ExitIfErrors()
   871  
   872  	// Compute directly referenced dependency modules.
   873  	ld.direct = make(map[string]bool)
   874  	for _, pkg := range ld.pkgs {
   875  		if pkg.mod == Target {
   876  			for _, dep := range pkg.imports {
   877  				if dep.mod.Path != "" && dep.mod.Path != Target.Path && index != nil {
   878  					_, explicit := index.require[dep.mod]
   879  					if allowWriteGoMod && cfg.BuildMod == "readonly" && !explicit {
   880  						// TODO(#40775): attach error to package instead of using
   881  						// base.Errorf. Ideally, 'go list' should not fail because of this,
   882  						// but today, LoadPackages calls WriteGoMod unconditionally, which
   883  						// would fail with a less clear message.
   884  						base.Errorf("go: %[1]s: package %[2]s imported from implicitly required module; to add missing requirements, run:\n\tgo get %[2]s@%[3]s", pkg.path, dep.path, dep.mod.Version)
   885  					}
   886  					ld.direct[dep.mod.Path] = true
   887  				}
   888  			}
   889  		}
   890  	}
   891  	base.ExitIfErrors()
   892  
   893  	// If we didn't scan all of the imports from the main module, or didn't use
   894  	// imports.AnyTags, then we didn't necessarily load every package that
   895  	// contributes “direct” imports — so we can't safely mark existing
   896  	// dependencies as indirect-only.
   897  	// Conservatively mark those dependencies as direct.
   898  	if modFile != nil && (!ld.allPatternIsRoot || !reflect.DeepEqual(ld.Tags, imports.AnyTags())) {
   899  		for _, r := range modFile.Require {
   900  			if !r.Indirect {
   901  				ld.direct[r.Mod.Path] = true
   902  			}
   903  		}
   904  	}
   905  
   906  	return ld
   907  }
   908  
   909  // resolveMissingImports adds module dependencies to the global build list
   910  // in order to resolve missing packages from pkgs.
   911  //
   912  // The newly-resolved packages are added to the addedModuleFor map, and
   913  // resolveMissingImports returns a map from each newly-added module version to
   914  // the first package for which that module was added.
   915  func (ld *loader) resolveMissingImports(addedModuleFor map[string]bool) (modAddedBy map[module.Version]*loadPkg) {
   916  	var needPkgs []*loadPkg
   917  	for _, pkg := range ld.pkgs {
   918  		if pkg.err == nil {
   919  			continue
   920  		}
   921  		if pkg.isTest() {
   922  			// If we are missing a test, we are also missing its non-test version, and
   923  			// we should only add the missing import once.
   924  			continue
   925  		}
   926  		if !errors.As(pkg.err, new(*ImportMissingError)) {
   927  			// Leave other errors for Import or load.Packages to report.
   928  			continue
   929  		}
   930  
   931  		needPkgs = append(needPkgs, pkg)
   932  
   933  		pkg := pkg
   934  		ld.work.Add(func() {
   935  			pkg.mod, pkg.err = queryImport(context.TODO(), pkg.path)
   936  		})
   937  	}
   938  	<-ld.work.Idle()
   939  
   940  	modAddedBy = map[module.Version]*loadPkg{}
   941  	for _, pkg := range needPkgs {
   942  		if pkg.err != nil {
   943  			continue
   944  		}
   945  
   946  		fmt.Fprintf(os.Stderr, "go: found %s in %s %s\n", pkg.path, pkg.mod.Path, pkg.mod.Version)
   947  		if addedModuleFor[pkg.path] {
   948  			// TODO(bcmills): This should only be an error if pkg.mod is the same
   949  			// version we already tried to add previously.
   950  			base.Fatalf("go: %s: looping trying to add package", pkg.stackText())
   951  		}
   952  		if modAddedBy[pkg.mod] == nil {
   953  			modAddedBy[pkg.mod] = pkg
   954  			buildList = append(buildList, pkg.mod)
   955  		}
   956  	}
   957  
   958  	return modAddedBy
   959  }
   960  
   961  // pkg locates the *loadPkg for path, creating and queuing it for loading if
   962  // needed, and updates its state to reflect the given flags.
   963  //
   964  // The imports of the returned *loadPkg will be loaded asynchronously in the
   965  // ld.work queue, and its test (if requested) will also be populated once
   966  // imports have been resolved. When ld.work goes idle, all transitive imports of
   967  // the requested package (and its test, if requested) will have been loaded.
   968  func (ld *loader) pkg(path string, flags loadPkgFlags) *loadPkg {
   969  	if flags.has(pkgImportsLoaded) {
   970  		panic("internal error: (*loader).pkg called with pkgImportsLoaded flag set")
   971  	}
   972  
   973  	pkg := ld.pkgCache.Do(path, func() interface{} {
   974  		pkg := &loadPkg{
   975  			path: path,
   976  		}
   977  		ld.applyPkgFlags(pkg, flags)
   978  
   979  		ld.work.Add(func() { ld.load(pkg) })
   980  		return pkg
   981  	}).(*loadPkg)
   982  
   983  	ld.applyPkgFlags(pkg, flags)
   984  	return pkg
   985  }
   986  
   987  // applyPkgFlags updates pkg.flags to set the given flags and propagate the
   988  // (transitive) effects of those flags, possibly loading or enqueueing further
   989  // packages as a result.
   990  func (ld *loader) applyPkgFlags(pkg *loadPkg, flags loadPkgFlags) {
   991  	if flags == 0 {
   992  		return
   993  	}
   994  
   995  	if flags.has(pkgInAll) && ld.allPatternIsRoot && !pkg.isTest() {
   996  		// This package matches a root pattern by virtue of being in "all".
   997  		flags |= pkgIsRoot
   998  	}
   999  
  1000  	old := pkg.flags.update(flags)
  1001  	new := old | flags
  1002  	if new == old || !new.has(pkgImportsLoaded) {
  1003  		// We either didn't change the state of pkg, or we don't know anything about
  1004  		// its dependencies yet. Either way, we can't usefully load its test or
  1005  		// update its dependencies.
  1006  		return
  1007  	}
  1008  
  1009  	if !pkg.isTest() {
  1010  		// Check whether we should add (or update the flags for) a test for pkg.
  1011  		// ld.pkgTest is idempotent and extra invocations are inexpensive,
  1012  		// so it's ok if we call it more than is strictly necessary.
  1013  		wantTest := false
  1014  		switch {
  1015  		case ld.allPatternIsRoot && pkg.mod == Target:
  1016  			// We are loading the "all" pattern, which includes packages imported by
  1017  			// tests in the main module. This package is in the main module, so we
  1018  			// need to identify the imports of its test even if LoadTests is not set.
  1019  			//
  1020  			// (We will filter out the extra tests explicitly in computePatternAll.)
  1021  			wantTest = true
  1022  
  1023  		case ld.allPatternIsRoot && ld.allClosesOverTests && new.has(pkgInAll):
  1024  			// This variant of the "all" pattern includes imports of tests of every
  1025  			// package that is itself in "all", and pkg is in "all", so its test is
  1026  			// also in "all" (as above).
  1027  			wantTest = true
  1028  
  1029  		case ld.LoadTests && new.has(pkgIsRoot):
  1030  			// LoadTest explicitly requests tests of “the root packages”.
  1031  			wantTest = true
  1032  		}
  1033  
  1034  		if wantTest {
  1035  			var testFlags loadPkgFlags
  1036  			if pkg.mod == Target || (ld.allClosesOverTests && new.has(pkgInAll)) {
  1037  				// Tests of packages in the main module are in "all", in the sense that
  1038  				// they cause the packages they import to also be in "all". So are tests
  1039  				// of packages in "all" if "all" closes over test dependencies.
  1040  				testFlags |= pkgInAll
  1041  			}
  1042  			ld.pkgTest(pkg, testFlags)
  1043  		}
  1044  	}
  1045  
  1046  	if new.has(pkgInAll) && !old.has(pkgInAll|pkgImportsLoaded) {
  1047  		// We have just marked pkg with pkgInAll, or we have just loaded its
  1048  		// imports, or both. Now is the time to propagate pkgInAll to the imports.
  1049  		for _, dep := range pkg.imports {
  1050  			ld.applyPkgFlags(dep, pkgInAll)
  1051  		}
  1052  	}
  1053  }
  1054  
  1055  // load loads an individual package.
  1056  func (ld *loader) load(pkg *loadPkg) {
  1057  	if strings.Contains(pkg.path, "@") {
  1058  		// Leave for error during load.
  1059  		return
  1060  	}
  1061  	if build.IsLocalImport(pkg.path) || filepath.IsAbs(pkg.path) {
  1062  		// Leave for error during load.
  1063  		// (Module mode does not allow local imports.)
  1064  		return
  1065  	}
  1066  
  1067  	if search.IsMetaPackage(pkg.path) {
  1068  		pkg.err = &invalidImportError{
  1069  			importPath: pkg.path,
  1070  			err:        fmt.Errorf("%q is not an importable package; see 'go help packages'", pkg.path),
  1071  		}
  1072  		return
  1073  	}
  1074  
  1075  	pkg.mod, pkg.dir, pkg.err = importFromBuildList(context.TODO(), pkg.path, buildList)
  1076  	if pkg.dir == "" {
  1077  		return
  1078  	}
  1079  	if pkg.mod == Target {
  1080  		// Go ahead and mark pkg as in "all". This provides the invariant that a
  1081  		// package that is *only* imported by other packages in "all" is always
  1082  		// marked as such before loading its imports.
  1083  		//
  1084  		// We don't actually rely on that invariant at the moment, but it may
  1085  		// improve efficiency somewhat and makes the behavior a bit easier to reason
  1086  		// about (by reducing churn on the flag bits of dependencies), and costs
  1087  		// essentially nothing (these atomic flag ops are essentially free compared
  1088  		// to scanning source code for imports).
  1089  		ld.applyPkgFlags(pkg, pkgInAll)
  1090  	}
  1091  	if ld.AllowPackage != nil {
  1092  		if err := ld.AllowPackage(context.TODO(), pkg.path, pkg.mod); err != nil {
  1093  			pkg.err = err
  1094  		}
  1095  	}
  1096  
  1097  	pkg.inStd = (search.IsStandardImportPath(pkg.path) && search.InDir(pkg.dir, cfg.GOROOTsrc) != "")
  1098  
  1099  	var imports, testImports []string
  1100  
  1101  	if cfg.BuildContext.Compiler == "gccgo" && pkg.inStd {
  1102  		// We can't scan standard packages for gccgo.
  1103  	} else {
  1104  		var err error
  1105  		imports, testImports, err = scanDir(pkg.dir, ld.Tags)
  1106  		if err != nil {
  1107  			pkg.err = err
  1108  			return
  1109  		}
  1110  	}
  1111  
  1112  	pkg.imports = make([]*loadPkg, 0, len(imports))
  1113  	var importFlags loadPkgFlags
  1114  	if pkg.flags.has(pkgInAll) {
  1115  		importFlags = pkgInAll
  1116  	}
  1117  	for _, path := range imports {
  1118  		if pkg.inStd {
  1119  			// Imports from packages in "std" and "cmd" should resolve using
  1120  			// GOROOT/src/vendor even when "std" is not the main module.
  1121  			path = ld.stdVendor(pkg.path, path)
  1122  		}
  1123  		pkg.imports = append(pkg.imports, ld.pkg(path, importFlags))
  1124  	}
  1125  	pkg.testImports = testImports
  1126  
  1127  	ld.applyPkgFlags(pkg, pkgImportsLoaded)
  1128  }
  1129  
  1130  // pkgTest locates the test of pkg, creating it if needed, and updates its state
  1131  // to reflect the given flags.
  1132  //
  1133  // pkgTest requires that the imports of pkg have already been loaded (flagged
  1134  // with pkgImportsLoaded).
  1135  func (ld *loader) pkgTest(pkg *loadPkg, testFlags loadPkgFlags) *loadPkg {
  1136  	if pkg.isTest() {
  1137  		panic("pkgTest called on a test package")
  1138  	}
  1139  
  1140  	createdTest := false
  1141  	pkg.testOnce.Do(func() {
  1142  		pkg.test = &loadPkg{
  1143  			path:   pkg.path,
  1144  			testOf: pkg,
  1145  			mod:    pkg.mod,
  1146  			dir:    pkg.dir,
  1147  			err:    pkg.err,
  1148  			inStd:  pkg.inStd,
  1149  		}
  1150  		ld.applyPkgFlags(pkg.test, testFlags)
  1151  		createdTest = true
  1152  	})
  1153  
  1154  	test := pkg.test
  1155  	if createdTest {
  1156  		test.imports = make([]*loadPkg, 0, len(pkg.testImports))
  1157  		var importFlags loadPkgFlags
  1158  		if test.flags.has(pkgInAll) {
  1159  			importFlags = pkgInAll
  1160  		}
  1161  		for _, path := range pkg.testImports {
  1162  			if pkg.inStd {
  1163  				path = ld.stdVendor(test.path, path)
  1164  			}
  1165  			test.imports = append(test.imports, ld.pkg(path, importFlags))
  1166  		}
  1167  		pkg.testImports = nil
  1168  		ld.applyPkgFlags(test, pkgImportsLoaded)
  1169  	} else {
  1170  		ld.applyPkgFlags(test, testFlags)
  1171  	}
  1172  
  1173  	return test
  1174  }
  1175  
  1176  // stdVendor returns the canonical import path for the package with the given
  1177  // path when imported from the standard-library package at parentPath.
  1178  func (ld *loader) stdVendor(parentPath, path string) string {
  1179  	if search.IsStandardImportPath(path) {
  1180  		return path
  1181  	}
  1182  
  1183  	if str.HasPathPrefix(parentPath, "cmd") {
  1184  		if Target.Path != "cmd" {
  1185  			vendorPath := pathpkg.Join("cmd", "vendor", path)
  1186  			if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
  1187  				return vendorPath
  1188  			}
  1189  		}
  1190  	} else if Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
  1191  		// If we are outside of the 'std' module, resolve imports from within 'std'
  1192  		// to the vendor directory.
  1193  		//
  1194  		// Do the same for importers beginning with the prefix 'vendor/' even if we
  1195  		// are *inside* of the 'std' module: the 'vendor/' packages that resolve
  1196  		// globally from GOROOT/src/vendor (and are listed as part of 'go list std')
  1197  		// are distinct from the real module dependencies, and cannot import
  1198  		// internal packages from the real module.
  1199  		//
  1200  		// (Note that although the 'vendor/' packages match the 'std' *package*
  1201  		// pattern, they are not part of the std *module*, and do not affect
  1202  		// 'go mod tidy' and similar module commands when working within std.)
  1203  		vendorPath := pathpkg.Join("vendor", path)
  1204  		if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
  1205  			return vendorPath
  1206  		}
  1207  	}
  1208  
  1209  	// Not vendored: resolve from modules.
  1210  	return path
  1211  }
  1212  
  1213  // computePatternAll returns the list of packages matching pattern "all",
  1214  // starting with a list of the import paths for the packages in the main module.
  1215  func (ld *loader) computePatternAll() (all []string) {
  1216  	for _, pkg := range ld.pkgs {
  1217  		if pkg.flags.has(pkgInAll) && !pkg.isTest() {
  1218  			all = append(all, pkg.path)
  1219  		}
  1220  	}
  1221  	sort.Strings(all)
  1222  	return all
  1223  }
  1224  
  1225  // scanDir is like imports.ScanDir but elides known magic imports from the list,
  1226  // so that we do not go looking for packages that don't really exist.
  1227  //
  1228  // The standard magic import is "C", for cgo.
  1229  //
  1230  // The only other known magic imports are appengine and appengine/*.
  1231  // These are so old that they predate "go get" and did not use URL-like paths.
  1232  // Most code today now uses google.golang.org/appengine instead,
  1233  // but not all code has been so updated. When we mostly ignore build tags
  1234  // during "go vendor", we look into "// +build appengine" files and
  1235  // may see these legacy imports. We drop them so that the module
  1236  // search does not look for modules to try to satisfy them.
  1237  func scanDir(dir string, tags map[string]bool) (imports_, testImports []string, err error) {
  1238  	imports_, testImports, err = imports.ScanDir(dir, tags)
  1239  
  1240  	filter := func(x []string) []string {
  1241  		w := 0
  1242  		for _, pkg := range x {
  1243  			if pkg != "C" && pkg != "appengine" && !strings.HasPrefix(pkg, "appengine/") &&
  1244  				pkg != "appengine_internal" && !strings.HasPrefix(pkg, "appengine_internal/") {
  1245  				x[w] = pkg
  1246  				w++
  1247  			}
  1248  		}
  1249  		return x[:w]
  1250  	}
  1251  
  1252  	return filter(imports_), filter(testImports), err
  1253  }
  1254  
  1255  // buildStacks computes minimal import stacks for each package,
  1256  // for use in error messages. When it completes, packages that
  1257  // are part of the original root set have pkg.stack == nil,
  1258  // and other packages have pkg.stack pointing at the next
  1259  // package up the import stack in their minimal chain.
  1260  // As a side effect, buildStacks also constructs ld.pkgs,
  1261  // the list of all packages loaded.
  1262  func (ld *loader) buildStacks() {
  1263  	if len(ld.pkgs) > 0 {
  1264  		panic("buildStacks")
  1265  	}
  1266  	for _, pkg := range ld.roots {
  1267  		pkg.stack = pkg // sentinel to avoid processing in next loop
  1268  		ld.pkgs = append(ld.pkgs, pkg)
  1269  	}
  1270  	for i := 0; i < len(ld.pkgs); i++ { // not range: appending to ld.pkgs in loop
  1271  		pkg := ld.pkgs[i]
  1272  		for _, next := range pkg.imports {
  1273  			if next.stack == nil {
  1274  				next.stack = pkg
  1275  				ld.pkgs = append(ld.pkgs, next)
  1276  			}
  1277  		}
  1278  		if next := pkg.test; next != nil && next.stack == nil {
  1279  			next.stack = pkg
  1280  			ld.pkgs = append(ld.pkgs, next)
  1281  		}
  1282  	}
  1283  	for _, pkg := range ld.roots {
  1284  		pkg.stack = nil
  1285  	}
  1286  }
  1287  
  1288  // stackText builds the import stack text to use when
  1289  // reporting an error in pkg. It has the general form
  1290  //
  1291  //	root imports
  1292  //		other imports
  1293  //		other2 tested by
  1294  //		other2.test imports
  1295  //		pkg
  1296  //
  1297  func (pkg *loadPkg) stackText() string {
  1298  	var stack []*loadPkg
  1299  	for p := pkg; p != nil; p = p.stack {
  1300  		stack = append(stack, p)
  1301  	}
  1302  
  1303  	var buf bytes.Buffer
  1304  	for i := len(stack) - 1; i >= 0; i-- {
  1305  		p := stack[i]
  1306  		fmt.Fprint(&buf, p.path)
  1307  		if p.testOf != nil {
  1308  			fmt.Fprint(&buf, ".test")
  1309  		}
  1310  		if i > 0 {
  1311  			if stack[i-1].testOf == p {
  1312  				fmt.Fprint(&buf, " tested by\n\t")
  1313  			} else {
  1314  				fmt.Fprint(&buf, " imports\n\t")
  1315  			}
  1316  		}
  1317  	}
  1318  	return buf.String()
  1319  }
  1320  
  1321  // why returns the text to use in "go mod why" output about the given package.
  1322  // It is less ornate than the stackText but contains the same information.
  1323  func (pkg *loadPkg) why() string {
  1324  	var buf strings.Builder
  1325  	var stack []*loadPkg
  1326  	for p := pkg; p != nil; p = p.stack {
  1327  		stack = append(stack, p)
  1328  	}
  1329  
  1330  	for i := len(stack) - 1; i >= 0; i-- {
  1331  		p := stack[i]
  1332  		if p.testOf != nil {
  1333  			fmt.Fprintf(&buf, "%s.test\n", p.testOf.path)
  1334  		} else {
  1335  			fmt.Fprintf(&buf, "%s\n", p.path)
  1336  		}
  1337  	}
  1338  	return buf.String()
  1339  }
  1340  
  1341  // Why returns the "go mod why" output stanza for the given package,
  1342  // without the leading # comment.
  1343  // The package graph must have been loaded already, usually by LoadPackages.
  1344  // If there is no reason for the package to be in the current build,
  1345  // Why returns an empty string.
  1346  func Why(path string) string {
  1347  	pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
  1348  	if !ok {
  1349  		return ""
  1350  	}
  1351  	return pkg.why()
  1352  }
  1353  
  1354  // WhyDepth returns the number of steps in the Why listing.
  1355  // If there is no reason for the package to be in the current build,
  1356  // WhyDepth returns 0.
  1357  func WhyDepth(path string) int {
  1358  	n := 0
  1359  	pkg, _ := loaded.pkgCache.Get(path).(*loadPkg)
  1360  	for p := pkg; p != nil; p = p.stack {
  1361  		n++
  1362  	}
  1363  	return n
  1364  }
  1365  

View as plain text