]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/go/internal/modload/load.go
cmd/go/internal/modload: clean up error reporting
[gostls13.git] / src / cmd / go / internal / modload / load.go
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package modload
6
7 // This file contains the module-mode package loader, as well as some accessory
8 // functions pertaining to the package import graph.
9 //
10 // There are two exported entry points into package loading — LoadPackages and
11 // ImportFromFiles — both implemented in terms of loadFromRoots, which itself
12 // manipulates an instance of the loader struct.
13 //
14 // Although most of the loading state is maintained in the loader struct,
15 // one key piece - the build list - is a global, so that it can be modified
16 // separate from the loading operation, such as during "go get"
17 // upgrades/downgrades or in "go mod" operations.
18 // TODO(#40775): It might be nice to make the loader take and return
19 // a buildList rather than hard-coding use of the global.
20 //
21 // Loading is an iterative process. On each iteration, we try to load the
22 // requested packages and their transitive imports, then try to resolve modules
23 // for any imported packages that are still missing.
24 //
25 // The first step of each iteration identifies a set of “root” packages.
26 // Normally the root packages are exactly those matching the named pattern
27 // arguments. However, for the "all" meta-pattern, the final set of packages is
28 // computed from the package import graph, and therefore cannot be an initial
29 // input to loading that graph. Instead, the root packages for the "all" pattern
30 // are those contained in the main module, and allPatternIsRoot parameter to the
31 // loader instructs it to dynamically expand those roots to the full "all"
32 // pattern as loading progresses.
33 //
34 // The pkgInAll flag on each loadPkg instance tracks whether that
35 // package is known to match the "all" meta-pattern.
36 // A package matches the "all" pattern if:
37 //      - it is in the main module, or
38 //      - it is imported by any test in the main module, or
39 //      - it is imported by 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 //
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.
132 var loaded *loader
133
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().
139         Tags map[string]bool
140
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
143         // packages.
144         Tidy bool
145
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
150
151         // ResolveMissingImports indicates that we should attempt to add module
152         // dependencies as needed to resolve imports of packages that are not found.
153         //
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
157
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
161         // loaded.
162         //
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
166
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.
170         LoadTests bool
171
172         // UseVendorAll causes the "all" package pattern to be interpreted as if
173         // running "go mod vendor" (or building with "-mod=vendor").
174         //
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.
177         UseVendorAll bool
178
179         // AllowErrors indicates that LoadPackages should not terminate the process if
180         // an error occurs.
181         AllowErrors bool
182
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.
186         //
187         // Errors encountered in the module graph will still be reported.
188         //
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
193
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
197         // future version).
198         SilenceMissingStdImports bool
199
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.
204         //
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
208
209         // SilenceUnmatchedWarnings suppresses the warnings normally emitted for
210         // patterns that did not match any packages.
211         SilenceUnmatchedWarnings bool
212 }
213
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()
219         }
220
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
228                 }
229         }
230
231         updateMatches := func(rs *Requirements, ld *loader) {
232                 for _, m := range matches {
233                         switch {
234                         case m.IsLocal():
235                                 // Evaluate list of file system directories on first iteration.
236                                 if m.Dirs == nil {
237                                         matchLocalDirs(ctx, m, rs)
238                                 }
239
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.
246                                 m.Pkgs = m.Pkgs[:0]
247                                 for _, dir := range m.Dirs {
248                                         pkg, err := resolveLocalPackage(ctx, dir, rs)
249                                         if err != nil {
250                                                 if !m.IsLiteral() && (err == errPkgIsBuiltin || err == errPkgIsGorootSrc) {
251                                                         continue // Don't include "builtin" or GOROOT/src in wildcard patterns.
252                                                 }
253
254                                                 // If we're outside of a module, ensure that the failure mode
255                                                 // indicates that.
256                                                 ModRoot()
257
258                                                 if ld != nil {
259                                                         m.AddError(err)
260                                                 }
261                                                 continue
262                                         }
263                                         m.Pkgs = append(m.Pkgs, pkg)
264                                 }
265
266                         case m.IsLiteral():
267                                 m.Pkgs = []string{m.Pattern()}
268
269                         case strings.Contains(m.Pattern(), "..."):
270                                 m.Errs = m.Errs[:0]
271                                 mg, err := rs.Graph(ctx)
272                                 if err != nil {
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)
280                                 }
281                                 matchPackages(ctx, m, opts.Tags, includeStd, mg.BuildList())
282
283                         case m.Pattern() == "all":
284                                 if ld == nil {
285                                         // The initial roots are the packages in the main module.
286                                         // loadFromRoots will expand that to "all".
287                                         m.Errs = m.Errs[:0]
288                                         matchPackages(ctx, m, opts.Tags, omitStd, []module.Version{Target})
289                                 } else {
290                                         // Starting with the packages in the main module,
291                                         // enumerate the full list of "all".
292                                         m.Pkgs = ld.computePatternAll()
293                                 }
294
295                         case m.Pattern() == "std" || m.Pattern() == "cmd":
296                                 if m.Pkgs == nil {
297                                         m.MatchPackages() // Locate the packages within GOROOT/src.
298                                 }
299
300                         default:
301                                 panic(fmt.Sprintf("internal error: modload missing case for pattern %s", m.Pattern()))
302                         }
303                 }
304         }
305
306         initialRS, _ := loadModFile(ctx) // Ignore needCommit — we're going to commit at the end regardless.
307
308         ld := loadFromRoots(ctx, loaderParams{
309                 PackageOpts:  opts,
310                 requirements: initialRS,
311
312                 allClosesOverTests: index.allPatternClosesOverTests() && !opts.UseVendorAll,
313                 allPatternIsRoot:   allPatternIsRoot,
314
315                 listRoots: func(rs *Requirements) (roots []string) {
316                         updateMatches(rs, nil)
317                         for _, m := range matches {
318                                 roots = append(roots, m.Pkgs...)
319                         }
320                         return roots
321                 },
322         })
323
324         // One last pass to finalize wildcards.
325         updateMatches(ld.requirements, ld)
326
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)
333                         }
334                 }
335         }
336         base.ExitIfErrors()
337
338         if !opts.SilenceUnmatchedWarnings {
339                 search.WarnUnmatched(matches)
340         }
341
342         if opts.Tidy {
343                 if cfg.BuildV {
344                         mg, _ := ld.requirements.Graph(ctx)
345
346                         for _, m := range initialRS.rootModules {
347                                 var unused bool
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"
353                                 } else {
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)
358                                         unused = !ok
359                                 }
360                                 if unused {
361                                         fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
362                                 }
363                         }
364                 }
365
366                 modfetch.TrimGoSum(keepSums(ctx, ld, ld.requirements, loadedZipSumsOnly))
367         }
368
369         // Success! Update go.mod and go.sum (if needed) and return the results.
370         loaded = ld
371         commitRequirements(ctx, loaded.requirements)
372
373         for _, pkg := range ld.pkgs {
374                 if !pkg.isTest() {
375                         loadedPackages = append(loadedPackages, pkg.path)
376                 }
377         }
378         sort.Strings(loadedPackages)
379         return matches, loadedPackages
380 }
381
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) {
385         if !m.IsLocal() {
386                 panic(fmt.Sprintf("internal error: resolveLocalDirs on non-local pattern %s", m.Pattern()))
387         }
388
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]))
396                 absDir := dir
397                 if !filepath.IsAbs(dir) {
398                         absDir = filepath.Join(base.Cwd, dir)
399                 }
400                 if search.InDir(absDir, cfg.GOROOTsrc) == "" && search.InDir(absDir, ModRoot()) == "" && pathInModuleCache(ctx, absDir, rs) == "" {
401                         m.Dirs = []string{}
402                         m.AddError(fmt.Errorf("directory prefix %s outside available modules", base.ShortPath(absDir)))
403                         return
404                 }
405         }
406
407         m.MatchDirs()
408 }
409
410 // resolveLocalPackage resolves a filesystem path to a package path.
411 func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (string, error) {
412         var absDir string
413         if filepath.IsAbs(dir) {
414                 absDir = filepath.Clean(dir)
415         } else {
416                 absDir = filepath.Join(base.Cwd, dir)
417         }
418
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
423                 // for that package.
424                 //
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}
433                         }
434                         return "", err
435                 }
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
440                         // module.
441                         //
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).
445                         return "", err
446                 }
447         }
448
449         if modRoot != "" && absDir == modRoot {
450                 if absDir == cfg.GOROOTsrc {
451                         return "", errPkgIsGorootSrc
452                 }
453                 return targetPrefix, nil
454         }
455
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)
464                         }
465
466                         readVendorList()
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)
470                         }
471                         return pkg, nil
472                 }
473
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
481                         }
482                         return pkg, nil
483                 }
484
485                 pkg := targetPrefix + suffix
486                 if _, ok, err := dirInModule(pkg, targetPrefix, modRoot, true); err != nil {
487                         return "", err
488                 } else if !ok {
489                         return "", &PackageNotInModuleError{Mod: Target, Pattern: pkg}
490                 }
491                 return pkg, nil
492         }
493
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
498                 }
499                 return pkg, nil
500         }
501
502         pkg := pathInModuleCache(ctx, absDir, rs)
503         if pkg == "" {
504                 return "", fmt.Errorf("directory %s outside available modules", base.ShortPath(absDir))
505         }
506         return pkg, nil
507 }
508
509 var (
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`)
513 )
514
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) {
519                 var root string
520                 var err error
521                 if repl := Replacement(m); repl.Path != "" && repl.Version == "" {
522                         root = repl.Path
523                         if !filepath.IsAbs(root) {
524                                 root = filepath.Join(ModRoot(), root)
525                         }
526                 } else if repl.Path != "" {
527                         root, err = modfetch.DownloadDir(repl)
528                 } else {
529                         root, err = modfetch.DownloadDir(m)
530                 }
531                 if err != nil {
532                         return "", false
533                 }
534
535                 sub := search.InDir(dir, root)
536                 if sub == "" {
537                         return "", false
538                 }
539                 sub = filepath.ToSlash(sub)
540                 if strings.Contains(sub, "/vendor/") || strings.HasPrefix(sub, "vendor/") || strings.Contains(sub, "@") {
541                         return "", false
542                 }
543
544                 return path.Join(m.Path, filepath.ToSlash(sub)), true
545         }
546
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.
551                         }
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.
555                                 return importPath
556                         }
557                 }
558         }
559
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.
563         //
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.
567
568         mg, _ := rs.Graph(ctx)
569         var importPath string
570         for _, m := range mg.BuildList() {
571                 var found bool
572                 importPath, found = tryMod(m)
573                 if found {
574                         break
575                 }
576         }
577         return importPath
578 }
579
580 // ImportFromFiles adds modules to the build list as needed
581 // to satisfy the imports in the named Go source files.
582 //
583 // Errors in missing dependencies are silenced.
584 //
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)
589
590         tags := imports.Tags()
591         imports, testImports, err := imports.ScanFiles(gofiles, tags)
592         if err != nil {
593                 base.Fatalf("go: %v", err)
594         }
595
596         loaded = loadFromRoots(ctx, loaderParams{
597                 PackageOpts: PackageOpts{
598                         Tags:                  tags,
599                         ResolveMissingImports: true,
600                         SilencePackageErrors:  true,
601                 },
602                 requirements:       rs,
603                 allClosesOverTests: index.allPatternClosesOverTests(),
604                 listRoots: func(*Requirements) (roots []string) {
605                         roots = append(roots, imports...)
606                         roots = append(roots, testImports...)
607                         return roots
608                 },
609         })
610         commitRequirements(ctx, loaded.requirements)
611 }
612
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 {
616         if !HasModRoot() {
617                 return "."
618         }
619         LoadModFile(ctx) // Sets targetPrefix.
620
621         if !filepath.IsAbs(dir) {
622                 dir = filepath.Join(base.Cwd, dir)
623         } else {
624                 dir = filepath.Clean(dir)
625         }
626
627         if dir == modRoot {
628                 return targetPrefix
629         }
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/")
634                 }
635                 return targetPrefix + suffix
636         }
637         return "."
638 }
639
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.
648
649         m := search.NewMatch(pattern)
650         matchPackages(ctx, m, imports.AnyTags(), omitStd, []module.Version{Target})
651         return m
652 }
653
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)
660         if !ok {
661                 return ""
662         }
663         return pkg.path
664 }
665
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)
670         if !ok {
671                 return ""
672         }
673         return pkg.dir
674 }
675
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)
679         if !ok {
680                 return module.Version{}
681         }
682         return pkg.mod
683 }
684
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)
692         if !ok {
693                 return nil, nil
694         }
695         imports = make([]string, len(pkg.imports))
696         for i, p := range pkg.imports {
697                 imports[i] = p.path
698         }
699         if pkg.test != nil {
700                 testImports = make([]string, len(pkg.test.imports))
701                 for i, p := range pkg.test.imports {
702                         testImports[i] = p.path
703                 }
704         }
705         return imports, testImports
706 }
707
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
711 // been called.
712 func Lookup(parentPath string, parentIsStd bool, path string) (dir, realPath string, err error) {
713         if path == "" {
714                 panic("Lookup called with empty package path")
715         }
716
717         if parentIsStd {
718                 path = loaded.stdVendor(parentPath, path)
719         }
720         pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
721         if !ok {
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)
731                 if dir != "" {
732                         return dir, path, nil
733                 }
734                 return "", "", errMissing
735         }
736         return pkg.dir, pkg.path, pkg.err
737 }
738
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.
743 type loader struct {
744         loaderParams
745
746         work *par.Queue
747
748         // reset on each iteration
749         roots    []*loadPkg
750         pkgCache *par.Cache // package path (string) → *loadPkg
751         pkgs     []*loadPkg // transitive closure of loaded packages and tests; populated in buildStacks
752 }
753
754 // loaderParams configure the packages loaded by, and the properties reported
755 // by, a loader instance.
756 type loaderParams struct {
757         PackageOpts
758         requirements *Requirements
759
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?
762
763         listRoots func(rs *Requirements) []string
764 }
765
766 func (ld *loader) reset() {
767         select {
768         case <-ld.work.Idle():
769         default:
770                 panic("loader.reset when not idle")
771         }
772
773         ld.roots = nil
774         ld.pkgCache = new(par.Cache)
775         ld.pkgs = nil
776 }
777
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{}) {
781         if ld.AllowErrors {
782                 fmt.Fprintf(os.Stderr, format, args...)
783         } else {
784                 base.Errorf(format, args...)
785         }
786 }
787
788 // A loadPkg records information about a single loaded package.
789 type loadPkg struct {
790         // Populated at construction time:
791         path   string // import path
792         testOf *loadPkg
793
794         // Populated at construction time and updated by (*loader).applyPkgFlags:
795         flags atomicLoadPkgFlags
796
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.
803         inStd       bool
804
805         // Populated by (*loader).pkgTest:
806         testOnce sync.Once
807         test     *loadPkg
808
809         // Populated by postprocessing in (*loader).buildStacks:
810         stack *loadPkg // package importing this one in minimal import stack for this pkg
811 }
812
813 // loadPkgFlags is a set of flags tracking metadata about a package.
814 type loadPkgFlags int8
815
816 const (
817         // pkgInAll indicates that the package is in the "all" package pattern,
818         // regardless of whether we are loading the "all" package pattern.
819         //
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.
823         //
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
828
829         // pkgIsRoot indicates that the package matches one of the root package
830         // patterns requested by the caller.
831         //
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).
835         //
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).
838         pkgIsRoot
839
840         // pkgImportsLoaded indicates that the imports and testImports fields of a
841         // loadPkg have been populated.
842         pkgImportsLoaded
843 )
844
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
848 }
849
850 // An atomicLoadPkgFlags stores a loadPkgFlags for which individual flags can be
851 // added atomically.
852 type atomicLoadPkgFlags struct {
853         bits int32
854 }
855
856 // update sets the given flags in af (in addition to any flags already set).
857 //
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) {
861         for {
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)
866                 }
867         }
868 }
869
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
873 }
874
875 // isTest reports whether pkg is a test of another package.
876 func (pkg *loadPkg) isTest() bool {
877         return pkg.testOf != nil
878 }
879
880 // fromExternalModule reports whether pkg was loaded from a module other than
881 // the main module.
882 func (pkg *loadPkg) fromExternalModule() bool {
883         if pkg.mod.Path == "" {
884                 return false // loaded from the standard library, not a module
885         }
886         if pkg.mod.Path == Target.Path {
887                 return false // loaded from the main module.
888         }
889         return true
890 }
891
892 var errMissing = errors.New("cannot find package")
893
894 // loadFromRoots attempts to load the build graph needed to process a set of
895 // root packages and their dependencies.
896 //
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)
899 // as needed.
900 func loadFromRoots(ctx context.Context, params loaderParams) *loader {
901         ld := &loader{
902                 loaderParams: params,
903                 work:         par.NewQueue(runtime.GOMAXPROCS(0)),
904         }
905
906         if ld.requirements.depth == eager {
907                 var err error
908                 ld.requirements, _, err = expandGraph(ctx, ld.requirements)
909                 if err != nil {
910                         ld.errorf("go: %v\n", err)
911                 }
912         }
913
914         for {
915                 ld.reset()
916
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)
922
923                 if go117LazyTODO {
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.
930                 }
931
932                 inRoots := map[*loadPkg]bool{}
933                 for _, path := range rootPkgs {
934                         root := ld.pkg(ctx, path, pkgIsRoot)
935                         if !inRoots[root] {
936                                 ld.roots = append(ld.roots, root)
937                                 inRoots[root] = true
938                         }
939                 }
940
941                 // ld.pkg adds imported packages to the work queue and calls applyPkgFlags,
942                 // which adds tests (and test dependencies) as needed.
943                 //
944                 // When all of the work in the queue has completed, we'll know that the
945                 // transitive closure of dependencies has been loaded.
946                 <-ld.work.Idle()
947
948                 ld.buildStacks()
949
950                 if !ld.ResolveMissingImports || (!HasModRoot() && !allowMissingModuleImports) {
951                         // We've loaded as much as we can without resolving missing imports.
952                         break
953                 }
954                 modAddedBy := ld.resolveMissingImports(ctx)
955                 if len(modAddedBy) == 0 {
956                         break
957                 }
958
959                 toAdd := make([]module.Version, 0, len(modAddedBy))
960                 for m, _ := range modAddedBy {
961                         toAdd = append(toAdd, m)
962                 }
963                 module.Sort(toAdd) // to make errors deterministic
964
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)
973                                 }
974                         }
975                         base.Fatalf("go: %v", err)
976                 }
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
980                         // effect.
981                         panic(fmt.Sprintf("internal error: adding %v to module graph had no effect on root requirements (%v)", toAdd, prevRS.rootModules))
982                 }
983         }
984         base.ExitIfErrors() // TODO(bcmills): Is this actually needed?
985
986         if err := ld.updateRequirements(ctx, nil); err != nil {
987                 base.Fatalf("go: %v", err)
988         }
989
990         if go117LazyTODO {
991                 // Promoting a root can pull in previously-irrelevant requirements,
992                 // changing the build list. Iterate until the roots are stable.
993         }
994
995         // Tidy the build list, if applicable, before we report errors.
996         // (The process of tidying may remove errors from irrelevant dependencies.)
997         if ld.Tidy {
998                 var err error
999                 ld.requirements, err = tidyRoots(ctx, ld.requirements, ld.pkgs)
1000                 if err != nil {
1001                         ld.errorf("go: %v\n", err)
1002                 }
1003         }
1004
1005         // Report errors, if any.
1006         for _, pkg := range ld.pkgs {
1007                 if pkg.err == nil {
1008                         continue
1009                 }
1010
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
1017                         }
1018                 }
1019
1020                 if ld.SilencePackageErrors {
1021                         continue
1022                 }
1023                 if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) &&
1024                         stdErr.isStd && ld.SilenceMissingStdImports {
1025                         continue
1026                 }
1027                 if ld.SilenceNoGoErrors && errors.Is(pkg.err, imports.ErrNoGo) {
1028                         continue
1029                 }
1030
1031                 ld.errorf("%s: %v\n", pkg.stackText(), pkg.err)
1032         }
1033
1034         ld.checkMultiplePaths()
1035         return ld
1036 }
1037
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.
1041 //
1042 // In particular:
1043 //
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.
1048 //
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.
1052 //
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
1056
1057         // Compute directly referenced dependency modules.
1058         direct := make(map[string]bool)
1059         for _, pkg := range ld.pkgs {
1060                 if pkg.mod != Target {
1061                         continue
1062                 }
1063                 for _, dep := range pkg.imports {
1064                         if dep.mod.Path == "" || dep.mod.Path == Target.Path {
1065                                 continue
1066                         }
1067
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.
1073                                         //
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,
1081                                                 Module:       dep.mod,
1082                                         }
1083                                         // cfg.BuildMod does not allow us to change dep.mod to be a direct
1084                                         // dependency, so don't mark it as such.
1085                                         continue
1086                                 }
1087                         }
1088
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
1093                 }
1094         }
1095
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())
1101         if !loadedDirect {
1102                 for mPath := range rs.direct {
1103                         direct[mPath] = true
1104                 }
1105         }
1106
1107         rs, err := updateRoots(ctx, direct, rs, add)
1108         if err != nil {
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
1111                 return err
1112         }
1113         if rs != ld.requirements {
1114                 if _, err := rs.Graph(ctx); err != nil {
1115                         ld.errorf("go: %v\n", err)
1116                 }
1117                 ld.requirements = rs
1118         }
1119         return nil
1120 }
1121
1122 // resolveMissingImports returns a set of modules that could be added as
1123 // dependencies in order to resolve missing packages from pkgs.
1124 //
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 {
1130                 pkg *loadPkg
1131                 mod *module.Version
1132         }
1133         var pkgMods []pkgMod
1134         for _, pkg := range ld.pkgs {
1135                 if pkg.err == nil {
1136                         continue
1137                 }
1138                 if pkg.isTest() {
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.
1141                         continue
1142                 }
1143                 if !errors.As(pkg.err, new(*ImportMissingError)) {
1144                         // Leave other errors for Import or load.Packages to report.
1145                         continue
1146                 }
1147
1148                 pkg := pkg
1149                 var mod module.Version
1150                 ld.work.Add(func() {
1151                         var err error
1152                         mod, err = queryImport(ctx, pkg.path, ld.requirements)
1153                         if err != nil {
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.
1159                                 pkg.err = err
1160                         }
1161
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.
1167                         //
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.
1173                 })
1174
1175                 pkgMods = append(pkgMods, pkgMod{pkg: pkg, mod: &mod})
1176         }
1177         <-ld.work.Idle()
1178
1179         modAddedBy = map[module.Version]*loadPkg{}
1180         for _, pm := range pkgMods {
1181                 pkg, mod := pm.pkg, *pm.mod
1182                 if mod.Path == "" {
1183                         continue
1184                 }
1185
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
1189                 }
1190         }
1191
1192         return modAddedBy
1193 }
1194
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.
1197 //
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")
1205         }
1206
1207         pkg := ld.pkgCache.Do(path, func() interface{} {
1208                 pkg := &loadPkg{
1209                         path: path,
1210                 }
1211                 ld.applyPkgFlags(ctx, pkg, flags)
1212
1213                 ld.work.Add(func() { ld.load(ctx, pkg) })
1214                 return pkg
1215         }).(*loadPkg)
1216
1217         ld.applyPkgFlags(ctx, pkg, flags)
1218         return pkg
1219 }
1220
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) {
1225         if flags == 0 {
1226                 return
1227         }
1228
1229         if flags.has(pkgInAll) && ld.allPatternIsRoot && !pkg.isTest() {
1230                 // This package matches a root pattern by virtue of being in "all".
1231                 flags |= pkgIsRoot
1232         }
1233
1234         old := pkg.flags.update(flags)
1235         new := old | 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.
1240                 return
1241         }
1242
1243         if !pkg.isTest() {
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.
1247                 wantTest := false
1248                 switch {
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.
1253                         //
1254                         // (We will filter out the extra tests explicitly in computePatternAll.)
1255                         wantTest = true
1256
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).
1261                         wantTest = true
1262
1263                 case ld.LoadTests && new.has(pkgIsRoot):
1264                         // LoadTest explicitly requests tests of “the root packages”.
1265                         wantTest = true
1266                 }
1267
1268                 if wantTest {
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
1275                         }
1276                         ld.pkgTest(ctx, pkg, testFlags)
1277                 }
1278         }
1279
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)
1285                 }
1286         }
1287 }
1288
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.
1293                 return
1294         }
1295         if build.IsLocalImport(pkg.path) || filepath.IsAbs(pkg.path) {
1296                 // Leave for error during load.
1297                 // (Module mode does not allow local imports.)
1298                 return
1299         }
1300
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),
1305                 }
1306                 return
1307         }
1308
1309         pkg.mod, pkg.dir, pkg.err = importFromModules(ctx, pkg.path, ld.requirements)
1310         if pkg.dir == "" {
1311                 return
1312         }
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.
1317                 //
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)
1324         }
1325         if ld.AllowPackage != nil {
1326                 if err := ld.AllowPackage(ctx, pkg.path, pkg.mod); err != nil {
1327                         pkg.err = err
1328                 }
1329         }
1330
1331         pkg.inStd = (search.IsStandardImportPath(pkg.path) && search.InDir(pkg.dir, cfg.GOROOTsrc) != "")
1332
1333         var imports, testImports []string
1334
1335         if cfg.BuildContext.Compiler == "gccgo" && pkg.inStd {
1336                 // We can't scan standard packages for gccgo.
1337         } else {
1338                 var err error
1339                 imports, testImports, err = scanDir(pkg.dir, ld.Tags)
1340                 if err != nil {
1341                         pkg.err = err
1342                         return
1343                 }
1344         }
1345
1346         pkg.imports = make([]*loadPkg, 0, len(imports))
1347         var importFlags loadPkgFlags
1348         if pkg.flags.has(pkgInAll) {
1349                 importFlags = pkgInAll
1350         }
1351         for _, path := range imports {
1352                 if pkg.inStd {
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)
1356                 }
1357                 pkg.imports = append(pkg.imports, ld.pkg(ctx, path, importFlags))
1358         }
1359         pkg.testImports = testImports
1360
1361         ld.applyPkgFlags(ctx, pkg, pkgImportsLoaded)
1362 }
1363
1364 // pkgTest locates the test of pkg, creating it if needed, and updates its state
1365 // to reflect the given flags.
1366 //
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 {
1370         if pkg.isTest() {
1371                 panic("pkgTest called on a test package")
1372         }
1373
1374         createdTest := false
1375         pkg.testOnce.Do(func() {
1376                 pkg.test = &loadPkg{
1377                         path:   pkg.path,
1378                         testOf: pkg,
1379                         mod:    pkg.mod,
1380                         dir:    pkg.dir,
1381                         err:    pkg.err,
1382                         inStd:  pkg.inStd,
1383                 }
1384                 ld.applyPkgFlags(ctx, pkg.test, testFlags)
1385                 createdTest = true
1386         })
1387
1388         test := pkg.test
1389         if createdTest {
1390                 test.imports = make([]*loadPkg, 0, len(pkg.testImports))
1391                 var importFlags loadPkgFlags
1392                 if test.flags.has(pkgInAll) {
1393                         importFlags = pkgInAll
1394                 }
1395                 for _, path := range pkg.testImports {
1396                         if pkg.inStd {
1397                                 path = ld.stdVendor(test.path, path)
1398                         }
1399                         test.imports = append(test.imports, ld.pkg(ctx, path, importFlags))
1400                 }
1401                 pkg.testImports = nil
1402                 ld.applyPkgFlags(ctx, test, pkgImportsLoaded)
1403         } else {
1404                 ld.applyPkgFlags(ctx, test, testFlags)
1405         }
1406
1407         return test
1408 }
1409
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) {
1414                 return path
1415         }
1416
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 {
1421                                 return vendorPath
1422                         }
1423                 }
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.
1427                 //
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.
1433                 //
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 {
1439                         return vendorPath
1440                 }
1441         }
1442
1443         // Not vendored: resolve from modules.
1444         return path
1445 }
1446
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)
1453                 }
1454         }
1455         sort.Strings(all)
1456         return all
1457 }
1458
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.
1461 //
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()
1468                 }
1469         }
1470
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)
1478                 }
1479         }
1480 }
1481
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.
1484 //
1485 // The standard magic import is "C", for cgo.
1486 //
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)
1496
1497         filter := func(x []string) []string {
1498                 w := 0
1499                 for _, pkg := range x {
1500                         if pkg != "C" && pkg != "appengine" && !strings.HasPrefix(pkg, "appengine/") &&
1501                                 pkg != "appengine_internal" && !strings.HasPrefix(pkg, "appengine_internal/") {
1502                                 x[w] = pkg
1503                                 w++
1504                         }
1505                 }
1506                 return x[:w]
1507         }
1508
1509         return filter(imports_), filter(testImports), err
1510 }
1511
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")
1522         }
1523         for _, pkg := range ld.roots {
1524                 pkg.stack = pkg // sentinel to avoid processing in next loop
1525                 ld.pkgs = append(ld.pkgs, pkg)
1526         }
1527         for i := 0; i < len(ld.pkgs); i++ { // not range: appending to ld.pkgs in loop
1528                 pkg := ld.pkgs[i]
1529                 for _, next := range pkg.imports {
1530                         if next.stack == nil {
1531                                 next.stack = pkg
1532                                 ld.pkgs = append(ld.pkgs, next)
1533                         }
1534                 }
1535                 if next := pkg.test; next != nil && next.stack == nil {
1536                         next.stack = pkg
1537                         ld.pkgs = append(ld.pkgs, next)
1538                 }
1539         }
1540         for _, pkg := range ld.roots {
1541                 pkg.stack = nil
1542         }
1543 }
1544
1545 // stackText builds the import stack text to use when
1546 // reporting an error in pkg. It has the general form
1547 //
1548 //      root imports
1549 //              other imports
1550 //              other2 tested by
1551 //              other2.test imports
1552 //              pkg
1553 //
1554 func (pkg *loadPkg) stackText() string {
1555         var stack []*loadPkg
1556         for p := pkg; p != nil; p = p.stack {
1557                 stack = append(stack, p)
1558         }
1559
1560         var buf bytes.Buffer
1561         for i := len(stack) - 1; i >= 0; i-- {
1562                 p := stack[i]
1563                 fmt.Fprint(&buf, p.path)
1564                 if p.testOf != nil {
1565                         fmt.Fprint(&buf, ".test")
1566                 }
1567                 if i > 0 {
1568                         if stack[i-1].testOf == p {
1569                                 fmt.Fprint(&buf, " tested by\n\t")
1570                         } else {
1571                                 fmt.Fprint(&buf, " imports\n\t")
1572                         }
1573                 }
1574         }
1575         return buf.String()
1576 }
1577
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)
1585         }
1586
1587         for i := len(stack) - 1; i >= 0; i-- {
1588                 p := stack[i]
1589                 if p.testOf != nil {
1590                         fmt.Fprintf(&buf, "%s.test\n", p.testOf.path)
1591                 } else {
1592                         fmt.Fprintf(&buf, "%s\n", p.path)
1593                 }
1594         }
1595         return buf.String()
1596 }
1597
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)
1605         if !ok {
1606                 return ""
1607         }
1608         return pkg.why()
1609 }
1610
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 {
1615         n := 0
1616         pkg, _ := loaded.pkgCache.Get(path).(*loadPkg)
1617         for p := pkg; p != nil; p = p.stack {
1618                 n++
1619         }
1620         return n
1621 }