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.
7 // This file contains the module-mode package loader, as well as some accessory
8 // functions pertaining to the package import graph.
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.
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.
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.
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.
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".
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.)
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+).
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+.)
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
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
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).
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.
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
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.
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"
123 "golang.org/x/mod/module"
126 // loaded is the most recently-used package loader.
127 // It holds details about individual packages.
129 // This variable should only be accessed directly in top-level exported
130 // functions. All other functions that require or produce a *loader should pass
131 // or return it as an explicit parameter.
134 // PackageOpts control the behavior of the LoadPackages function.
135 type PackageOpts struct {
136 // Tags are the build tags in effect (as interpreted by the
137 // cmd/go/internal/imports package).
138 // If nil, treated as equivalent to imports.Tags().
141 // Tidy, if true, requests that the build list and go.sum file be reduced to
142 // the minimial dependencies needed to reproducibly reload the requested
146 // VendorModulesInGOROOTSrc indicates that if we are within a module in
147 // GOROOT/src, packages in the module's vendor directory should be resolved as
148 // actual module dependencies (instead of standard-library packages).
149 VendorModulesInGOROOTSrc bool
151 // ResolveMissingImports indicates that we should attempt to add module
152 // dependencies as needed to resolve imports of packages that are not found.
154 // For commands that support the -mod flag, resolving imports may still fail
155 // if the flag is set to "readonly" (the default) or "vendor".
156 ResolveMissingImports bool
158 // AllowPackage, if non-nil, is called after identifying the module providing
159 // each package. If AllowPackage returns a non-nil error, that error is set
160 // for the package, and the imports and test of that package will not be
163 // AllowPackage may be invoked concurrently by multiple goroutines,
164 // and may be invoked multiple times for a given package path.
165 AllowPackage func(ctx context.Context, path string, mod module.Version) error
167 // LoadTests loads the test dependencies of each package matching a requested
168 // pattern. If ResolveMissingImports is also true, test dependencies will be
169 // resolved if missing.
172 // UseVendorAll causes the "all" package pattern to be interpreted as if
173 // running "go mod vendor" (or building with "-mod=vendor").
175 // This is a no-op for modules that declare 'go 1.16' or higher, for which this
176 // is the default (and only) interpretation of the "all" pattern in module mode.
179 // AllowErrors indicates that LoadPackages should not terminate the process if
183 // SilencePackageErrors indicates that LoadPackages should not print errors
184 // that occur while matching or loading packages, and should not terminate the
185 // process if such an error occurs.
187 // Errors encountered in the module graph will still be reported.
189 // The caller may retrieve the silenced package errors using the Lookup
190 // function, and matching errors are still populated in the Errs field of the
191 // associated search.Match.)
192 SilencePackageErrors bool
194 // SilenceMissingStdImports indicates that LoadPackages should not print
195 // errors or terminate the process if an imported package is missing, and the
196 // import path looks like it might be in the standard library (perhaps in a
198 SilenceMissingStdImports bool
200 // SilenceNoGoErrors indicates that LoadPackages should not print
201 // imports.ErrNoGo errors.
202 // This allows the caller to invoke LoadPackages (and report other errors)
203 // without knowing whether the requested packages exist for the given tags.
205 // Note that if a requested package does not exist *at all*, it will fail
206 // during module resolution and the error will not be suppressed.
207 SilenceNoGoErrors bool
209 // SilenceUnmatchedWarnings suppresses the warnings normally emitted for
210 // patterns that did not match any packages.
211 SilenceUnmatchedWarnings bool
214 // LoadPackages identifies the set of packages matching the given patterns and
215 // loads the packages in the import graph rooted at that set.
216 func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (matches []*search.Match, loadedPackages []string) {
217 if opts.Tags == nil {
218 opts.Tags = imports.Tags()
221 patterns = search.CleanPatterns(patterns)
222 matches = make([]*search.Match, 0, len(patterns))
223 allPatternIsRoot := false
224 for _, pattern := range patterns {
225 matches = append(matches, search.NewMatch(pattern))
226 if pattern == "all" {
227 allPatternIsRoot = true
231 updateMatches := func(rs *Requirements, ld *loader) {
232 for _, m := range matches {
235 // Evaluate list of file system directories on first iteration.
237 matchLocalDirs(ctx, m, rs)
240 // Make a copy of the directory list and translate to import paths.
241 // Note that whether a directory corresponds to an import path
242 // changes as the build list is updated, and a directory can change
243 // from not being in the build list to being in it and back as
244 // the exact version of a particular module increases during
245 // the loader iterations.
247 for _, dir := range m.Dirs {
248 pkg, err := resolveLocalPackage(ctx, dir, rs)
250 if !m.IsLiteral() && (err == errPkgIsBuiltin || err == errPkgIsGorootSrc) {
251 continue // Don't include "builtin" or GOROOT/src in wildcard patterns.
254 // If we're outside of a module, ensure that the failure mode
263 m.Pkgs = append(m.Pkgs, pkg)
267 m.Pkgs = []string{m.Pattern()}
269 case strings.Contains(m.Pattern(), "..."):
271 mg, err := rs.Graph(ctx)
273 // The module graph is (or may be) incomplete — perhaps we failed to
274 // load the requirements of some module. This is an error in matching
275 // the patterns to packages, because we may be missing some packages
276 // or we may erroneously match packages in the wrong versions of
277 // modules. However, for cases like 'go list -e', the error should not
278 // necessarily prevent us from loading the packages we could find.
279 m.Errs = append(m.Errs, err)
281 matchPackages(ctx, m, opts.Tags, includeStd, mg.BuildList())
283 case m.Pattern() == "all":
285 // The initial roots are the packages in the main module.
286 // loadFromRoots will expand that to "all".
288 matchPackages(ctx, m, opts.Tags, omitStd, []module.Version{Target})
290 // Starting with the packages in the main module,
291 // enumerate the full list of "all".
292 m.Pkgs = ld.computePatternAll()
295 case m.Pattern() == "std" || m.Pattern() == "cmd":
297 m.MatchPackages() // Locate the packages within GOROOT/src.
301 panic(fmt.Sprintf("internal error: modload missing case for pattern %s", m.Pattern()))
306 initialRS, _ := loadModFile(ctx) // Ignore needCommit — we're going to commit at the end regardless.
308 ld := loadFromRoots(ctx, loaderParams{
310 requirements: initialRS,
312 allClosesOverTests: index.allPatternClosesOverTests() && !opts.UseVendorAll,
313 allPatternIsRoot: allPatternIsRoot,
315 listRoots: func(rs *Requirements) (roots []string) {
316 updateMatches(rs, nil)
317 for _, m := range matches {
318 roots = append(roots, m.Pkgs...)
324 // One last pass to finalize wildcards.
325 updateMatches(ld.requirements, ld)
327 // List errors in matching patterns (such as directory permission
328 // errors for wildcard patterns).
329 if !ld.SilencePackageErrors {
330 for _, match := range matches {
331 for _, err := range match.Errs {
332 ld.errorf("%v\n", err)
338 if !opts.SilenceUnmatchedWarnings {
339 search.WarnUnmatched(matches)
344 mg, _ := ld.requirements.Graph(ctx)
346 for _, m := range initialRS.rootModules {
348 if ld.requirements.depth == eager {
349 // m is unused if it was dropped from the module graph entirely. If it
350 // was only demoted from direct to indirect, it may still be in use via
351 // a transitive import.
352 unused = mg.Selected(m.Path) == "none"
354 // m is unused if it was dropped from the roots. If it is still present
355 // as a transitive dependency, that transitive dependency is not needed
356 // by any package or test in the main module.
357 _, ok := ld.requirements.rootSelected(m.Path)
361 fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
366 modfetch.TrimGoSum(keepSums(ctx, ld, ld.requirements, loadedZipSumsOnly))
369 // Success! Update go.mod and go.sum (if needed) and return the results.
371 commitRequirements(ctx, loaded.requirements)
373 for _, pkg := range ld.pkgs {
375 loadedPackages = append(loadedPackages, pkg.path)
378 sort.Strings(loadedPackages)
379 return matches, loadedPackages
382 // matchLocalDirs is like m.MatchDirs, but tries to avoid scanning directories
383 // outside of the standard library and active modules.
384 func matchLocalDirs(ctx context.Context, m *search.Match, rs *Requirements) {
386 panic(fmt.Sprintf("internal error: resolveLocalDirs on non-local pattern %s", m.Pattern()))
389 if i := strings.Index(m.Pattern(), "..."); i >= 0 {
390 // The pattern is local, but it is a wildcard. Its packages will
391 // only resolve to paths if they are inside of the standard
392 // library, the main module, or some dependency of the main
393 // module. Verify that before we walk the filesystem: a filesystem
394 // walk in a directory like /var or /etc can be very expensive!
395 dir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3]))
397 if !filepath.IsAbs(dir) {
398 absDir = filepath.Join(base.Cwd, dir)
400 if search.InDir(absDir, cfg.GOROOTsrc) == "" && search.InDir(absDir, ModRoot()) == "" && pathInModuleCache(ctx, absDir, rs) == "" {
402 m.AddError(fmt.Errorf("directory prefix %s outside available modules", base.ShortPath(absDir)))
410 // resolveLocalPackage resolves a filesystem path to a package path.
411 func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (string, error) {
413 if filepath.IsAbs(dir) {
414 absDir = filepath.Clean(dir)
416 absDir = filepath.Join(base.Cwd, dir)
419 bp, err := cfg.BuildContext.ImportDir(absDir, 0)
420 if err != nil && (bp == nil || len(bp.IgnoredGoFiles) == 0) {
421 // golang.org/issue/32917: We should resolve a relative path to a
422 // package path only if the relative path actually contains the code
425 // If the named directory does not exist or contains no Go files,
426 // the package does not exist.
427 // Other errors may affect package loading, but not resolution.
428 if _, err := fsys.Stat(absDir); err != nil {
429 if os.IsNotExist(err) {
430 // Canonicalize OS-specific errors to errDirectoryNotFound so that error
431 // messages will be easier for users to search for.
432 return "", &fs.PathError{Op: "stat", Path: absDir, Err: errDirectoryNotFound}
436 if _, noGo := err.(*build.NoGoError); noGo {
437 // A directory that does not contain any Go source files — even ignored
438 // ones! — is not a Go package, and we can't resolve it to a package
439 // path because that path could plausibly be provided by some other
442 // Any other error indicates that the package “exists” (at least in the
443 // sense that it cannot exist in any other module), but has some other
444 // problem (such as a syntax error).
449 if modRoot != "" && absDir == modRoot {
450 if absDir == cfg.GOROOTsrc {
451 return "", errPkgIsGorootSrc
453 return targetPrefix, nil
456 // Note: The checks for @ here are just to avoid misinterpreting
457 // the module cache directories (formerly GOPATH/src/mod/foo@v1.5.2/bar).
458 // It's not strictly necessary but helpful to keep the checks.
459 if modRoot != "" && strings.HasPrefix(absDir, modRoot+string(filepath.Separator)) && !strings.Contains(absDir[len(modRoot):], "@") {
460 suffix := filepath.ToSlash(absDir[len(modRoot):])
461 if strings.HasPrefix(suffix, "/vendor/") {
462 if cfg.BuildMod != "vendor" {
463 return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir)
467 pkg := strings.TrimPrefix(suffix, "/vendor/")
468 if _, ok := vendorPkgModule[pkg]; !ok {
469 return "", fmt.Errorf("directory %s is not a package listed in vendor/modules.txt", absDir)
474 if targetPrefix == "" {
475 pkg := strings.TrimPrefix(suffix, "/")
476 if pkg == "builtin" {
477 // "builtin" is a pseudo-package with a real source file.
478 // It's not included in "std", so it shouldn't resolve from "."
479 // within module "std" either.
480 return "", errPkgIsBuiltin
485 pkg := targetPrefix + suffix
486 if _, ok, err := dirInModule(pkg, targetPrefix, modRoot, true); err != nil {
489 return "", &PackageNotInModuleError{Mod: Target, Pattern: pkg}
494 if sub := search.InDir(absDir, cfg.GOROOTsrc); sub != "" && sub != "." && !strings.Contains(sub, "@") {
495 pkg := filepath.ToSlash(sub)
496 if pkg == "builtin" {
497 return "", errPkgIsBuiltin
502 pkg := pathInModuleCache(ctx, absDir, rs)
504 return "", fmt.Errorf("directory %s outside available modules", base.ShortPath(absDir))
510 errDirectoryNotFound = errors.New("directory not found")
511 errPkgIsGorootSrc = errors.New("GOROOT/src is not an importable package")
512 errPkgIsBuiltin = errors.New(`"builtin" is a pseudo-package, not an importable package`)
515 // pathInModuleCache returns the import path of the directory dir,
516 // if dir is in the module cache copy of a module in our build list.
517 func pathInModuleCache(ctx context.Context, dir string, rs *Requirements) string {
518 tryMod := func(m module.Version) (string, bool) {
521 if repl := Replacement(m); repl.Path != "" && repl.Version == "" {
523 if !filepath.IsAbs(root) {
524 root = filepath.Join(ModRoot(), root)
526 } else if repl.Path != "" {
527 root, err = modfetch.DownloadDir(repl)
529 root, err = modfetch.DownloadDir(m)
535 sub := search.InDir(dir, root)
539 sub = filepath.ToSlash(sub)
540 if strings.Contains(sub, "/vendor/") || strings.HasPrefix(sub, "vendor/") || strings.Contains(sub, "@") {
544 return path.Join(m.Path, filepath.ToSlash(sub)), true
547 if rs.depth == lazy {
548 for _, m := range rs.rootModules {
549 if v, _ := rs.rootSelected(m.Path); v != m.Version {
550 continue // m is a root, but we have a higher root for the same path.
552 if importPath, ok := tryMod(m); ok {
553 // checkMultiplePaths ensures that a module can be used for at most one
554 // requirement, so this must be it.
560 // None of the roots contained dir, or we're in eager mode and want to load
561 // the full module graph more aggressively. Either way, check the full graph
562 // to see if the directory is a non-root dependency.
564 // If the roots are not consistent with the full module graph, the selected
565 // versions of root modules may differ from what we already checked above.
566 // Re-check those paths too.
568 mg, _ := rs.Graph(ctx)
569 var importPath string
570 for _, m := range mg.BuildList() {
572 importPath, found = tryMod(m)
580 // ImportFromFiles adds modules to the build list as needed
581 // to satisfy the imports in the named Go source files.
583 // Errors in missing dependencies are silenced.
585 // TODO(bcmills): Silencing errors seems off. Take a closer look at this and
586 // figure out what the error-reporting actually ought to be.
587 func ImportFromFiles(ctx context.Context, gofiles []string) {
588 rs := LoadModFile(ctx)
590 tags := imports.Tags()
591 imports, testImports, err := imports.ScanFiles(gofiles, tags)
593 base.Fatalf("go: %v", err)
596 loaded = loadFromRoots(ctx, loaderParams{
597 PackageOpts: PackageOpts{
599 ResolveMissingImports: true,
600 SilencePackageErrors: true,
603 allClosesOverTests: index.allPatternClosesOverTests(),
604 listRoots: func(*Requirements) (roots []string) {
605 roots = append(roots, imports...)
606 roots = append(roots, testImports...)
610 commitRequirements(ctx, loaded.requirements)
613 // DirImportPath returns the effective import path for dir,
614 // provided it is within the main module, or else returns ".".
615 func DirImportPath(ctx context.Context, dir string) string {
619 LoadModFile(ctx) // Sets targetPrefix.
621 if !filepath.IsAbs(dir) {
622 dir = filepath.Join(base.Cwd, dir)
624 dir = filepath.Clean(dir)
630 if strings.HasPrefix(dir, modRoot+string(filepath.Separator)) {
631 suffix := filepath.ToSlash(dir[len(modRoot):])
632 if strings.HasPrefix(suffix, "/vendor/") {
633 return strings.TrimPrefix(suffix, "/vendor/")
635 return targetPrefix + suffix
640 // TargetPackages returns the list of packages in the target (top-level) module
641 // matching pattern, which may be relative to the working directory, under all
642 // build tag settings.
643 func TargetPackages(ctx context.Context, pattern string) *search.Match {
644 // TargetPackages is relative to the main module, so ensure that the main
645 // module is a thing that can contain packages.
646 LoadModFile(ctx) // Sets Target.
647 ModRoot() // Emits an error if Target cannot contain packages.
649 m := search.NewMatch(pattern)
650 matchPackages(ctx, m, imports.AnyTags(), omitStd, []module.Version{Target})
654 // ImportMap returns the actual package import path
655 // for an import path found in source code.
656 // If the given import path does not appear in the source code
657 // for the packages that have been loaded, ImportMap returns the empty string.
658 func ImportMap(path string) string {
659 pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
666 // PackageDir returns the directory containing the source code
667 // for the package named by the import path.
668 func PackageDir(path string) string {
669 pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
676 // PackageModule returns the module providing the package named by the import path.
677 func PackageModule(path string) module.Version {
678 pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
680 return module.Version{}
685 // PackageImports returns the imports for the package named by the import path.
686 // Test imports will be returned as well if tests were loaded for the package
687 // (i.e., if "all" was loaded or if LoadTests was set and the path was matched
688 // by a command line argument). PackageImports will return nil for
689 // unknown package paths.
690 func PackageImports(path string) (imports, testImports []string) {
691 pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
695 imports = make([]string, len(pkg.imports))
696 for i, p := range pkg.imports {
700 testImports = make([]string, len(pkg.test.imports))
701 for i, p := range pkg.test.imports {
702 testImports[i] = p.path
705 return imports, testImports
708 // Lookup returns the source directory, import path, and any loading error for
709 // the package at path as imported from the package in parentDir.
710 // Lookup requires that one of the Load functions in this package has already
712 func Lookup(parentPath string, parentIsStd bool, path string) (dir, realPath string, err error) {
714 panic("Lookup called with empty package path")
718 path = loaded.stdVendor(parentPath, path)
720 pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
722 // The loader should have found all the relevant paths.
723 // There are a few exceptions, though:
724 // - during go list without -test, the p.Resolve calls to process p.TestImports and p.XTestImports
725 // end up here to canonicalize the import paths.
726 // - during any load, non-loaded packages like "unsafe" end up here.
727 // - during any load, build-injected dependencies like "runtime/cgo" end up here.
728 // - because we ignore appengine/* in the module loader,
729 // the dependencies of any actual appengine/* library end up here.
730 dir := findStandardImportPath(path)
732 return dir, path, nil
734 return "", "", errMissing
736 return pkg.dir, pkg.path, pkg.err
739 // A loader manages the process of loading information about
740 // the required packages for a particular build,
741 // checking that the packages are available in the module set,
742 // and updating the module set if needed.
748 // reset on each iteration
750 pkgCache *par.Cache // package path (string) → *loadPkg
751 pkgs []*loadPkg // transitive closure of loaded packages and tests; populated in buildStacks
754 // loaderParams configure the packages loaded by, and the properties reported
755 // by, a loader instance.
756 type loaderParams struct {
758 requirements *Requirements
760 allClosesOverTests bool // Does the "all" pattern include the transitive closure of tests of packages in "all"?
761 allPatternIsRoot bool // Is the "all" pattern an additional root?
763 listRoots func(rs *Requirements) []string
766 func (ld *loader) reset() {
768 case <-ld.work.Idle():
770 panic("loader.reset when not idle")
774 ld.pkgCache = new(par.Cache)
778 // errorf reports an error via either os.Stderr or base.Errorf,
779 // according to whether ld.AllowErrors is set.
780 func (ld *loader) errorf(format string, args ...interface{}) {
782 fmt.Fprintf(os.Stderr, format, args...)
784 base.Errorf(format, args...)
788 // A loadPkg records information about a single loaded package.
789 type loadPkg struct {
790 // Populated at construction time:
791 path string // import path
794 // Populated at construction time and updated by (*loader).applyPkgFlags:
795 flags atomicLoadPkgFlags
797 // Populated by (*loader).load:
798 mod module.Version // module providing package
799 dir string // directory containing source code
800 err error // error loading package
801 imports []*loadPkg // packages imported by this one
802 testImports []string // test-only imports, saved for use by pkg.test.
805 // Populated by (*loader).pkgTest:
809 // Populated by postprocessing in (*loader).buildStacks:
810 stack *loadPkg // package importing this one in minimal import stack for this pkg
813 // loadPkgFlags is a set of flags tracking metadata about a package.
814 type loadPkgFlags int8
817 // pkgInAll indicates that the package is in the "all" package pattern,
818 // regardless of whether we are loading the "all" package pattern.
820 // When the pkgInAll flag and pkgImportsLoaded flags are both set, the caller
821 // who set the last of those flags must propagate the pkgInAll marking to all
822 // of the imports of the marked package.
824 // A test is marked with pkgInAll if that test would promote the packages it
825 // imports to be in "all" (such as when the test is itself within the main
826 // module, or when ld.allClosesOverTests is true).
827 pkgInAll loadPkgFlags = 1 << iota
829 // pkgIsRoot indicates that the package matches one of the root package
830 // patterns requested by the caller.
832 // If LoadTests is set, then when pkgIsRoot and pkgImportsLoaded are both set,
833 // the caller who set the last of those flags must populate a test for the
834 // package (in the pkg.test field).
836 // If the "all" pattern is included as a root, then non-test packages in "all"
837 // are also roots (and must be marked pkgIsRoot).
840 // pkgImportsLoaded indicates that the imports and testImports fields of a
841 // loadPkg have been populated.
845 // has reports whether all of the flags in cond are set in f.
846 func (f loadPkgFlags) has(cond loadPkgFlags) bool {
847 return f&cond == cond
850 // An atomicLoadPkgFlags stores a loadPkgFlags for which individual flags can be
852 type atomicLoadPkgFlags struct {
856 // update sets the given flags in af (in addition to any flags already set).
858 // update returns the previous flag state so that the caller may determine which
859 // flags were newly-set.
860 func (af *atomicLoadPkgFlags) update(flags loadPkgFlags) (old loadPkgFlags) {
862 old := atomic.LoadInt32(&af.bits)
863 new := old | int32(flags)
864 if new == old || atomic.CompareAndSwapInt32(&af.bits, old, new) {
865 return loadPkgFlags(old)
870 // has reports whether all of the flags in cond are set in af.
871 func (af *atomicLoadPkgFlags) has(cond loadPkgFlags) bool {
872 return loadPkgFlags(atomic.LoadInt32(&af.bits))&cond == cond
875 // isTest reports whether pkg is a test of another package.
876 func (pkg *loadPkg) isTest() bool {
877 return pkg.testOf != nil
880 // fromExternalModule reports whether pkg was loaded from a module other than
882 func (pkg *loadPkg) fromExternalModule() bool {
883 if pkg.mod.Path == "" {
884 return false // loaded from the standard library, not a module
886 if pkg.mod.Path == Target.Path {
887 return false // loaded from the main module.
892 var errMissing = errors.New("cannot find package")
894 // loadFromRoots attempts to load the build graph needed to process a set of
895 // root packages and their dependencies.
897 // The set of root packages is returned by the params.listRoots function, and
898 // expanded to the full set of packages by tracing imports (and possibly tests)
900 func loadFromRoots(ctx context.Context, params loaderParams) *loader {
902 loaderParams: params,
903 work: par.NewQueue(runtime.GOMAXPROCS(0)),
906 if ld.requirements.depth == eager {
908 ld.requirements, _, err = expandGraph(ctx, ld.requirements)
910 ld.errorf("go: %v\n", err)
917 // Load the root packages and their imports.
918 // Note: the returned roots can change on each iteration,
919 // since the expansion of package patterns depends on the
920 // build list we're using.
921 rootPkgs := ld.listRoots(ld.requirements)
924 // Before we start loading transitive imports of packages, locate all of
925 // the root packages and promote their containing modules to root modules
926 // dependencies. If their go.mod files are tidy (the common case) and the
927 // set of root packages does not change then we can select the correct
928 // versions of all transitive imports on the first try and complete
929 // loading in a single iteration.
932 inRoots := map[*loadPkg]bool{}
933 for _, path := range rootPkgs {
934 root := ld.pkg(ctx, path, pkgIsRoot)
936 ld.roots = append(ld.roots, root)
941 // ld.pkg adds imported packages to the work queue and calls applyPkgFlags,
942 // which adds tests (and test dependencies) as needed.
944 // When all of the work in the queue has completed, we'll know that the
945 // transitive closure of dependencies has been loaded.
950 if !ld.ResolveMissingImports || (!HasModRoot() && !allowMissingModuleImports) {
951 // We've loaded as much as we can without resolving missing imports.
954 modAddedBy := ld.resolveMissingImports(ctx)
955 if len(modAddedBy) == 0 {
959 toAdd := make([]module.Version, 0, len(modAddedBy))
960 for m, _ := range modAddedBy {
961 toAdd = append(toAdd, m)
963 module.Sort(toAdd) // to make errors deterministic
965 prevRS := ld.requirements
966 if err := ld.updateRequirements(ctx, toAdd); err != nil {
967 // If an error was found in a newly added module, report the package
968 // import stack instead of the module requirement stack. Packages
969 // are more descriptive.
970 if err, ok := err.(*mvs.BuildListError); ok {
971 if pkg := modAddedBy[err.Module()]; pkg != nil {
972 base.Fatalf("go: %s: %v", pkg.stackText(), err.Err)
975 base.Fatalf("go: %v", err)
977 if reflect.DeepEqual(prevRS.rootModules, ld.requirements.rootModules) {
978 // Something is deeply wrong. resolveMissingImports gave us a non-empty
979 // set of modules to add, but adding those modules to the graph had no
981 panic(fmt.Sprintf("internal error: adding %v to module graph had no effect on root requirements (%v)", toAdd, prevRS.rootModules))
984 base.ExitIfErrors() // TODO(bcmills): Is this actually needed?
986 if err := ld.updateRequirements(ctx, nil); err != nil {
987 base.Fatalf("go: %v", err)
991 // Promoting a root can pull in previously-irrelevant requirements,
992 // changing the build list. Iterate until the roots are stable.
995 // Tidy the build list, if applicable, before we report errors.
996 // (The process of tidying may remove errors from irrelevant dependencies.)
999 ld.requirements, err = tidyRoots(ctx, ld.requirements, ld.pkgs)
1001 ld.errorf("go: %v\n", err)
1005 // Report errors, if any.
1006 for _, pkg := range ld.pkgs {
1011 // Add importer information to checksum errors.
1012 if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
1013 if importer := pkg.stack; importer != nil {
1014 sumErr.importer = importer.path
1015 sumErr.importerVersion = importer.mod.Version
1016 sumErr.importerIsTest = importer.testOf != nil
1020 if ld.SilencePackageErrors {
1023 if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) &&
1024 stdErr.isStd && ld.SilenceMissingStdImports {
1027 if ld.SilenceNoGoErrors && errors.Is(pkg.err, imports.ErrNoGo) {
1031 ld.errorf("%s: %v\n", pkg.stackText(), pkg.err)
1034 ld.checkMultiplePaths()
1038 // updateRequirements ensures that ld.requirements is consistent with the
1039 // information gained from ld.pkgs and includes the modules in add as roots at
1040 // at least the given versions.
1044 // - Modules that provide packages directly imported from the main module are
1045 // marked as direct, and are promoted to explicit roots. If a needed root
1046 // cannot be promoted due to -mod=readonly or -mod=vendor, the importing
1047 // package is marked with an error.
1049 // - If ld scanned the "all" pattern independent of build constraints, it is
1050 // guaranteed to have seen every direct import. Module dependencies that did
1051 // not provide any directly-imported package are then marked as indirect.
1053 // - Root dependencies are updated to their selected versions.
1054 func (ld *loader) updateRequirements(ctx context.Context, add []module.Version) error {
1055 rs := ld.requirements
1057 // Compute directly referenced dependency modules.
1058 direct := make(map[string]bool)
1059 for _, pkg := range ld.pkgs {
1060 if pkg.mod != Target {
1063 for _, dep := range pkg.imports {
1064 if dep.mod.Path == "" || dep.mod.Path == Target.Path {
1068 if pkg.err == nil && cfg.BuildMod != "mod" {
1069 if v, ok := rs.rootSelected(dep.mod.Path); !ok || v != dep.mod.Version {
1070 // dep.mod is not an explicit dependency, but needs to be.
1071 // Because we are not in "mod" mode, we will not be able to update it.
1072 // Instead, mark the importing package with an error.
1074 // TODO(#41688): The resulting error message fails to include the file
1075 // position of the import statement (because that information is not
1076 // tracked by the module loader). Figure out how to plumb the import
1077 // position through.
1078 pkg.err = &DirectImportFromImplicitDependencyError{
1079 ImporterPath: pkg.path,
1080 ImportedPath: dep.path,
1083 // cfg.BuildMod does not allow us to change dep.mod to be a direct
1084 // dependency, so don't mark it as such.
1089 // dep is a package directly imported by a package or test in the main
1090 // module and loaded from some other module (not the standard library).
1091 // Mark its module as a direct dependency.
1092 direct[dep.mod.Path] = true
1096 // If we didn't scan all of the imports from the main module, or didn't use
1097 // imports.AnyTags, then we didn't necessarily load every package that
1098 // contributes “direct” imports — so we can't safely mark existing direct
1099 // dependencies in ld.requirements as indirect-only. Propagate them as direct.
1100 loadedDirect := ld.allPatternIsRoot && reflect.DeepEqual(ld.Tags, imports.AnyTags())
1102 for mPath := range rs.direct {
1103 direct[mPath] = true
1107 rs, err := updateRoots(ctx, direct, rs, add)
1109 // We don't actually know what even the root requirements are supposed to be,
1110 // so we can't proceed with loading. Return the error to the caller
1113 if rs != ld.requirements {
1114 if _, err := rs.Graph(ctx); err != nil {
1115 ld.errorf("go: %v\n", err)
1117 ld.requirements = rs
1122 // resolveMissingImports returns a set of modules that could be added as
1123 // dependencies in order to resolve missing packages from pkgs.
1125 // The newly-resolved packages are added to the addedModuleFor map, and
1126 // resolveMissingImports returns a map from each new module version to
1127 // the first missing package that module would resolve.
1128 func (ld *loader) resolveMissingImports(ctx context.Context) (modAddedBy map[module.Version]*loadPkg) {
1129 type pkgMod struct {
1133 var pkgMods []pkgMod
1134 for _, pkg := range ld.pkgs {
1139 // If we are missing a test, we are also missing its non-test version, and
1140 // we should only add the missing import once.
1143 if !errors.As(pkg.err, new(*ImportMissingError)) {
1144 // Leave other errors for Import or load.Packages to report.
1149 var mod module.Version
1150 ld.work.Add(func() {
1152 mod, err = queryImport(ctx, pkg.path, ld.requirements)
1154 // pkg.err was already non-nil, so we can reasonably attribute the error
1155 // for pkg to either the original error or the one returned by
1156 // queryImport. The existing error indicates only that we couldn't find
1157 // the package, whereas the query error also explains why we didn't fix
1158 // the problem — so we prefer the latter.
1162 // err is nil, but we intentionally leave pkg.err non-nil and pkg.mod
1163 // unset: we still haven't satisfied other invariants of a
1164 // successfully-loaded package, such as scanning and loading the imports
1165 // of that package. If we succeed in resolving the new dependency graph,
1166 // the caller can reload pkg and update the error at that point.
1168 // Even then, the package might not be loaded from the version we've
1169 // identified here. The module may be upgraded by some other dependency,
1170 // or by a transitive dependency of mod itself, or — less likely — the
1171 // package may be rejected by an AllowPackage hook or rendered ambiguous
1172 // by some other newly-added or newly-upgraded dependency.
1175 pkgMods = append(pkgMods, pkgMod{pkg: pkg, mod: &mod})
1179 modAddedBy = map[module.Version]*loadPkg{}
1180 for _, pm := range pkgMods {
1181 pkg, mod := pm.pkg, *pm.mod
1186 fmt.Fprintf(os.Stderr, "go: found %s in %s %s\n", pkg.path, mod.Path, mod.Version)
1187 if modAddedBy[mod] == nil {
1188 modAddedBy[mod] = pkg
1195 // pkg locates the *loadPkg for path, creating and queuing it for loading if
1196 // needed, and updates its state to reflect the given flags.
1198 // The imports of the returned *loadPkg will be loaded asynchronously in the
1199 // ld.work queue, and its test (if requested) will also be populated once
1200 // imports have been resolved. When ld.work goes idle, all transitive imports of
1201 // the requested package (and its test, if requested) will have been loaded.
1202 func (ld *loader) pkg(ctx context.Context, path string, flags loadPkgFlags) *loadPkg {
1203 if flags.has(pkgImportsLoaded) {
1204 panic("internal error: (*loader).pkg called with pkgImportsLoaded flag set")
1207 pkg := ld.pkgCache.Do(path, func() interface{} {
1211 ld.applyPkgFlags(ctx, pkg, flags)
1213 ld.work.Add(func() { ld.load(ctx, pkg) })
1217 ld.applyPkgFlags(ctx, pkg, flags)
1221 // applyPkgFlags updates pkg.flags to set the given flags and propagate the
1222 // (transitive) effects of those flags, possibly loading or enqueueing further
1223 // packages as a result.
1224 func (ld *loader) applyPkgFlags(ctx context.Context, pkg *loadPkg, flags loadPkgFlags) {
1229 if flags.has(pkgInAll) && ld.allPatternIsRoot && !pkg.isTest() {
1230 // This package matches a root pattern by virtue of being in "all".
1234 old := pkg.flags.update(flags)
1236 if new == old || !new.has(pkgImportsLoaded) {
1237 // We either didn't change the state of pkg, or we don't know anything about
1238 // its dependencies yet. Either way, we can't usefully load its test or
1239 // update its dependencies.
1244 // Check whether we should add (or update the flags for) a test for pkg.
1245 // ld.pkgTest is idempotent and extra invocations are inexpensive,
1246 // so it's ok if we call it more than is strictly necessary.
1249 case ld.allPatternIsRoot && pkg.mod == Target:
1250 // We are loading the "all" pattern, which includes packages imported by
1251 // tests in the main module. This package is in the main module, so we
1252 // need to identify the imports of its test even if LoadTests is not set.
1254 // (We will filter out the extra tests explicitly in computePatternAll.)
1257 case ld.allPatternIsRoot && ld.allClosesOverTests && new.has(pkgInAll):
1258 // This variant of the "all" pattern includes imports of tests of every
1259 // package that is itself in "all", and pkg is in "all", so its test is
1260 // also in "all" (as above).
1263 case ld.LoadTests && new.has(pkgIsRoot):
1264 // LoadTest explicitly requests tests of “the root packages”.
1269 var testFlags loadPkgFlags
1270 if pkg.mod == Target || (ld.allClosesOverTests && new.has(pkgInAll)) {
1271 // Tests of packages in the main module are in "all", in the sense that
1272 // they cause the packages they import to also be in "all". So are tests
1273 // of packages in "all" if "all" closes over test dependencies.
1274 testFlags |= pkgInAll
1276 ld.pkgTest(ctx, pkg, testFlags)
1280 if new.has(pkgInAll) && !old.has(pkgInAll|pkgImportsLoaded) {
1281 // We have just marked pkg with pkgInAll, or we have just loaded its
1282 // imports, or both. Now is the time to propagate pkgInAll to the imports.
1283 for _, dep := range pkg.imports {
1284 ld.applyPkgFlags(ctx, dep, pkgInAll)
1289 // load loads an individual package.
1290 func (ld *loader) load(ctx context.Context, pkg *loadPkg) {
1291 if strings.Contains(pkg.path, "@") {
1292 // Leave for error during load.
1295 if build.IsLocalImport(pkg.path) || filepath.IsAbs(pkg.path) {
1296 // Leave for error during load.
1297 // (Module mode does not allow local imports.)
1301 if search.IsMetaPackage(pkg.path) {
1302 pkg.err = &invalidImportError{
1303 importPath: pkg.path,
1304 err: fmt.Errorf("%q is not an importable package; see 'go help packages'", pkg.path),
1309 pkg.mod, pkg.dir, pkg.err = importFromModules(ctx, pkg.path, ld.requirements)
1313 if pkg.mod == Target {
1314 // Go ahead and mark pkg as in "all". This provides the invariant that a
1315 // package that is *only* imported by other packages in "all" is always
1316 // marked as such before loading its imports.
1318 // We don't actually rely on that invariant at the moment, but it may
1319 // improve efficiency somewhat and makes the behavior a bit easier to reason
1320 // about (by reducing churn on the flag bits of dependencies), and costs
1321 // essentially nothing (these atomic flag ops are essentially free compared
1322 // to scanning source code for imports).
1323 ld.applyPkgFlags(ctx, pkg, pkgInAll)
1325 if ld.AllowPackage != nil {
1326 if err := ld.AllowPackage(ctx, pkg.path, pkg.mod); err != nil {
1331 pkg.inStd = (search.IsStandardImportPath(pkg.path) && search.InDir(pkg.dir, cfg.GOROOTsrc) != "")
1333 var imports, testImports []string
1335 if cfg.BuildContext.Compiler == "gccgo" && pkg.inStd {
1336 // We can't scan standard packages for gccgo.
1339 imports, testImports, err = scanDir(pkg.dir, ld.Tags)
1346 pkg.imports = make([]*loadPkg, 0, len(imports))
1347 var importFlags loadPkgFlags
1348 if pkg.flags.has(pkgInAll) {
1349 importFlags = pkgInAll
1351 for _, path := range imports {
1353 // Imports from packages in "std" and "cmd" should resolve using
1354 // GOROOT/src/vendor even when "std" is not the main module.
1355 path = ld.stdVendor(pkg.path, path)
1357 pkg.imports = append(pkg.imports, ld.pkg(ctx, path, importFlags))
1359 pkg.testImports = testImports
1361 ld.applyPkgFlags(ctx, pkg, pkgImportsLoaded)
1364 // pkgTest locates the test of pkg, creating it if needed, and updates its state
1365 // to reflect the given flags.
1367 // pkgTest requires that the imports of pkg have already been loaded (flagged
1368 // with pkgImportsLoaded).
1369 func (ld *loader) pkgTest(ctx context.Context, pkg *loadPkg, testFlags loadPkgFlags) *loadPkg {
1371 panic("pkgTest called on a test package")
1374 createdTest := false
1375 pkg.testOnce.Do(func() {
1376 pkg.test = &loadPkg{
1384 ld.applyPkgFlags(ctx, pkg.test, testFlags)
1390 test.imports = make([]*loadPkg, 0, len(pkg.testImports))
1391 var importFlags loadPkgFlags
1392 if test.flags.has(pkgInAll) {
1393 importFlags = pkgInAll
1395 for _, path := range pkg.testImports {
1397 path = ld.stdVendor(test.path, path)
1399 test.imports = append(test.imports, ld.pkg(ctx, path, importFlags))
1401 pkg.testImports = nil
1402 ld.applyPkgFlags(ctx, test, pkgImportsLoaded)
1404 ld.applyPkgFlags(ctx, test, testFlags)
1410 // stdVendor returns the canonical import path for the package with the given
1411 // path when imported from the standard-library package at parentPath.
1412 func (ld *loader) stdVendor(parentPath, path string) string {
1413 if search.IsStandardImportPath(path) {
1417 if str.HasPathPrefix(parentPath, "cmd") {
1418 if !ld.VendorModulesInGOROOTSrc || Target.Path != "cmd" {
1419 vendorPath := pathpkg.Join("cmd", "vendor", path)
1420 if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
1424 } else if !ld.VendorModulesInGOROOTSrc || Target.Path != "std" || str.HasPathPrefix(parentPath, "vendor") {
1425 // If we are outside of the 'std' module, resolve imports from within 'std'
1426 // to the vendor directory.
1428 // Do the same for importers beginning with the prefix 'vendor/' even if we
1429 // are *inside* of the 'std' module: the 'vendor/' packages that resolve
1430 // globally from GOROOT/src/vendor (and are listed as part of 'go list std')
1431 // are distinct from the real module dependencies, and cannot import
1432 // internal packages from the real module.
1434 // (Note that although the 'vendor/' packages match the 'std' *package*
1435 // pattern, they are not part of the std *module*, and do not affect
1436 // 'go mod tidy' and similar module commands when working within std.)
1437 vendorPath := pathpkg.Join("vendor", path)
1438 if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
1443 // Not vendored: resolve from modules.
1447 // computePatternAll returns the list of packages matching pattern "all",
1448 // starting with a list of the import paths for the packages in the main module.
1449 func (ld *loader) computePatternAll() (all []string) {
1450 for _, pkg := range ld.pkgs {
1451 if pkg.flags.has(pkgInAll) && !pkg.isTest() {
1452 all = append(all, pkg.path)
1459 // checkMultiplePaths verifies that a given module path is used as itself
1460 // or as a replacement for another module, but not both at the same time.
1462 // (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
1463 func (ld *loader) checkMultiplePaths() {
1464 mods := ld.requirements.rootModules
1465 if cached := ld.requirements.graph.Load(); cached != nil {
1466 if mg := cached.(cachedGraph).mg; mg != nil {
1467 mods = mg.BuildList()
1471 firstPath := map[module.Version]string{}
1472 for _, mod := range mods {
1473 src := resolveReplacement(mod)
1474 if prev, ok := firstPath[src]; !ok {
1475 firstPath[src] = mod.Path
1476 } else if prev != mod.Path {
1477 ld.errorf("go: %s@%s used for two different module paths (%s and %s)", src.Path, src.Version, prev, mod.Path)
1482 // scanDir is like imports.ScanDir but elides known magic imports from the list,
1483 // so that we do not go looking for packages that don't really exist.
1485 // The standard magic import is "C", for cgo.
1487 // The only other known magic imports are appengine and appengine/*.
1488 // These are so old that they predate "go get" and did not use URL-like paths.
1489 // Most code today now uses google.golang.org/appengine instead,
1490 // but not all code has been so updated. When we mostly ignore build tags
1491 // during "go vendor", we look into "// +build appengine" files and
1492 // may see these legacy imports. We drop them so that the module
1493 // search does not look for modules to try to satisfy them.
1494 func scanDir(dir string, tags map[string]bool) (imports_, testImports []string, err error) {
1495 imports_, testImports, err = imports.ScanDir(dir, tags)
1497 filter := func(x []string) []string {
1499 for _, pkg := range x {
1500 if pkg != "C" && pkg != "appengine" && !strings.HasPrefix(pkg, "appengine/") &&
1501 pkg != "appengine_internal" && !strings.HasPrefix(pkg, "appengine_internal/") {
1509 return filter(imports_), filter(testImports), err
1512 // buildStacks computes minimal import stacks for each package,
1513 // for use in error messages. When it completes, packages that
1514 // are part of the original root set have pkg.stack == nil,
1515 // and other packages have pkg.stack pointing at the next
1516 // package up the import stack in their minimal chain.
1517 // As a side effect, buildStacks also constructs ld.pkgs,
1518 // the list of all packages loaded.
1519 func (ld *loader) buildStacks() {
1520 if len(ld.pkgs) > 0 {
1521 panic("buildStacks")
1523 for _, pkg := range ld.roots {
1524 pkg.stack = pkg // sentinel to avoid processing in next loop
1525 ld.pkgs = append(ld.pkgs, pkg)
1527 for i := 0; i < len(ld.pkgs); i++ { // not range: appending to ld.pkgs in loop
1529 for _, next := range pkg.imports {
1530 if next.stack == nil {
1532 ld.pkgs = append(ld.pkgs, next)
1535 if next := pkg.test; next != nil && next.stack == nil {
1537 ld.pkgs = append(ld.pkgs, next)
1540 for _, pkg := range ld.roots {
1545 // stackText builds the import stack text to use when
1546 // reporting an error in pkg. It has the general form
1551 // other2.test imports
1554 func (pkg *loadPkg) stackText() string {
1555 var stack []*loadPkg
1556 for p := pkg; p != nil; p = p.stack {
1557 stack = append(stack, p)
1560 var buf bytes.Buffer
1561 for i := len(stack) - 1; i >= 0; i-- {
1563 fmt.Fprint(&buf, p.path)
1564 if p.testOf != nil {
1565 fmt.Fprint(&buf, ".test")
1568 if stack[i-1].testOf == p {
1569 fmt.Fprint(&buf, " tested by\n\t")
1571 fmt.Fprint(&buf, " imports\n\t")
1578 // why returns the text to use in "go mod why" output about the given package.
1579 // It is less ornate than the stackText but contains the same information.
1580 func (pkg *loadPkg) why() string {
1581 var buf strings.Builder
1582 var stack []*loadPkg
1583 for p := pkg; p != nil; p = p.stack {
1584 stack = append(stack, p)
1587 for i := len(stack) - 1; i >= 0; i-- {
1589 if p.testOf != nil {
1590 fmt.Fprintf(&buf, "%s.test\n", p.testOf.path)
1592 fmt.Fprintf(&buf, "%s\n", p.path)
1598 // Why returns the "go mod why" output stanza for the given package,
1599 // without the leading # comment.
1600 // The package graph must have been loaded already, usually by LoadPackages.
1601 // If there is no reason for the package to be in the current build,
1602 // Why returns an empty string.
1603 func Why(path string) string {
1604 pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
1611 // WhyDepth returns the number of steps in the Why listing.
1612 // If there is no reason for the package to be in the current build,
1613 // WhyDepth returns 0.
1614 func WhyDepth(path string) int {
1616 pkg, _ := loaded.pkgCache.Get(path).(*loadPkg)
1617 for p := pkg; p != nil; p = p.stack {