]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/go/internal/load/pkg.go
[dev.fuzz] all: merge master (c95464f) into dev.fuzz
[gostls13.git] / src / cmd / go / internal / load / pkg.go
1 // Copyright 2011 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 load loads packages.
6 package load
7
8 import (
9         "bytes"
10         "context"
11         "encoding/json"
12         "errors"
13         "fmt"
14         "go/build"
15         "go/scanner"
16         "go/token"
17         "internal/goroot"
18         "io/fs"
19         "os"
20         "path"
21         pathpkg "path"
22         "path/filepath"
23         "runtime"
24         "sort"
25         "strconv"
26         "strings"
27         "unicode"
28         "unicode/utf8"
29
30         "cmd/go/internal/base"
31         "cmd/go/internal/cfg"
32         "cmd/go/internal/fsys"
33         "cmd/go/internal/imports"
34         "cmd/go/internal/modfetch"
35         "cmd/go/internal/modinfo"
36         "cmd/go/internal/modload"
37         "cmd/go/internal/par"
38         "cmd/go/internal/search"
39         "cmd/go/internal/str"
40         "cmd/go/internal/trace"
41         "cmd/internal/sys"
42
43         "golang.org/x/mod/modfile"
44         "golang.org/x/mod/module"
45 )
46
47 // A Package describes a single package found in a directory.
48 type Package struct {
49         PackagePublic                 // visible in 'go list'
50         Internal      PackageInternal // for use inside go command only
51 }
52
53 type PackagePublic struct {
54         // Note: These fields are part of the go command's public API.
55         // See list.go. It is okay to add fields, but not to change or
56         // remove existing ones. Keep in sync with list.go
57         Dir           string                `json:",omitempty"` // directory containing package sources
58         ImportPath    string                `json:",omitempty"` // import path of package in dir
59         ImportComment string                `json:",omitempty"` // path in import comment on package statement
60         Name          string                `json:",omitempty"` // package name
61         Doc           string                `json:",omitempty"` // package documentation string
62         Target        string                `json:",omitempty"` // installed target for this package (may be executable)
63         Shlib         string                `json:",omitempty"` // the shared library that contains this package (only set when -linkshared)
64         Root          string                `json:",omitempty"` // Go root, Go path dir, or module root dir containing this package
65         ConflictDir   string                `json:",omitempty"` // Dir is hidden by this other directory
66         ForTest       string                `json:",omitempty"` // package is only for use in named test
67         Export        string                `json:",omitempty"` // file containing export data (set by go list -export)
68         BuildID       string                `json:",omitempty"` // build ID of the compiled package (set by go list -export)
69         Module        *modinfo.ModulePublic `json:",omitempty"` // info about package's module, if any
70         Match         []string              `json:",omitempty"` // command-line patterns matching this package
71         Goroot        bool                  `json:",omitempty"` // is this package found in the Go root?
72         Standard      bool                  `json:",omitempty"` // is this package part of the standard Go library?
73         DepOnly       bool                  `json:",omitempty"` // package is only as a dependency, not explicitly listed
74         BinaryOnly    bool                  `json:",omitempty"` // package cannot be recompiled
75         Incomplete    bool                  `json:",omitempty"` // was there an error loading this package or dependencies?
76
77         // Stale and StaleReason remain here *only* for the list command.
78         // They are only initialized in preparation for list execution.
79         // The regular build determines staleness on the fly during action execution.
80         Stale       bool   `json:",omitempty"` // would 'go install' do anything for this package?
81         StaleReason string `json:",omitempty"` // why is Stale true?
82
83         // Source files
84         // If you add to this list you MUST add to p.AllFiles (below) too.
85         // Otherwise file name security lists will not apply to any new additions.
86         GoFiles           []string `json:",omitempty"` // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
87         CgoFiles          []string `json:",omitempty"` // .go source files that import "C"
88         CompiledGoFiles   []string `json:",omitempty"` // .go output from running cgo on CgoFiles
89         IgnoredGoFiles    []string `json:",omitempty"` // .go source files ignored due to build constraints
90         InvalidGoFiles    []string `json:",omitempty"` // .go source files with detected problems (parse error, wrong package name, and so on)
91         IgnoredOtherFiles []string `json:",omitempty"` // non-.go source files ignored due to build constraints
92         CFiles            []string `json:",omitempty"` // .c source files
93         CXXFiles          []string `json:",omitempty"` // .cc, .cpp and .cxx source files
94         MFiles            []string `json:",omitempty"` // .m source files
95         HFiles            []string `json:",omitempty"` // .h, .hh, .hpp and .hxx source files
96         FFiles            []string `json:",omitempty"` // .f, .F, .for and .f90 Fortran source files
97         SFiles            []string `json:",omitempty"` // .s source files
98         SwigFiles         []string `json:",omitempty"` // .swig files
99         SwigCXXFiles      []string `json:",omitempty"` // .swigcxx files
100         SysoFiles         []string `json:",omitempty"` // .syso system object files added to package
101
102         // Embedded files
103         EmbedPatterns []string `json:",omitempty"` // //go:embed patterns
104         EmbedFiles    []string `json:",omitempty"` // files matched by EmbedPatterns
105
106         // Cgo directives
107         CgoCFLAGS    []string `json:",omitempty"` // cgo: flags for C compiler
108         CgoCPPFLAGS  []string `json:",omitempty"` // cgo: flags for C preprocessor
109         CgoCXXFLAGS  []string `json:",omitempty"` // cgo: flags for C++ compiler
110         CgoFFLAGS    []string `json:",omitempty"` // cgo: flags for Fortran compiler
111         CgoLDFLAGS   []string `json:",omitempty"` // cgo: flags for linker
112         CgoPkgConfig []string `json:",omitempty"` // cgo: pkg-config names
113
114         // Dependency information
115         Imports   []string          `json:",omitempty"` // import paths used by this package
116         ImportMap map[string]string `json:",omitempty"` // map from source import to ImportPath (identity entries omitted)
117         Deps      []string          `json:",omitempty"` // all (recursively) imported dependencies
118
119         // Error information
120         // Incomplete is above, packed into the other bools
121         Error      *PackageError   `json:",omitempty"` // error loading this package (not dependencies)
122         DepsErrors []*PackageError `json:",omitempty"` // errors loading dependencies
123
124         // Test information
125         // If you add to this list you MUST add to p.AllFiles (below) too.
126         // Otherwise file name security lists will not apply to any new additions.
127         TestGoFiles        []string `json:",omitempty"` // _test.go files in package
128         TestImports        []string `json:",omitempty"` // imports from TestGoFiles
129         TestEmbedPatterns  []string `json:",omitempty"` // //go:embed patterns
130         TestEmbedFiles     []string `json:",omitempty"` // files matched by TestEmbedPatterns
131         XTestGoFiles       []string `json:",omitempty"` // _test.go files outside package
132         XTestImports       []string `json:",omitempty"` // imports from XTestGoFiles
133         XTestEmbedPatterns []string `json:",omitempty"` // //go:embed patterns
134         XTestEmbedFiles    []string `json:",omitempty"` // files matched by XTestEmbedPatterns
135 }
136
137 // AllFiles returns the names of all the files considered for the package.
138 // This is used for sanity and security checks, so we include all files,
139 // even IgnoredGoFiles, because some subcommands consider them.
140 // The go/build package filtered others out (like foo_wrongGOARCH.s)
141 // and that's OK.
142 func (p *Package) AllFiles() []string {
143         files := str.StringList(
144                 p.GoFiles,
145                 p.CgoFiles,
146                 // no p.CompiledGoFiles, because they are from GoFiles or generated by us
147                 p.IgnoredGoFiles,
148                 // no p.InvalidGoFiles, because they are from GoFiles
149                 p.IgnoredOtherFiles,
150                 p.CFiles,
151                 p.CXXFiles,
152                 p.MFiles,
153                 p.HFiles,
154                 p.FFiles,
155                 p.SFiles,
156                 p.SwigFiles,
157                 p.SwigCXXFiles,
158                 p.SysoFiles,
159                 p.TestGoFiles,
160                 p.XTestGoFiles,
161         )
162
163         // EmbedFiles may overlap with the other files.
164         // Dedup, but delay building the map as long as possible.
165         // Only files in the current directory (no slash in name)
166         // need to be checked against the files variable above.
167         var have map[string]bool
168         for _, file := range p.EmbedFiles {
169                 if !strings.Contains(file, "/") {
170                         if have == nil {
171                                 have = make(map[string]bool)
172                                 for _, file := range files {
173                                         have[file] = true
174                                 }
175                         }
176                         if have[file] {
177                                 continue
178                         }
179                 }
180                 files = append(files, file)
181         }
182         return files
183 }
184
185 // Desc returns the package "description", for use in b.showOutput.
186 func (p *Package) Desc() string {
187         if p.ForTest != "" {
188                 return p.ImportPath + " [" + p.ForTest + ".test]"
189         }
190         return p.ImportPath
191 }
192
193 type PackageInternal struct {
194         // Unexported fields are not part of the public API.
195         Build             *build.Package
196         Imports           []*Package           // this package's direct imports
197         CompiledImports   []string             // additional Imports necessary when using CompiledGoFiles (all from standard library); 1:1 with the end of PackagePublic.Imports
198         RawImports        []string             // this package's original imports as they appear in the text of the program; 1:1 with the end of PackagePublic.Imports
199         ForceLibrary      bool                 // this package is a library (even if named "main")
200         CmdlineFiles      bool                 // package built from files listed on command line
201         CmdlinePkg        bool                 // package listed on command line
202         CmdlinePkgLiteral bool                 // package listed as literal on command line (not via wildcard)
203         Local             bool                 // imported via local path (./ or ../)
204         LocalPrefix       string               // interpret ./ and ../ imports relative to this prefix
205         ExeName           string               // desired name for temporary executable
206         CoverMode         string               // preprocess Go source files with the coverage tool in this mode
207         CoverVars         map[string]*CoverVar // variables created by coverage analysis
208         OmitDebug         bool                 // tell linker not to write debug information
209         GobinSubdir       bool                 // install target would be subdir of GOBIN
210         BuildInfo         string               // add this info to package main
211         TestmainGo        *[]byte              // content for _testmain.go
212         Embed             map[string][]string  // //go:embed comment mapping
213         OrigImportPath    string               // original import path before adding '_test' suffix
214
215         Asmflags   []string // -asmflags for this package
216         Gcflags    []string // -gcflags for this package
217         Ldflags    []string // -ldflags for this package
218         Gccgoflags []string // -gccgoflags for this package
219 }
220
221 // A NoGoError indicates that no Go files for the package were applicable to the
222 // build for that package.
223 //
224 // That may be because there were no files whatsoever, or because all files were
225 // excluded, or because all non-excluded files were test sources.
226 type NoGoError struct {
227         Package *Package
228 }
229
230 func (e *NoGoError) Error() string {
231         if len(e.Package.IgnoredGoFiles) > 0 {
232                 // Go files exist, but they were ignored due to build constraints.
233                 return "build constraints exclude all Go files in " + e.Package.Dir
234         }
235         if len(e.Package.TestGoFiles)+len(e.Package.XTestGoFiles) > 0 {
236                 // Test Go files exist, but we're not interested in them.
237                 // The double-negative is unfortunate but we want e.Package.Dir
238                 // to appear at the end of error message.
239                 return "no non-test Go files in " + e.Package.Dir
240         }
241         return "no Go files in " + e.Package.Dir
242 }
243
244 // setLoadPackageDataError presents an error found when loading package data
245 // as a *PackageError. It has special cases for some common errors to improve
246 // messages shown to users and reduce redundancy.
247 //
248 // setLoadPackageDataError returns true if it's safe to load information about
249 // imported packages, for example, if there was a parse error loading imports
250 // in one file, but other files are okay.
251 func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportStack, importPos []token.Position) {
252         matchErr, isMatchErr := err.(*search.MatchError)
253         if isMatchErr && matchErr.Match.Pattern() == path {
254                 if matchErr.Match.IsLiteral() {
255                         // The error has a pattern has a pattern similar to the import path.
256                         // It may be slightly different (./foo matching example.com/foo),
257                         // but close enough to seem redundant.
258                         // Unwrap the error so we don't show the pattern.
259                         err = matchErr.Err
260                 }
261         }
262
263         // Replace (possibly wrapped) *build.NoGoError with *load.NoGoError.
264         // The latter is more specific about the cause.
265         var nogoErr *build.NoGoError
266         if errors.As(err, &nogoErr) {
267                 if p.Dir == "" && nogoErr.Dir != "" {
268                         p.Dir = nogoErr.Dir
269                 }
270                 err = &NoGoError{Package: p}
271         }
272
273         // Take only the first error from a scanner.ErrorList. PackageError only
274         // has room for one position, so we report the first error with a position
275         // instead of all of the errors without a position.
276         var pos string
277         var isScanErr bool
278         if scanErr, ok := err.(scanner.ErrorList); ok && len(scanErr) > 0 {
279                 isScanErr = true // For stack push/pop below.
280
281                 scanPos := scanErr[0].Pos
282                 scanPos.Filename = base.ShortPath(scanPos.Filename)
283                 pos = scanPos.String()
284                 err = errors.New(scanErr[0].Msg)
285         }
286
287         // Report the error on the importing package if the problem is with the import declaration
288         // for example, if the package doesn't exist or if the import path is malformed.
289         // On the other hand, don't include a position if the problem is with the imported package,
290         // for example there are no Go files (NoGoError), or there's a problem in the imported
291         // package's source files themselves (scanner errors).
292         //
293         // TODO(matloob): Perhaps make each of those the errors in the first group
294         // (including modload.ImportMissingError, ImportMissingSumError, and the
295         // corresponding "cannot find package %q in any of" GOPATH-mode error
296         // produced in build.(*Context).Import; modload.AmbiguousImportError,
297         // and modload.PackageNotInModuleError; and the malformed module path errors
298         // produced in golang.org/x/mod/module.CheckMod) implement an interface
299         // to make it easier to check for them? That would save us from having to
300         // move the modload errors into this package to avoid a package import cycle,
301         // and from having to export an error type for the errors produced in build.
302         if !isMatchErr && (nogoErr != nil || isScanErr) {
303                 stk.Push(path)
304                 defer stk.Pop()
305         }
306
307         p.Error = &PackageError{
308                 ImportStack: stk.Copy(),
309                 Pos:         pos,
310                 Err:         err,
311         }
312
313         if path != stk.Top() {
314                 p.Error.setPos(importPos)
315         }
316 }
317
318 // Resolve returns the resolved version of imports,
319 // which should be p.TestImports or p.XTestImports, NOT p.Imports.
320 // The imports in p.TestImports and p.XTestImports are not recursively
321 // loaded during the initial load of p, so they list the imports found in
322 // the source file, but most processing should be over the vendor-resolved
323 // import paths. We do this resolution lazily both to avoid file system work
324 // and because the eventual real load of the test imports (during 'go test')
325 // can produce better error messages if it starts with the original paths.
326 // The initial load of p loads all the non-test imports and rewrites
327 // the vendored paths, so nothing should ever call p.vendored(p.Imports).
328 func (p *Package) Resolve(imports []string) []string {
329         if len(imports) > 0 && len(p.Imports) > 0 && &imports[0] == &p.Imports[0] {
330                 panic("internal error: p.Resolve(p.Imports) called")
331         }
332         seen := make(map[string]bool)
333         var all []string
334         for _, path := range imports {
335                 path = ResolveImportPath(p, path)
336                 if !seen[path] {
337                         seen[path] = true
338                         all = append(all, path)
339                 }
340         }
341         sort.Strings(all)
342         return all
343 }
344
345 // CoverVar holds the name of the generated coverage variables targeting the named file.
346 type CoverVar struct {
347         File string // local file name
348         Var  string // name of count struct
349 }
350
351 func (p *Package) copyBuild(opts PackageOpts, pp *build.Package) {
352         p.Internal.Build = pp
353
354         if pp.PkgTargetRoot != "" && cfg.BuildPkgdir != "" {
355                 old := pp.PkgTargetRoot
356                 pp.PkgRoot = cfg.BuildPkgdir
357                 pp.PkgTargetRoot = cfg.BuildPkgdir
358                 pp.PkgObj = filepath.Join(cfg.BuildPkgdir, strings.TrimPrefix(pp.PkgObj, old))
359         }
360
361         p.Dir = pp.Dir
362         p.ImportPath = pp.ImportPath
363         p.ImportComment = pp.ImportComment
364         p.Name = pp.Name
365         p.Doc = pp.Doc
366         p.Root = pp.Root
367         p.ConflictDir = pp.ConflictDir
368         p.BinaryOnly = pp.BinaryOnly
369
370         // TODO? Target
371         p.Goroot = pp.Goroot
372         p.Standard = p.Goroot && p.ImportPath != "" && search.IsStandardImportPath(p.ImportPath)
373         p.GoFiles = pp.GoFiles
374         p.CgoFiles = pp.CgoFiles
375         p.IgnoredGoFiles = pp.IgnoredGoFiles
376         p.InvalidGoFiles = pp.InvalidGoFiles
377         p.IgnoredOtherFiles = pp.IgnoredOtherFiles
378         p.CFiles = pp.CFiles
379         p.CXXFiles = pp.CXXFiles
380         p.MFiles = pp.MFiles
381         p.HFiles = pp.HFiles
382         p.FFiles = pp.FFiles
383         p.SFiles = pp.SFiles
384         p.SwigFiles = pp.SwigFiles
385         p.SwigCXXFiles = pp.SwigCXXFiles
386         p.SysoFiles = pp.SysoFiles
387         p.CgoCFLAGS = pp.CgoCFLAGS
388         p.CgoCPPFLAGS = pp.CgoCPPFLAGS
389         p.CgoCXXFLAGS = pp.CgoCXXFLAGS
390         p.CgoFFLAGS = pp.CgoFFLAGS
391         p.CgoLDFLAGS = pp.CgoLDFLAGS
392         p.CgoPkgConfig = pp.CgoPkgConfig
393         // We modify p.Imports in place, so make copy now.
394         p.Imports = make([]string, len(pp.Imports))
395         copy(p.Imports, pp.Imports)
396         p.Internal.RawImports = pp.Imports
397         p.TestGoFiles = pp.TestGoFiles
398         p.TestImports = pp.TestImports
399         p.XTestGoFiles = pp.XTestGoFiles
400         p.XTestImports = pp.XTestImports
401         if opts.IgnoreImports {
402                 p.Imports = nil
403                 p.Internal.RawImports = nil
404                 p.TestImports = nil
405                 p.XTestImports = nil
406         }
407         p.EmbedPatterns = pp.EmbedPatterns
408         p.TestEmbedPatterns = pp.TestEmbedPatterns
409         p.XTestEmbedPatterns = pp.XTestEmbedPatterns
410         p.Internal.OrigImportPath = pp.ImportPath
411 }
412
413 // A PackageError describes an error loading information about a package.
414 type PackageError struct {
415         ImportStack      []string // shortest path from package named on command line to this one
416         Pos              string   // position of error
417         Err              error    // the error itself
418         IsImportCycle    bool     // the error is an import cycle
419         Hard             bool     // whether the error is soft or hard; soft errors are ignored in some places
420         alwaysPrintStack bool     // whether to always print the ImportStack
421 }
422
423 func (p *PackageError) Error() string {
424         // TODO(#43696): decide when to print the stack or the position based on
425         // the error type and whether the package is in the main module.
426         // Document the rationale.
427         if p.Pos != "" && (len(p.ImportStack) == 0 || !p.alwaysPrintStack) {
428                 // Omit import stack. The full path to the file where the error
429                 // is the most important thing.
430                 return p.Pos + ": " + p.Err.Error()
431         }
432
433         // If the error is an ImportPathError, and the last path on the stack appears
434         // in the error message, omit that path from the stack to avoid repetition.
435         // If an ImportPathError wraps another ImportPathError that matches the
436         // last path on the stack, we don't omit the path. An error like
437         // "package A imports B: error loading C caused by B" would not be clearer
438         // if "imports B" were omitted.
439         if len(p.ImportStack) == 0 {
440                 return p.Err.Error()
441         }
442         var optpos string
443         if p.Pos != "" {
444                 optpos = "\n\t" + p.Pos
445         }
446         return "package " + strings.Join(p.ImportStack, "\n\timports ") + optpos + ": " + p.Err.Error()
447 }
448
449 func (p *PackageError) Unwrap() error { return p.Err }
450
451 // PackageError implements MarshalJSON so that Err is marshaled as a string
452 // and non-essential fields are omitted.
453 func (p *PackageError) MarshalJSON() ([]byte, error) {
454         perr := struct {
455                 ImportStack []string
456                 Pos         string
457                 Err         string
458         }{p.ImportStack, p.Pos, p.Err.Error()}
459         return json.Marshal(perr)
460 }
461
462 func (p *PackageError) setPos(posList []token.Position) {
463         if len(posList) == 0 {
464                 return
465         }
466         pos := posList[0]
467         pos.Filename = base.ShortPath(pos.Filename)
468         p.Pos = pos.String()
469 }
470
471 // ImportPathError is a type of error that prevents a package from being loaded
472 // for a given import path. When such a package is loaded, a *Package is
473 // returned with Err wrapping an ImportPathError: the error is attached to
474 // the imported package, not the importing package.
475 //
476 // The string returned by ImportPath must appear in the string returned by
477 // Error. Errors that wrap ImportPathError (such as PackageError) may omit
478 // the import path.
479 type ImportPathError interface {
480         error
481         ImportPath() string
482 }
483
484 var (
485         _ ImportPathError = (*importError)(nil)
486         _ ImportPathError = (*mainPackageError)(nil)
487         _ ImportPathError = (*modload.ImportMissingError)(nil)
488         _ ImportPathError = (*modload.ImportMissingSumError)(nil)
489         _ ImportPathError = (*modload.DirectImportFromImplicitDependencyError)(nil)
490 )
491
492 type importError struct {
493         importPath string
494         err        error // created with fmt.Errorf
495 }
496
497 func ImportErrorf(path, format string, args ...interface{}) ImportPathError {
498         err := &importError{importPath: path, err: fmt.Errorf(format, args...)}
499         if errStr := err.Error(); !strings.Contains(errStr, path) {
500                 panic(fmt.Sprintf("path %q not in error %q", path, errStr))
501         }
502         return err
503 }
504
505 func (e *importError) Error() string {
506         return e.err.Error()
507 }
508
509 func (e *importError) Unwrap() error {
510         // Don't return e.err directly, since we're only wrapping an error if %w
511         // was passed to ImportErrorf.
512         return errors.Unwrap(e.err)
513 }
514
515 func (e *importError) ImportPath() string {
516         return e.importPath
517 }
518
519 // An ImportStack is a stack of import paths, possibly with the suffix " (test)" appended.
520 // The import path of a test package is the import path of the corresponding
521 // non-test package with the suffix "_test" added.
522 type ImportStack []string
523
524 func (s *ImportStack) Push(p string) {
525         *s = append(*s, p)
526 }
527
528 func (s *ImportStack) Pop() {
529         *s = (*s)[0 : len(*s)-1]
530 }
531
532 func (s *ImportStack) Copy() []string {
533         return append([]string{}, *s...)
534 }
535
536 func (s *ImportStack) Top() string {
537         if len(*s) == 0 {
538                 return ""
539         }
540         return (*s)[len(*s)-1]
541 }
542
543 // shorterThan reports whether sp is shorter than t.
544 // We use this to record the shortest import sequence
545 // that leads to a particular package.
546 func (sp *ImportStack) shorterThan(t []string) bool {
547         s := *sp
548         if len(s) != len(t) {
549                 return len(s) < len(t)
550         }
551         // If they are the same length, settle ties using string ordering.
552         for i := range s {
553                 if s[i] != t[i] {
554                         return s[i] < t[i]
555                 }
556         }
557         return false // they are equal
558 }
559
560 // packageCache is a lookup cache for LoadImport,
561 // so that if we look up a package multiple times
562 // we return the same pointer each time.
563 var packageCache = map[string]*Package{}
564
565 // ClearPackageCache clears the in-memory package cache and the preload caches.
566 // It is only for use by GOPATH-based "go get".
567 // TODO(jayconrod): When GOPATH-based "go get" is removed, delete this function.
568 func ClearPackageCache() {
569         for name := range packageCache {
570                 delete(packageCache, name)
571         }
572         resolvedImportCache.Clear()
573         packageDataCache.Clear()
574 }
575
576 // ClearPackageCachePartial clears packages with the given import paths from the
577 // in-memory package cache and the preload caches. It is only for use by
578 // GOPATH-based "go get".
579 // TODO(jayconrod): When GOPATH-based "go get" is removed, delete this function.
580 func ClearPackageCachePartial(args []string) {
581         shouldDelete := make(map[string]bool)
582         for _, arg := range args {
583                 shouldDelete[arg] = true
584                 if p := packageCache[arg]; p != nil {
585                         delete(packageCache, arg)
586                 }
587         }
588         resolvedImportCache.DeleteIf(func(key interface{}) bool {
589                 return shouldDelete[key.(importSpec).path]
590         })
591         packageDataCache.DeleteIf(func(key interface{}) bool {
592                 return shouldDelete[key.(string)]
593         })
594 }
595
596 // ReloadPackageNoFlags is like LoadImport but makes sure
597 // not to use the package cache.
598 // It is only for use by GOPATH-based "go get".
599 // TODO(rsc): When GOPATH-based "go get" is removed, delete this function.
600 func ReloadPackageNoFlags(arg string, stk *ImportStack) *Package {
601         p := packageCache[arg]
602         if p != nil {
603                 delete(packageCache, arg)
604                 resolvedImportCache.DeleteIf(func(key interface{}) bool {
605                         return key.(importSpec).path == p.ImportPath
606                 })
607                 packageDataCache.Delete(p.ImportPath)
608         }
609         return LoadImport(context.TODO(), PackageOpts{}, arg, base.Cwd(), nil, stk, nil, 0)
610 }
611
612 // dirToImportPath returns the pseudo-import path we use for a package
613 // outside the Go path. It begins with _/ and then contains the full path
614 // to the directory. If the package lives in c:\home\gopher\my\pkg then
615 // the pseudo-import path is _/c_/home/gopher/my/pkg.
616 // Using a pseudo-import path like this makes the ./ imports no longer
617 // a special case, so that all the code to deal with ordinary imports works
618 // automatically.
619 func dirToImportPath(dir string) string {
620         return pathpkg.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
621 }
622
623 func makeImportValid(r rune) rune {
624         // Should match Go spec, compilers, and ../../go/parser/parser.go:/isValidImport.
625         const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
626         if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
627                 return '_'
628         }
629         return r
630 }
631
632 // Mode flags for loadImport and download (in get.go).
633 const (
634         // ResolveImport means that loadImport should do import path expansion.
635         // That is, ResolveImport means that the import path came from
636         // a source file and has not been expanded yet to account for
637         // vendoring or possible module adjustment.
638         // Every import path should be loaded initially with ResolveImport,
639         // and then the expanded version (for example with the /vendor/ in it)
640         // gets recorded as the canonical import path. At that point, future loads
641         // of that package must not pass ResolveImport, because
642         // disallowVendor will reject direct use of paths containing /vendor/.
643         ResolveImport = 1 << iota
644
645         // ResolveModule is for download (part of "go get") and indicates
646         // that the module adjustment should be done, but not vendor adjustment.
647         ResolveModule
648
649         // GetTestDeps is for download (part of "go get") and indicates
650         // that test dependencies should be fetched too.
651         GetTestDeps
652 )
653
654 // LoadImport scans the directory named by path, which must be an import path,
655 // but possibly a local import path (an absolute file system path or one beginning
656 // with ./ or ../). A local relative path is interpreted relative to srcDir.
657 // It returns a *Package describing the package found in that directory.
658 // LoadImport does not set tool flags and should only be used by
659 // this package, as part of a bigger load operation, and by GOPATH-based "go get".
660 // TODO(rsc): When GOPATH-based "go get" is removed, unexport this function.
661 func LoadImport(ctx context.Context, opts PackageOpts, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
662         return loadImport(ctx, opts, nil, path, srcDir, parent, stk, importPos, mode)
663 }
664
665 func loadImport(ctx context.Context, opts PackageOpts, pre *preload, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
666         if path == "" {
667                 panic("LoadImport called with empty package path")
668         }
669
670         var parentPath, parentRoot string
671         parentIsStd := false
672         if parent != nil {
673                 parentPath = parent.ImportPath
674                 parentRoot = parent.Root
675                 parentIsStd = parent.Standard
676         }
677         bp, loaded, err := loadPackageData(ctx, path, parentPath, srcDir, parentRoot, parentIsStd, mode)
678         if loaded && pre != nil && !opts.IgnoreImports {
679                 pre.preloadImports(ctx, opts, bp.Imports, bp)
680         }
681         if bp == nil {
682                 p := &Package{
683                         PackagePublic: PackagePublic{
684                                 ImportPath: path,
685                                 Incomplete: true,
686                         },
687                 }
688                 if importErr, ok := err.(ImportPathError); !ok || importErr.ImportPath() != path {
689                         // Only add path to the error's import stack if it's not already present
690                         // in the error.
691                         //
692                         // TODO(bcmills): setLoadPackageDataError itself has a similar Push / Pop
693                         // sequence that empirically doesn't trigger for these errors, guarded by
694                         // a somewhat complex condition. Figure out how to generalize that
695                         // condition and eliminate the explicit calls here.
696                         stk.Push(path)
697                         defer stk.Pop()
698                 }
699                 p.setLoadPackageDataError(err, path, stk, nil)
700                 return p
701         }
702
703         importPath := bp.ImportPath
704         p := packageCache[importPath]
705         if p != nil {
706                 stk.Push(path)
707                 p = reusePackage(p, stk)
708                 stk.Pop()
709         } else {
710                 p = new(Package)
711                 p.Internal.Local = build.IsLocalImport(path)
712                 p.ImportPath = importPath
713                 packageCache[importPath] = p
714
715                 // Load package.
716                 // loadPackageData may return bp != nil even if an error occurs,
717                 // in order to return partial information.
718                 p.load(ctx, opts, path, stk, importPos, bp, err)
719
720                 if !cfg.ModulesEnabled && path != cleanImport(path) {
721                         p.Error = &PackageError{
722                                 ImportStack: stk.Copy(),
723                                 Err:         ImportErrorf(path, "non-canonical import path %q: should be %q", path, pathpkg.Clean(path)),
724                         }
725                         p.Incomplete = true
726                         p.Error.setPos(importPos)
727                 }
728         }
729
730         // Checked on every import because the rules depend on the code doing the importing.
731         if perr := disallowInternal(ctx, srcDir, parent, parentPath, p, stk); perr != p {
732                 perr.Error.setPos(importPos)
733                 return perr
734         }
735         if mode&ResolveImport != 0 {
736                 if perr := disallowVendor(srcDir, path, parentPath, p, stk); perr != p {
737                         perr.Error.setPos(importPos)
738                         return perr
739                 }
740         }
741
742         if p.Name == "main" && parent != nil && parent.Dir != p.Dir {
743                 perr := *p
744                 perr.Error = &PackageError{
745                         ImportStack: stk.Copy(),
746                         Err:         ImportErrorf(path, "import %q is a program, not an importable package", path),
747                 }
748                 perr.Error.setPos(importPos)
749                 return &perr
750         }
751
752         if p.Internal.Local && parent != nil && !parent.Internal.Local {
753                 perr := *p
754                 var err error
755                 if path == "." {
756                         err = ImportErrorf(path, "%s: cannot import current directory", path)
757                 } else {
758                         err = ImportErrorf(path, "local import %q in non-local package", path)
759                 }
760                 perr.Error = &PackageError{
761                         ImportStack: stk.Copy(),
762                         Err:         err,
763                 }
764                 perr.Error.setPos(importPos)
765                 return &perr
766         }
767
768         return p
769 }
770
771 // loadPackageData loads information needed to construct a *Package. The result
772 // is cached, and later calls to loadPackageData for the same package will return
773 // the same data.
774 //
775 // loadPackageData returns a non-nil package even if err is non-nil unless
776 // the package path is malformed (for example, the path contains "mod/" or "@").
777 //
778 // loadPackageData returns a boolean, loaded, which is true if this is the
779 // first time the package was loaded. Callers may preload imports in this case.
780 func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoot string, parentIsStd bool, mode int) (bp *build.Package, loaded bool, err error) {
781         if path == "" {
782                 panic("loadPackageData called with empty package path")
783         }
784
785         if strings.HasPrefix(path, "mod/") {
786                 // Paths beginning with "mod/" might accidentally
787                 // look in the module cache directory tree in $GOPATH/pkg/mod/.
788                 // This prefix is owned by the Go core for possible use in the
789                 // standard library (since it does not begin with a domain name),
790                 // so it's OK to disallow entirely.
791                 return nil, false, fmt.Errorf("disallowed import path %q", path)
792         }
793
794         if strings.Contains(path, "@") {
795                 return nil, false, errors.New("can only use path@version syntax with 'go get' and 'go install' in module-aware mode")
796         }
797
798         // Determine canonical package path and directory.
799         // For a local import the identifier is the pseudo-import path
800         // we create from the full directory to the package.
801         // Otherwise it is the usual import path.
802         // For vendored imports, it is the expanded form.
803         //
804         // Note that when modules are enabled, local import paths are normally
805         // canonicalized by modload.LoadPackages before now. However, if there's an
806         // error resolving a local path, it will be returned untransformed
807         // so that 'go list -e' reports something useful.
808         importKey := importSpec{
809                 path:        path,
810                 parentPath:  parentPath,
811                 parentDir:   parentDir,
812                 parentRoot:  parentRoot,
813                 parentIsStd: parentIsStd,
814                 mode:        mode,
815         }
816         r := resolvedImportCache.Do(importKey, func() interface{} {
817                 var r resolvedImport
818                 if build.IsLocalImport(path) {
819                         r.dir = filepath.Join(parentDir, path)
820                         r.path = dirToImportPath(r.dir)
821                 } else if cfg.ModulesEnabled {
822                         r.dir, r.path, r.err = modload.Lookup(parentPath, parentIsStd, path)
823                 } else if mode&ResolveImport != 0 {
824                         // We do our own path resolution, because we want to
825                         // find out the key to use in packageCache without the
826                         // overhead of repeated calls to buildContext.Import.
827                         // The code is also needed in a few other places anyway.
828                         r.path = resolveImportPath(path, parentPath, parentDir, parentRoot, parentIsStd)
829                 } else if mode&ResolveModule != 0 {
830                         r.path = moduleImportPath(path, parentPath, parentDir, parentRoot)
831                 }
832                 if r.path == "" {
833                         r.path = path
834                 }
835                 return r
836         }).(resolvedImport)
837         // Invariant: r.path is set to the resolved import path. If the path cannot
838         // be resolved, r.path is set to path, the source import path.
839         // r.path is never empty.
840
841         // Load the package from its directory. If we already found the package's
842         // directory when resolving its import path, use that.
843         data := packageDataCache.Do(r.path, func() interface{} {
844                 loaded = true
845                 var data packageData
846                 if r.dir != "" {
847                         var buildMode build.ImportMode
848                         if !cfg.ModulesEnabled {
849                                 buildMode = build.ImportComment
850                         }
851                         data.p, data.err = cfg.BuildContext.ImportDir(r.dir, buildMode)
852                         if cfg.ModulesEnabled {
853                                 // Override data.p.Root, since ImportDir sets it to $GOPATH, if
854                                 // the module is inside $GOPATH/src.
855                                 if info := modload.PackageModuleInfo(ctx, path); info != nil {
856                                         data.p.Root = info.Dir
857                                 }
858                         }
859                         if r.err != nil {
860                                 if data.err != nil {
861                                         // ImportDir gave us one error, and the module loader gave us another.
862                                         // We arbitrarily choose to keep the error from ImportDir because
863                                         // that's what our tests already expect, and it seems to provide a bit
864                                         // more detail in most cases.
865                                 } else if errors.Is(r.err, imports.ErrNoGo) {
866                                         // ImportDir said there were files in the package, but the module
867                                         // loader said there weren't. Which one is right?
868                                         // Without this special-case hack, the TestScript/test_vet case fails
869                                         // on the vetfail/p1 package (added in CL 83955).
870                                         // Apparently, imports.ShouldBuild biases toward rejecting files
871                                         // with invalid build constraints, whereas ImportDir biases toward
872                                         // accepting them.
873                                         //
874                                         // TODO(#41410: Figure out how this actually ought to work and fix
875                                         // this mess.
876                                 } else {
877                                         data.err = r.err
878                                 }
879                         }
880                 } else if r.err != nil {
881                         data.p = new(build.Package)
882                         data.err = r.err
883                 } else if cfg.ModulesEnabled && path != "unsafe" {
884                         data.p = new(build.Package)
885                         data.err = fmt.Errorf("unknown import path %q: internal error: module loader did not resolve import", r.path)
886                 } else {
887                         buildMode := build.ImportComment
888                         if mode&ResolveImport == 0 || r.path != path {
889                                 // Not vendoring, or we already found the vendored path.
890                                 buildMode |= build.IgnoreVendor
891                         }
892                         data.p, data.err = cfg.BuildContext.Import(r.path, parentDir, buildMode)
893                 }
894                 data.p.ImportPath = r.path
895
896                 // Set data.p.BinDir in cases where go/build.Context.Import
897                 // may give us a path we don't want.
898                 if !data.p.Goroot {
899                         if cfg.GOBIN != "" {
900                                 data.p.BinDir = cfg.GOBIN
901                         } else if cfg.ModulesEnabled {
902                                 data.p.BinDir = modload.BinDir()
903                         }
904                 }
905
906                 if !cfg.ModulesEnabled && data.err == nil &&
907                         data.p.ImportComment != "" && data.p.ImportComment != path &&
908                         !strings.Contains(path, "/vendor/") && !strings.HasPrefix(path, "vendor/") {
909                         data.err = fmt.Errorf("code in directory %s expects import %q", data.p.Dir, data.p.ImportComment)
910                 }
911                 return data
912         }).(packageData)
913
914         return data.p, loaded, data.err
915 }
916
917 // importSpec describes an import declaration in source code. It is used as a
918 // cache key for resolvedImportCache.
919 type importSpec struct {
920         path                              string
921         parentPath, parentDir, parentRoot string
922         parentIsStd                       bool
923         mode                              int
924 }
925
926 // resolvedImport holds a canonical identifier for a package. It may also contain
927 // a path to the package's directory and an error if one occurred. resolvedImport
928 // is the value type in resolvedImportCache.
929 type resolvedImport struct {
930         path, dir string
931         err       error
932 }
933
934 // packageData holds information loaded from a package. It is the value type
935 // in packageDataCache.
936 type packageData struct {
937         p   *build.Package
938         err error
939 }
940
941 // resolvedImportCache maps import strings (importSpec) to canonical package names
942 // (resolvedImport).
943 var resolvedImportCache par.Cache
944
945 // packageDataCache maps canonical package names (string) to package metadata
946 // (packageData).
947 var packageDataCache par.Cache
948
949 // preloadWorkerCount is the number of concurrent goroutines that can load
950 // packages. Experimentally, there are diminishing returns with more than
951 // 4 workers. This was measured on the following machines.
952 //
953 // * MacBookPro with a 4-core Intel Core i7 CPU
954 // * Linux workstation with 6-core Intel Xeon CPU
955 // * Linux workstation with 24-core Intel Xeon CPU
956 //
957 // It is very likely (though not confirmed) that this workload is limited
958 // by memory bandwidth. We don't have a good way to determine the number of
959 // workers that would saturate the bus though, so runtime.GOMAXPROCS
960 // seems like a reasonable default.
961 var preloadWorkerCount = runtime.GOMAXPROCS(0)
962
963 // preload holds state for managing concurrent preloading of package data.
964 //
965 // A preload should be created with newPreload before loading a large
966 // package graph. flush must be called when package loading is complete
967 // to ensure preload goroutines are no longer active. This is necessary
968 // because of global mutable state that cannot safely be read and written
969 // concurrently. In particular, packageDataCache may be cleared by "go get"
970 // in GOPATH mode, and modload.loaded (accessed via modload.Lookup) may be
971 // modified by modload.LoadPackages.
972 type preload struct {
973         cancel chan struct{}
974         sema   chan struct{}
975 }
976
977 // newPreload creates a new preloader. flush must be called later to avoid
978 // accessing global state while it is being modified.
979 func newPreload() *preload {
980         pre := &preload{
981                 cancel: make(chan struct{}),
982                 sema:   make(chan struct{}, preloadWorkerCount),
983         }
984         return pre
985 }
986
987 // preloadMatches loads data for package paths matched by patterns.
988 // When preloadMatches returns, some packages may not be loaded yet, but
989 // loadPackageData and loadImport are always safe to call.
990 func (pre *preload) preloadMatches(ctx context.Context, opts PackageOpts, matches []*search.Match) {
991         for _, m := range matches {
992                 for _, pkg := range m.Pkgs {
993                         select {
994                         case <-pre.cancel:
995                                 return
996                         case pre.sema <- struct{}{}:
997                                 go func(pkg string) {
998                                         mode := 0 // don't use vendoring or module import resolution
999                                         bp, loaded, err := loadPackageData(ctx, pkg, "", base.Cwd(), "", false, mode)
1000                                         <-pre.sema
1001                                         if bp != nil && loaded && err == nil && !opts.IgnoreImports {
1002                                                 pre.preloadImports(ctx, opts, bp.Imports, bp)
1003                                         }
1004                                 }(pkg)
1005                         }
1006                 }
1007         }
1008 }
1009
1010 // preloadImports queues a list of imports for preloading.
1011 // When preloadImports returns, some packages may not be loaded yet,
1012 // but loadPackageData and loadImport are always safe to call.
1013 func (pre *preload) preloadImports(ctx context.Context, opts PackageOpts, imports []string, parent *build.Package) {
1014         parentIsStd := parent.Goroot && parent.ImportPath != "" && search.IsStandardImportPath(parent.ImportPath)
1015         for _, path := range imports {
1016                 if path == "C" || path == "unsafe" {
1017                         continue
1018                 }
1019                 select {
1020                 case <-pre.cancel:
1021                         return
1022                 case pre.sema <- struct{}{}:
1023                         go func(path string) {
1024                                 bp, loaded, err := loadPackageData(ctx, path, parent.ImportPath, parent.Dir, parent.Root, parentIsStd, ResolveImport)
1025                                 <-pre.sema
1026                                 if bp != nil && loaded && err == nil && !opts.IgnoreImports {
1027                                         pre.preloadImports(ctx, opts, bp.Imports, bp)
1028                                 }
1029                         }(path)
1030                 }
1031         }
1032 }
1033
1034 // flush stops pending preload operations. flush blocks until preload calls to
1035 // loadPackageData have completed. The preloader will not make any new calls
1036 // to loadPackageData.
1037 func (pre *preload) flush() {
1038         // flush is usually deferred.
1039         // Don't hang program waiting for workers on panic.
1040         if v := recover(); v != nil {
1041                 panic(v)
1042         }
1043
1044         close(pre.cancel)
1045         for i := 0; i < preloadWorkerCount; i++ {
1046                 pre.sema <- struct{}{}
1047         }
1048 }
1049
1050 func cleanImport(path string) string {
1051         orig := path
1052         path = pathpkg.Clean(path)
1053         if strings.HasPrefix(orig, "./") && path != ".." && !strings.HasPrefix(path, "../") {
1054                 path = "./" + path
1055         }
1056         return path
1057 }
1058
1059 var isDirCache par.Cache
1060
1061 func isDir(path string) bool {
1062         return isDirCache.Do(path, func() interface{} {
1063                 fi, err := fsys.Stat(path)
1064                 return err == nil && fi.IsDir()
1065         }).(bool)
1066 }
1067
1068 // ResolveImportPath returns the true meaning of path when it appears in parent.
1069 // There are two different resolutions applied.
1070 // First, there is Go 1.5 vendoring (golang.org/s/go15vendor).
1071 // If vendor expansion doesn't trigger, then the path is also subject to
1072 // Go 1.11 module legacy conversion (golang.org/issue/25069).
1073 func ResolveImportPath(parent *Package, path string) (found string) {
1074         var parentPath, parentDir, parentRoot string
1075         parentIsStd := false
1076         if parent != nil {
1077                 parentPath = parent.ImportPath
1078                 parentDir = parent.Dir
1079                 parentRoot = parent.Root
1080                 parentIsStd = parent.Standard
1081         }
1082         return resolveImportPath(path, parentPath, parentDir, parentRoot, parentIsStd)
1083 }
1084
1085 func resolveImportPath(path, parentPath, parentDir, parentRoot string, parentIsStd bool) (found string) {
1086         if cfg.ModulesEnabled {
1087                 if _, p, e := modload.Lookup(parentPath, parentIsStd, path); e == nil {
1088                         return p
1089                 }
1090                 return path
1091         }
1092         found = vendoredImportPath(path, parentPath, parentDir, parentRoot)
1093         if found != path {
1094                 return found
1095         }
1096         return moduleImportPath(path, parentPath, parentDir, parentRoot)
1097 }
1098
1099 // dirAndRoot returns the source directory and workspace root
1100 // for the package p, guaranteeing that root is a path prefix of dir.
1101 func dirAndRoot(path string, dir, root string) (string, string) {
1102         origDir, origRoot := dir, root
1103         dir = filepath.Clean(dir)
1104         root = filepath.Join(root, "src")
1105         if !str.HasFilePathPrefix(dir, root) || path != "command-line-arguments" && filepath.Join(root, path) != dir {
1106                 // Look for symlinks before reporting error.
1107                 dir = expandPath(dir)
1108                 root = expandPath(root)
1109         }
1110
1111         if !str.HasFilePathPrefix(dir, root) || len(dir) <= len(root) || dir[len(root)] != filepath.Separator || path != "command-line-arguments" && !build.IsLocalImport(path) && filepath.Join(root, path) != dir {
1112                 base.Fatalf("unexpected directory layout:\n"+
1113                         "       import path: %s\n"+
1114                         "       root: %s\n"+
1115                         "       dir: %s\n"+
1116                         "       expand root: %s\n"+
1117                         "       expand dir: %s\n"+
1118                         "       separator: %s",
1119                         path,
1120                         filepath.Join(origRoot, "src"),
1121                         filepath.Clean(origDir),
1122                         origRoot,
1123                         origDir,
1124                         string(filepath.Separator))
1125         }
1126
1127         return dir, root
1128 }
1129
1130 // vendoredImportPath returns the vendor-expansion of path when it appears in parent.
1131 // If parent is x/y/z, then path might expand to x/y/z/vendor/path, x/y/vendor/path,
1132 // x/vendor/path, vendor/path, or else stay path if none of those exist.
1133 // vendoredImportPath returns the expanded path or, if no expansion is found, the original.
1134 func vendoredImportPath(path, parentPath, parentDir, parentRoot string) (found string) {
1135         if parentRoot == "" {
1136                 return path
1137         }
1138
1139         dir, root := dirAndRoot(parentPath, parentDir, parentRoot)
1140
1141         vpath := "vendor/" + path
1142         for i := len(dir); i >= len(root); i-- {
1143                 if i < len(dir) && dir[i] != filepath.Separator {
1144                         continue
1145                 }
1146                 // Note: checking for the vendor directory before checking
1147                 // for the vendor/path directory helps us hit the
1148                 // isDir cache more often. It also helps us prepare a more useful
1149                 // list of places we looked, to report when an import is not found.
1150                 if !isDir(filepath.Join(dir[:i], "vendor")) {
1151                         continue
1152                 }
1153                 targ := filepath.Join(dir[:i], vpath)
1154                 if isDir(targ) && hasGoFiles(targ) {
1155                         importPath := parentPath
1156                         if importPath == "command-line-arguments" {
1157                                 // If parent.ImportPath is 'command-line-arguments'.
1158                                 // set to relative directory to root (also chopped root directory)
1159                                 importPath = dir[len(root)+1:]
1160                         }
1161                         // We started with parent's dir c:\gopath\src\foo\bar\baz\quux\xyzzy.
1162                         // We know the import path for parent's dir.
1163                         // We chopped off some number of path elements and
1164                         // added vendor\path to produce c:\gopath\src\foo\bar\baz\vendor\path.
1165                         // Now we want to know the import path for that directory.
1166                         // Construct it by chopping the same number of path elements
1167                         // (actually the same number of bytes) from parent's import path
1168                         // and then append /vendor/path.
1169                         chopped := len(dir) - i
1170                         if chopped == len(importPath)+1 {
1171                                 // We walked up from c:\gopath\src\foo\bar
1172                                 // and found c:\gopath\src\vendor\path.
1173                                 // We chopped \foo\bar (length 8) but the import path is "foo/bar" (length 7).
1174                                 // Use "vendor/path" without any prefix.
1175                                 return vpath
1176                         }
1177                         return importPath[:len(importPath)-chopped] + "/" + vpath
1178                 }
1179         }
1180         return path
1181 }
1182
1183 var (
1184         modulePrefix   = []byte("\nmodule ")
1185         goModPathCache par.Cache
1186 )
1187
1188 // goModPath returns the module path in the go.mod in dir, if any.
1189 func goModPath(dir string) (path string) {
1190         return goModPathCache.Do(dir, func() interface{} {
1191                 data, err := os.ReadFile(filepath.Join(dir, "go.mod"))
1192                 if err != nil {
1193                         return ""
1194                 }
1195                 var i int
1196                 if bytes.HasPrefix(data, modulePrefix[1:]) {
1197                         i = 0
1198                 } else {
1199                         i = bytes.Index(data, modulePrefix)
1200                         if i < 0 {
1201                                 return ""
1202                         }
1203                         i++
1204                 }
1205                 line := data[i:]
1206
1207                 // Cut line at \n, drop trailing \r if present.
1208                 if j := bytes.IndexByte(line, '\n'); j >= 0 {
1209                         line = line[:j]
1210                 }
1211                 if line[len(line)-1] == '\r' {
1212                         line = line[:len(line)-1]
1213                 }
1214                 line = line[len("module "):]
1215
1216                 // If quoted, unquote.
1217                 path = strings.TrimSpace(string(line))
1218                 if path != "" && path[0] == '"' {
1219                         s, err := strconv.Unquote(path)
1220                         if err != nil {
1221                                 return ""
1222                         }
1223                         path = s
1224                 }
1225                 return path
1226         }).(string)
1227 }
1228
1229 // findVersionElement returns the slice indices of the final version element /vN in path.
1230 // If there is no such element, it returns -1, -1.
1231 func findVersionElement(path string) (i, j int) {
1232         j = len(path)
1233         for i = len(path) - 1; i >= 0; i-- {
1234                 if path[i] == '/' {
1235                         if isVersionElement(path[i+1 : j]) {
1236                                 return i, j
1237                         }
1238                         j = i
1239                 }
1240         }
1241         return -1, -1
1242 }
1243
1244 // isVersionElement reports whether s is a well-formed path version element:
1245 // v2, v3, v10, etc, but not v0, v05, v1.
1246 func isVersionElement(s string) bool {
1247         if len(s) < 2 || s[0] != 'v' || s[1] == '0' || s[1] == '1' && len(s) == 2 {
1248                 return false
1249         }
1250         for i := 1; i < len(s); i++ {
1251                 if s[i] < '0' || '9' < s[i] {
1252                         return false
1253                 }
1254         }
1255         return true
1256 }
1257
1258 // moduleImportPath translates import paths found in go modules
1259 // back down to paths that can be resolved in ordinary builds.
1260 //
1261 // Define “new” code as code with a go.mod file in the same directory
1262 // or a parent directory. If an import in new code says x/y/v2/z but
1263 // x/y/v2/z does not exist and x/y/go.mod says “module x/y/v2”,
1264 // then go build will read the import as x/y/z instead.
1265 // See golang.org/issue/25069.
1266 func moduleImportPath(path, parentPath, parentDir, parentRoot string) (found string) {
1267         if parentRoot == "" {
1268                 return path
1269         }
1270
1271         // If there are no vN elements in path, leave it alone.
1272         // (The code below would do the same, but only after
1273         // some other file system accesses that we can avoid
1274         // here by returning early.)
1275         if i, _ := findVersionElement(path); i < 0 {
1276                 return path
1277         }
1278
1279         dir, root := dirAndRoot(parentPath, parentDir, parentRoot)
1280
1281         // Consider dir and parents, up to and including root.
1282         for i := len(dir); i >= len(root); i-- {
1283                 if i < len(dir) && dir[i] != filepath.Separator {
1284                         continue
1285                 }
1286                 if goModPath(dir[:i]) != "" {
1287                         goto HaveGoMod
1288                 }
1289         }
1290         // This code is not in a tree with a go.mod,
1291         // so apply no changes to the path.
1292         return path
1293
1294 HaveGoMod:
1295         // This import is in a tree with a go.mod.
1296         // Allow it to refer to code in GOPATH/src/x/y/z as x/y/v2/z
1297         // if GOPATH/src/x/y/go.mod says module "x/y/v2",
1298
1299         // If x/y/v2/z exists, use it unmodified.
1300         if bp, _ := cfg.BuildContext.Import(path, "", build.IgnoreVendor); bp.Dir != "" {
1301                 return path
1302         }
1303
1304         // Otherwise look for a go.mod supplying a version element.
1305         // Some version-like elements may appear in paths but not
1306         // be module versions; we skip over those to look for module
1307         // versions. For example the module m/v2 might have a
1308         // package m/v2/api/v1/foo.
1309         limit := len(path)
1310         for limit > 0 {
1311                 i, j := findVersionElement(path[:limit])
1312                 if i < 0 {
1313                         return path
1314                 }
1315                 if bp, _ := cfg.BuildContext.Import(path[:i], "", build.IgnoreVendor); bp.Dir != "" {
1316                         if mpath := goModPath(bp.Dir); mpath != "" {
1317                                 // Found a valid go.mod file, so we're stopping the search.
1318                                 // If the path is m/v2/p and we found m/go.mod that says
1319                                 // "module m/v2", then we return "m/p".
1320                                 if mpath == path[:j] {
1321                                         return path[:i] + path[j:]
1322                                 }
1323                                 // Otherwise just return the original path.
1324                                 // We didn't find anything worth rewriting,
1325                                 // and the go.mod indicates that we should
1326                                 // not consider parent directories.
1327                                 return path
1328                         }
1329                 }
1330                 limit = i
1331         }
1332         return path
1333 }
1334
1335 // hasGoFiles reports whether dir contains any files with names ending in .go.
1336 // For a vendor check we must exclude directories that contain no .go files.
1337 // Otherwise it is not possible to vendor just a/b/c and still import the
1338 // non-vendored a/b. See golang.org/issue/13832.
1339 func hasGoFiles(dir string) bool {
1340         files, _ := os.ReadDir(dir)
1341         for _, f := range files {
1342                 if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") {
1343                         return true
1344                 }
1345         }
1346         return false
1347 }
1348
1349 // reusePackage reuses package p to satisfy the import at the top
1350 // of the import stack stk. If this use causes an import loop,
1351 // reusePackage updates p's error information to record the loop.
1352 func reusePackage(p *Package, stk *ImportStack) *Package {
1353         // We use p.Internal.Imports==nil to detect a package that
1354         // is in the midst of its own loadPackage call
1355         // (all the recursion below happens before p.Internal.Imports gets set).
1356         if p.Internal.Imports == nil {
1357                 if p.Error == nil {
1358                         p.Error = &PackageError{
1359                                 ImportStack:   stk.Copy(),
1360                                 Err:           errors.New("import cycle not allowed"),
1361                                 IsImportCycle: true,
1362                         }
1363                 } else if !p.Error.IsImportCycle {
1364                         // If the error is already set, but it does not indicate that
1365                         // we are in an import cycle, set IsImportCycle so that we don't
1366                         // end up stuck in a loop down the road.
1367                         p.Error.IsImportCycle = true
1368                 }
1369                 p.Incomplete = true
1370         }
1371         // Don't rewrite the import stack in the error if we have an import cycle.
1372         // If we do, we'll lose the path that describes the cycle.
1373         if p.Error != nil && !p.Error.IsImportCycle && stk.shorterThan(p.Error.ImportStack) {
1374                 p.Error.ImportStack = stk.Copy()
1375         }
1376         return p
1377 }
1378
1379 // disallowInternal checks that srcDir (containing package importerPath, if non-empty)
1380 // is allowed to import p.
1381 // If the import is allowed, disallowInternal returns the original package p.
1382 // If not, it returns a new package containing just an appropriate error.
1383 func disallowInternal(ctx context.Context, srcDir string, importer *Package, importerPath string, p *Package, stk *ImportStack) *Package {
1384         // golang.org/s/go14internal:
1385         // An import of a path containing the element “internal”
1386         // is disallowed if the importing code is outside the tree
1387         // rooted at the parent of the “internal” directory.
1388
1389         // There was an error loading the package; stop here.
1390         if p.Error != nil {
1391                 return p
1392         }
1393
1394         // The generated 'testmain' package is allowed to access testing/internal/...,
1395         // as if it were generated into the testing directory tree
1396         // (it's actually in a temporary directory outside any Go tree).
1397         // This cleans up a former kludge in passing functionality to the testing package.
1398         if str.HasPathPrefix(p.ImportPath, "testing/internal") && importerPath == "testmain" {
1399                 return p
1400         }
1401
1402         // We can't check standard packages with gccgo.
1403         if cfg.BuildContext.Compiler == "gccgo" && p.Standard {
1404                 return p
1405         }
1406
1407         // The sort package depends on internal/reflectlite, but during bootstrap
1408         // the path rewriting causes the normal internal checks to fail.
1409         // Instead, just ignore the internal rules during bootstrap.
1410         if p.Standard && strings.HasPrefix(importerPath, "bootstrap/") {
1411                 return p
1412         }
1413
1414         // importerPath is empty: we started
1415         // with a name given on the command line, not an
1416         // import. Anything listed on the command line is fine.
1417         if importerPath == "" {
1418                 return p
1419         }
1420
1421         // Check for "internal" element: three cases depending on begin of string and/or end of string.
1422         i, ok := findInternal(p.ImportPath)
1423         if !ok {
1424                 return p
1425         }
1426
1427         // Internal is present.
1428         // Map import path back to directory corresponding to parent of internal.
1429         if i > 0 {
1430                 i-- // rewind over slash in ".../internal"
1431         }
1432
1433         if p.Module == nil {
1434                 parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
1435
1436                 if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
1437                         return p
1438                 }
1439
1440                 // Look for symlinks before reporting error.
1441                 srcDir = expandPath(srcDir)
1442                 parent = expandPath(parent)
1443                 if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
1444                         return p
1445                 }
1446         } else {
1447                 // p is in a module, so make it available based on the importer's import path instead
1448                 // of the file path (https://golang.org/issue/23970).
1449                 if importer.Internal.CmdlineFiles {
1450                         // The importer is a list of command-line files.
1451                         // Pretend that the import path is the import path of the
1452                         // directory containing them.
1453                         // If the directory is outside the main module, this will resolve to ".",
1454                         // which is not a prefix of any valid module.
1455                         importerPath = modload.DirImportPath(ctx, importer.Dir)
1456                 }
1457                 parentOfInternal := p.ImportPath[:i]
1458                 if str.HasPathPrefix(importerPath, parentOfInternal) {
1459                         return p
1460                 }
1461         }
1462
1463         // Internal is present, and srcDir is outside parent's tree. Not allowed.
1464         perr := *p
1465         perr.Error = &PackageError{
1466                 alwaysPrintStack: true,
1467                 ImportStack:      stk.Copy(),
1468                 Err:              ImportErrorf(p.ImportPath, "use of internal package "+p.ImportPath+" not allowed"),
1469         }
1470         perr.Incomplete = true
1471         return &perr
1472 }
1473
1474 // findInternal looks for the final "internal" path element in the given import path.
1475 // If there isn't one, findInternal returns ok=false.
1476 // Otherwise, findInternal returns ok=true and the index of the "internal".
1477 func findInternal(path string) (index int, ok bool) {
1478         // Three cases, depending on internal at start/end of string or not.
1479         // The order matters: we must return the index of the final element,
1480         // because the final one produces the most restrictive requirement
1481         // on the importer.
1482         switch {
1483         case strings.HasSuffix(path, "/internal"):
1484                 return len(path) - len("internal"), true
1485         case strings.Contains(path, "/internal/"):
1486                 return strings.LastIndex(path, "/internal/") + 1, true
1487         case path == "internal", strings.HasPrefix(path, "internal/"):
1488                 return 0, true
1489         }
1490         return 0, false
1491 }
1492
1493 // disallowVendor checks that srcDir is allowed to import p as path.
1494 // If the import is allowed, disallowVendor returns the original package p.
1495 // If not, it returns a new package containing just an appropriate error.
1496 func disallowVendor(srcDir string, path string, importerPath string, p *Package, stk *ImportStack) *Package {
1497         // If the importerPath is empty, we started
1498         // with a name given on the command line, not an
1499         // import. Anything listed on the command line is fine.
1500         if importerPath == "" {
1501                 return p
1502         }
1503
1504         if perr := disallowVendorVisibility(srcDir, p, importerPath, stk); perr != p {
1505                 return perr
1506         }
1507
1508         // Paths like x/vendor/y must be imported as y, never as x/vendor/y.
1509         if i, ok := FindVendor(path); ok {
1510                 perr := *p
1511                 perr.Error = &PackageError{
1512                         ImportStack: stk.Copy(),
1513                         Err:         ImportErrorf(path, "%s must be imported as %s", path, path[i+len("vendor/"):]),
1514                 }
1515                 perr.Incomplete = true
1516                 return &perr
1517         }
1518
1519         return p
1520 }
1521
1522 // disallowVendorVisibility checks that srcDir is allowed to import p.
1523 // The rules are the same as for /internal/ except that a path ending in /vendor
1524 // is not subject to the rules, only subdirectories of vendor.
1525 // This allows people to have packages and commands named vendor,
1526 // for maximal compatibility with existing source trees.
1527 func disallowVendorVisibility(srcDir string, p *Package, importerPath string, stk *ImportStack) *Package {
1528         // The stack does not include p.ImportPath.
1529         // If there's nothing on the stack, we started
1530         // with a name given on the command line, not an
1531         // import. Anything listed on the command line is fine.
1532         if importerPath == "" {
1533                 return p
1534         }
1535
1536         // Check for "vendor" element.
1537         i, ok := FindVendor(p.ImportPath)
1538         if !ok {
1539                 return p
1540         }
1541
1542         // Vendor is present.
1543         // Map import path back to directory corresponding to parent of vendor.
1544         if i > 0 {
1545                 i-- // rewind over slash in ".../vendor"
1546         }
1547         truncateTo := i + len(p.Dir) - len(p.ImportPath)
1548         if truncateTo < 0 || len(p.Dir) < truncateTo {
1549                 return p
1550         }
1551         parent := p.Dir[:truncateTo]
1552         if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
1553                 return p
1554         }
1555
1556         // Look for symlinks before reporting error.
1557         srcDir = expandPath(srcDir)
1558         parent = expandPath(parent)
1559         if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
1560                 return p
1561         }
1562
1563         // Vendor is present, and srcDir is outside parent's tree. Not allowed.
1564         perr := *p
1565         perr.Error = &PackageError{
1566                 ImportStack: stk.Copy(),
1567                 Err:         errors.New("use of vendored package not allowed"),
1568         }
1569         perr.Incomplete = true
1570         return &perr
1571 }
1572
1573 // FindVendor looks for the last non-terminating "vendor" path element in the given import path.
1574 // If there isn't one, FindVendor returns ok=false.
1575 // Otherwise, FindVendor returns ok=true and the index of the "vendor".
1576 //
1577 // Note that terminating "vendor" elements don't count: "x/vendor" is its own package,
1578 // not the vendored copy of an import "" (the empty import path).
1579 // This will allow people to have packages or commands named vendor.
1580 // This may help reduce breakage, or it may just be confusing. We'll see.
1581 func FindVendor(path string) (index int, ok bool) {
1582         // Two cases, depending on internal at start of string or not.
1583         // The order matters: we must return the index of the final element,
1584         // because the final one is where the effective import path starts.
1585         switch {
1586         case strings.Contains(path, "/vendor/"):
1587                 return strings.LastIndex(path, "/vendor/") + 1, true
1588         case strings.HasPrefix(path, "vendor/"):
1589                 return 0, true
1590         }
1591         return 0, false
1592 }
1593
1594 type TargetDir int
1595
1596 const (
1597         ToTool    TargetDir = iota // to GOROOT/pkg/tool (default for cmd/*)
1598         ToBin                      // to bin dir inside package root (default for non-cmd/*)
1599         StalePath                  // an old import path; fail to build
1600 )
1601
1602 // InstallTargetDir reports the target directory for installing the command p.
1603 func InstallTargetDir(p *Package) TargetDir {
1604         if strings.HasPrefix(p.ImportPath, "code.google.com/p/go.tools/cmd/") {
1605                 return StalePath
1606         }
1607         if p.Goroot && strings.HasPrefix(p.ImportPath, "cmd/") && p.Name == "main" {
1608                 switch p.ImportPath {
1609                 case "cmd/go", "cmd/gofmt":
1610                         return ToBin
1611                 }
1612                 return ToTool
1613         }
1614         return ToBin
1615 }
1616
1617 var cgoExclude = map[string]bool{
1618         "runtime/cgo": true,
1619 }
1620
1621 var cgoSyscallExclude = map[string]bool{
1622         "runtime/cgo":  true,
1623         "runtime/race": true,
1624         "runtime/msan": true,
1625 }
1626
1627 var foldPath = make(map[string]string)
1628
1629 // exeFromImportPath returns an executable name
1630 // for a package using the import path.
1631 //
1632 // The executable name is the last element of the import path.
1633 // In module-aware mode, an additional rule is used on import paths
1634 // consisting of two or more path elements. If the last element is
1635 // a vN path element specifying the major version, then the
1636 // second last element of the import path is used instead.
1637 func (p *Package) exeFromImportPath() string {
1638         _, elem := pathpkg.Split(p.ImportPath)
1639         if cfg.ModulesEnabled {
1640                 // If this is example.com/mycmd/v2, it's more useful to
1641                 // install it as mycmd than as v2. See golang.org/issue/24667.
1642                 if elem != p.ImportPath && isVersionElement(elem) {
1643                         _, elem = pathpkg.Split(pathpkg.Dir(p.ImportPath))
1644                 }
1645         }
1646         return elem
1647 }
1648
1649 // exeFromFiles returns an executable name for a package
1650 // using the first element in GoFiles or CgoFiles collections without the prefix.
1651 //
1652 // Returns empty string in case of empty collection.
1653 func (p *Package) exeFromFiles() string {
1654         var src string
1655         if len(p.GoFiles) > 0 {
1656                 src = p.GoFiles[0]
1657         } else if len(p.CgoFiles) > 0 {
1658                 src = p.CgoFiles[0]
1659         } else {
1660                 return ""
1661         }
1662         _, elem := filepath.Split(src)
1663         return elem[:len(elem)-len(".go")]
1664 }
1665
1666 // DefaultExecName returns the default executable name for a package
1667 func (p *Package) DefaultExecName() string {
1668         if p.Internal.CmdlineFiles {
1669                 return p.exeFromFiles()
1670         }
1671         return p.exeFromImportPath()
1672 }
1673
1674 // load populates p using information from bp, err, which should
1675 // be the result of calling build.Context.Import.
1676 // stk contains the import stack, not including path itself.
1677 func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *ImportStack, importPos []token.Position, bp *build.Package, err error) {
1678         p.copyBuild(opts, bp)
1679
1680         // The localPrefix is the path we interpret ./ imports relative to.
1681         // Synthesized main packages sometimes override this.
1682         if p.Internal.Local {
1683                 p.Internal.LocalPrefix = dirToImportPath(p.Dir)
1684         }
1685
1686         // setError sets p.Error if it hasn't already been set. We may proceed
1687         // after encountering some errors so that 'go list -e' has more complete
1688         // output. If there's more than one error, we should report the first.
1689         setError := func(err error) {
1690                 if p.Error == nil {
1691                         p.Error = &PackageError{
1692                                 ImportStack: stk.Copy(),
1693                                 Err:         err,
1694                         }
1695
1696                         // Add the importer's position information if the import position exists, and
1697                         // the current package being examined is the importer.
1698                         // If we have not yet accepted package p onto the import stack,
1699                         // then the cause of the error is not within p itself: the error
1700                         // must be either in an explicit command-line argument,
1701                         // or on the importer side (indicated by a non-empty importPos).
1702                         if path != stk.Top() && len(importPos) > 0 {
1703                                 p.Error.setPos(importPos)
1704                         }
1705                 }
1706         }
1707
1708         if err != nil {
1709                 p.Incomplete = true
1710                 p.setLoadPackageDataError(err, path, stk, importPos)
1711         }
1712
1713         useBindir := p.Name == "main"
1714         if !p.Standard {
1715                 switch cfg.BuildBuildmode {
1716                 case "c-archive", "c-shared", "plugin":
1717                         useBindir = false
1718                 }
1719         }
1720
1721         if useBindir {
1722                 // Report an error when the old code.google.com/p/go.tools paths are used.
1723                 if InstallTargetDir(p) == StalePath {
1724                         // TODO(matloob): remove this branch, and StalePath itself. code.google.com/p/go is so
1725                         // old, even this code checking for it is stale now!
1726                         newPath := strings.Replace(p.ImportPath, "code.google.com/p/go.", "golang.org/x/", 1)
1727                         e := ImportErrorf(p.ImportPath, "the %v command has moved; use %v instead.", p.ImportPath, newPath)
1728                         setError(e)
1729                         return
1730                 }
1731                 elem := p.DefaultExecName()
1732                 full := cfg.BuildContext.GOOS + "_" + cfg.BuildContext.GOARCH + "/" + elem
1733                 if cfg.BuildContext.GOOS != base.ToolGOOS || cfg.BuildContext.GOARCH != base.ToolGOARCH {
1734                         // Install cross-compiled binaries to subdirectories of bin.
1735                         elem = full
1736                 }
1737                 if p.Internal.Build.BinDir == "" && cfg.ModulesEnabled {
1738                         p.Internal.Build.BinDir = modload.BinDir()
1739                 }
1740                 if p.Internal.Build.BinDir != "" {
1741                         // Install to GOBIN or bin of GOPATH entry.
1742                         p.Target = filepath.Join(p.Internal.Build.BinDir, elem)
1743                         if !p.Goroot && strings.Contains(elem, "/") && cfg.GOBIN != "" {
1744                                 // Do not create $GOBIN/goos_goarch/elem.
1745                                 p.Target = ""
1746                                 p.Internal.GobinSubdir = true
1747                         }
1748                 }
1749                 if InstallTargetDir(p) == ToTool {
1750                         // This is for 'go tool'.
1751                         // Override all the usual logic and force it into the tool directory.
1752                         if cfg.BuildToolchainName == "gccgo" {
1753                                 p.Target = filepath.Join(base.ToolDir, elem)
1754                         } else {
1755                                 p.Target = filepath.Join(cfg.GOROOTpkg, "tool", full)
1756                         }
1757                 }
1758                 if p.Target != "" && cfg.BuildContext.GOOS == "windows" {
1759                         p.Target += ".exe"
1760                 }
1761         } else if p.Internal.Local {
1762                 // Local import turned into absolute path.
1763                 // No permanent install target.
1764                 p.Target = ""
1765         } else {
1766                 p.Target = p.Internal.Build.PkgObj
1767                 if cfg.BuildLinkshared && p.Target != "" {
1768                         // TODO(bcmills): The reliance on p.Target implies that -linkshared does
1769                         // not work for any package that lacks a Target — such as a non-main
1770                         // package in module mode. We should probably fix that.
1771                         shlibnamefile := p.Target[:len(p.Target)-2] + ".shlibname"
1772                         shlib, err := os.ReadFile(shlibnamefile)
1773                         if err != nil && !os.IsNotExist(err) {
1774                                 base.Fatalf("reading shlibname: %v", err)
1775                         }
1776                         if err == nil {
1777                                 libname := strings.TrimSpace(string(shlib))
1778                                 if cfg.BuildContext.Compiler == "gccgo" {
1779                                         p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, "shlibs", libname)
1780                                 } else {
1781                                         p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, libname)
1782                                 }
1783                         }
1784                 }
1785         }
1786
1787         // Build augmented import list to add implicit dependencies.
1788         // Be careful not to add imports twice, just to avoid confusion.
1789         importPaths := p.Imports
1790         addImport := func(path string, forCompiler bool) {
1791                 for _, p := range importPaths {
1792                         if path == p {
1793                                 return
1794                         }
1795                 }
1796                 importPaths = append(importPaths, path)
1797                 if forCompiler {
1798                         p.Internal.CompiledImports = append(p.Internal.CompiledImports, path)
1799                 }
1800         }
1801
1802         if !opts.IgnoreImports {
1803                 // Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
1804                 // except for certain packages, to avoid circular dependencies.
1805                 if p.UsesCgo() {
1806                         addImport("unsafe", true)
1807                 }
1808                 if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
1809                         addImport("runtime/cgo", true)
1810                 }
1811                 if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
1812                         addImport("syscall", true)
1813                 }
1814
1815                 // SWIG adds imports of some standard packages.
1816                 if p.UsesSwig() {
1817                         addImport("unsafe", true)
1818                         if cfg.BuildContext.Compiler != "gccgo" {
1819                                 addImport("runtime/cgo", true)
1820                         }
1821                         addImport("syscall", true)
1822                         addImport("sync", true)
1823
1824                         // TODO: The .swig and .swigcxx files can use
1825                         // %go_import directives to import other packages.
1826                 }
1827
1828                 // The linker loads implicit dependencies.
1829                 if p.Name == "main" && !p.Internal.ForceLibrary {
1830                         for _, dep := range LinkerDeps(p) {
1831                                 addImport(dep, false)
1832                         }
1833                 }
1834         }
1835
1836         // Check for case-insensitive collisions of import paths.
1837         fold := str.ToFold(p.ImportPath)
1838         if other := foldPath[fold]; other == "" {
1839                 foldPath[fold] = p.ImportPath
1840         } else if other != p.ImportPath {
1841                 setError(ImportErrorf(p.ImportPath, "case-insensitive import collision: %q and %q", p.ImportPath, other))
1842                 return
1843         }
1844
1845         if !SafeArg(p.ImportPath) {
1846                 setError(ImportErrorf(p.ImportPath, "invalid import path %q", p.ImportPath))
1847                 return
1848         }
1849
1850         // Errors after this point are caused by this package, not the importing
1851         // package. Pushing the path here prevents us from reporting the error
1852         // with the position of the import declaration.
1853         stk.Push(path)
1854         defer stk.Pop()
1855
1856         pkgPath := p.ImportPath
1857         if p.Internal.CmdlineFiles {
1858                 pkgPath = "command-line-arguments"
1859         }
1860         if cfg.ModulesEnabled {
1861                 p.Module = modload.PackageModuleInfo(ctx, pkgPath)
1862         }
1863
1864         p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns)
1865         if err != nil {
1866                 p.Incomplete = true
1867                 setError(err)
1868                 embedErr := err.(*EmbedError)
1869                 p.Error.setPos(p.Internal.Build.EmbedPatternPos[embedErr.Pattern])
1870         }
1871
1872         // Check for case-insensitive collision of input files.
1873         // To avoid problems on case-insensitive files, we reject any package
1874         // where two different input files have equal names under a case-insensitive
1875         // comparison.
1876         inputs := p.AllFiles()
1877         f1, f2 := str.FoldDup(inputs)
1878         if f1 != "" {
1879                 setError(fmt.Errorf("case-insensitive file name collision: %q and %q", f1, f2))
1880                 return
1881         }
1882
1883         // If first letter of input file is ASCII, it must be alphanumeric.
1884         // This avoids files turning into flags when invoking commands,
1885         // and other problems we haven't thought of yet.
1886         // Also, _cgo_ files must be generated by us, not supplied.
1887         // They are allowed to have //go:cgo_ldflag directives.
1888         // The directory scan ignores files beginning with _,
1889         // so we shouldn't see any _cgo_ files anyway, but just be safe.
1890         for _, file := range inputs {
1891                 if !SafeArg(file) || strings.HasPrefix(file, "_cgo_") {
1892                         setError(fmt.Errorf("invalid input file name %q", file))
1893                         return
1894                 }
1895         }
1896         if name := pathpkg.Base(p.ImportPath); !SafeArg(name) {
1897                 setError(fmt.Errorf("invalid input directory name %q", name))
1898                 return
1899         }
1900
1901         // Build list of imported packages and full dependency list.
1902         imports := make([]*Package, 0, len(p.Imports))
1903         for i, path := range importPaths {
1904                 if path == "C" {
1905                         continue
1906                 }
1907                 p1 := LoadImport(ctx, opts, path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport)
1908
1909                 path = p1.ImportPath
1910                 importPaths[i] = path
1911                 if i < len(p.Imports) {
1912                         p.Imports[i] = path
1913                 }
1914
1915                 imports = append(imports, p1)
1916                 if p1.Incomplete {
1917                         p.Incomplete = true
1918                 }
1919         }
1920         p.Internal.Imports = imports
1921         p.collectDeps()
1922
1923         if cfg.ModulesEnabled && p.Error == nil && p.Name == "main" && len(p.DepsErrors) == 0 {
1924                 p.Internal.BuildInfo = modload.PackageBuildInfo(pkgPath, p.Deps)
1925         }
1926
1927         // unsafe is a fake package.
1928         if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
1929                 p.Target = ""
1930         }
1931
1932         // If cgo is not enabled, ignore cgo supporting sources
1933         // just as we ignore go files containing import "C".
1934         if !cfg.BuildContext.CgoEnabled {
1935                 p.CFiles = nil
1936                 p.CXXFiles = nil
1937                 p.MFiles = nil
1938                 p.SwigFiles = nil
1939                 p.SwigCXXFiles = nil
1940                 // Note that SFiles are okay (they go to the Go assembler)
1941                 // and HFiles are okay (they might be used by the SFiles).
1942                 // Also Sysofiles are okay (they might not contain object
1943                 // code; see issue #16050).
1944         }
1945
1946         // The gc toolchain only permits C source files with cgo or SWIG.
1947         if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" {
1948                 setError(fmt.Errorf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")))
1949                 return
1950         }
1951
1952         // C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,
1953         // regardless of toolchain.
1954         if len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1955                 setError(fmt.Errorf("C++ source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CXXFiles, " ")))
1956                 return
1957         }
1958         if len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1959                 setError(fmt.Errorf("Objective-C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.MFiles, " ")))
1960                 return
1961         }
1962         if len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1963                 setError(fmt.Errorf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
1964                 return
1965         }
1966 }
1967
1968 // An EmbedError indicates a problem with a go:embed directive.
1969 type EmbedError struct {
1970         Pattern string
1971         Err     error
1972 }
1973
1974 func (e *EmbedError) Error() string {
1975         return fmt.Sprintf("pattern %s: %v", e.Pattern, e.Err)
1976 }
1977
1978 func (e *EmbedError) Unwrap() error {
1979         return e.Err
1980 }
1981
1982 // ResolveEmbed resolves //go:embed patterns and returns only the file list.
1983 // For use by go mod vendor to find embedded files it should copy into the
1984 // vendor directory.
1985 // TODO(#42504): Once go mod vendor uses load.PackagesAndErrors, just
1986 // call (*Package).ResolveEmbed
1987 func ResolveEmbed(dir string, patterns []string) ([]string, error) {
1988         files, _, err := resolveEmbed(dir, patterns)
1989         return files, err
1990 }
1991
1992 // resolveEmbed resolves //go:embed patterns to precise file lists.
1993 // It sets files to the list of unique files matched (for go list),
1994 // and it sets pmap to the more precise mapping from
1995 // patterns to files.
1996 func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[string][]string, err error) {
1997         var pattern string
1998         defer func() {
1999                 if err != nil {
2000                         err = &EmbedError{
2001                                 Pattern: pattern,
2002                                 Err:     err,
2003                         }
2004                 }
2005         }()
2006
2007         // TODO(rsc): All these messages need position information for better error reports.
2008         pmap = make(map[string][]string)
2009         have := make(map[string]int)
2010         dirOK := make(map[string]bool)
2011         pid := 0 // pattern ID, to allow reuse of have map
2012         for _, pattern = range patterns {
2013                 pid++
2014
2015                 // Check pattern is valid for //go:embed.
2016                 if _, err := path.Match(pattern, ""); err != nil || !validEmbedPattern(pattern) {
2017                         return nil, nil, fmt.Errorf("invalid pattern syntax")
2018                 }
2019
2020                 // Glob to find matches.
2021                 match, err := fsys.Glob(pkgdir + string(filepath.Separator) + filepath.FromSlash(pattern))
2022                 if err != nil {
2023                         return nil, nil, err
2024                 }
2025
2026                 // Filter list of matches down to the ones that will still exist when
2027                 // the directory is packaged up as a module. (If p.Dir is in the module cache,
2028                 // only those files exist already, but if p.Dir is in the current module,
2029                 // then there may be other things lying around, like symbolic links or .git directories.)
2030                 var list []string
2031                 for _, file := range match {
2032                         rel := filepath.ToSlash(file[len(pkgdir)+1:]) // file, relative to p.Dir
2033
2034                         what := "file"
2035                         info, err := fsys.Lstat(file)
2036                         if err != nil {
2037                                 return nil, nil, err
2038                         }
2039                         if info.IsDir() {
2040                                 what = "directory"
2041                         }
2042
2043                         // Check that directories along path do not begin a new module
2044                         // (do not contain a go.mod).
2045                         for dir := file; len(dir) > len(pkgdir)+1 && !dirOK[dir]; dir = filepath.Dir(dir) {
2046                                 if _, err := fsys.Stat(filepath.Join(dir, "go.mod")); err == nil {
2047                                         return nil, nil, fmt.Errorf("cannot embed %s %s: in different module", what, rel)
2048                                 }
2049                                 if dir != file {
2050                                         if info, err := fsys.Lstat(dir); err == nil && !info.IsDir() {
2051                                                 return nil, nil, fmt.Errorf("cannot embed %s %s: in non-directory %s", what, rel, dir[len(pkgdir)+1:])
2052                                         }
2053                                 }
2054                                 dirOK[dir] = true
2055                                 if elem := filepath.Base(dir); isBadEmbedName(elem) {
2056                                         if dir == file {
2057                                                 return nil, nil, fmt.Errorf("cannot embed %s %s: invalid name %s", what, rel, elem)
2058                                         } else {
2059                                                 return nil, nil, fmt.Errorf("cannot embed %s %s: in invalid directory %s", what, rel, elem)
2060                                         }
2061                                 }
2062                         }
2063
2064                         switch {
2065                         default:
2066                                 return nil, nil, fmt.Errorf("cannot embed irregular file %s", rel)
2067
2068                         case info.Mode().IsRegular():
2069                                 if have[rel] != pid {
2070                                         have[rel] = pid
2071                                         list = append(list, rel)
2072                                 }
2073
2074                         case info.IsDir():
2075                                 // Gather all files in the named directory, stopping at module boundaries
2076                                 // and ignoring files that wouldn't be packaged into a module.
2077                                 count := 0
2078                                 err := fsys.Walk(file, func(path string, info os.FileInfo, err error) error {
2079                                         if err != nil {
2080                                                 return err
2081                                         }
2082                                         rel := filepath.ToSlash(path[len(pkgdir)+1:])
2083                                         name := info.Name()
2084                                         if path != file && (isBadEmbedName(name) || name[0] == '.' || name[0] == '_') {
2085                                                 // Ignore bad names, assuming they won't go into modules.
2086                                                 // Also avoid hidden files that user may not know about.
2087                                                 // See golang.org/issue/42328.
2088                                                 if info.IsDir() {
2089                                                         return fs.SkipDir
2090                                                 }
2091                                                 return nil
2092                                         }
2093                                         if info.IsDir() {
2094                                                 if _, err := fsys.Stat(filepath.Join(path, "go.mod")); err == nil {
2095                                                         return filepath.SkipDir
2096                                                 }
2097                                                 return nil
2098                                         }
2099                                         if !info.Mode().IsRegular() {
2100                                                 return nil
2101                                         }
2102                                         count++
2103                                         if have[rel] != pid {
2104                                                 have[rel] = pid
2105                                                 list = append(list, rel)
2106                                         }
2107                                         return nil
2108                                 })
2109                                 if err != nil {
2110                                         return nil, nil, err
2111                                 }
2112                                 if count == 0 {
2113                                         return nil, nil, fmt.Errorf("cannot embed directory %s: contains no embeddable files", rel)
2114                                 }
2115                         }
2116                 }
2117
2118                 if len(list) == 0 {
2119                         return nil, nil, fmt.Errorf("no matching files found")
2120                 }
2121                 sort.Strings(list)
2122                 pmap[pattern] = list
2123         }
2124
2125         for file := range have {
2126                 files = append(files, file)
2127         }
2128         sort.Strings(files)
2129         return files, pmap, nil
2130 }
2131
2132 func validEmbedPattern(pattern string) bool {
2133         return pattern != "." && fs.ValidPath(pattern)
2134 }
2135
2136 // isBadEmbedName reports whether name is the base name of a file that
2137 // can't or won't be included in modules and therefore shouldn't be treated
2138 // as existing for embedding.
2139 func isBadEmbedName(name string) bool {
2140         if err := module.CheckFilePath(name); err != nil {
2141                 return true
2142         }
2143         switch name {
2144         // Empty string should be impossible but make it bad.
2145         case "":
2146                 return true
2147         // Version control directories won't be present in module.
2148         case ".bzr", ".hg", ".git", ".svn":
2149                 return true
2150         }
2151         return false
2152 }
2153
2154 // collectDeps populates p.Deps and p.DepsErrors by iterating over
2155 // p.Internal.Imports.
2156 //
2157 // TODO(jayconrod): collectDeps iterates over transitive imports for every
2158 // package. We should only need to visit direct imports.
2159 func (p *Package) collectDeps() {
2160         deps := make(map[string]*Package)
2161         var q []*Package
2162         q = append(q, p.Internal.Imports...)
2163         for i := 0; i < len(q); i++ {
2164                 p1 := q[i]
2165                 path := p1.ImportPath
2166                 // The same import path could produce an error or not,
2167                 // depending on what tries to import it.
2168                 // Prefer to record entries with errors, so we can report them.
2169                 p0 := deps[path]
2170                 if p0 == nil || p1.Error != nil && (p0.Error == nil || len(p0.Error.ImportStack) > len(p1.Error.ImportStack)) {
2171                         deps[path] = p1
2172                         for _, p2 := range p1.Internal.Imports {
2173                                 if deps[p2.ImportPath] != p2 {
2174                                         q = append(q, p2)
2175                                 }
2176                         }
2177                 }
2178         }
2179
2180         p.Deps = make([]string, 0, len(deps))
2181         for dep := range deps {
2182                 p.Deps = append(p.Deps, dep)
2183         }
2184         sort.Strings(p.Deps)
2185         for _, dep := range p.Deps {
2186                 p1 := deps[dep]
2187                 if p1 == nil {
2188                         panic("impossible: missing entry in package cache for " + dep + " imported by " + p.ImportPath)
2189                 }
2190                 if p1.Error != nil {
2191                         p.DepsErrors = append(p.DepsErrors, p1.Error)
2192                 }
2193         }
2194 }
2195
2196 // SafeArg reports whether arg is a "safe" command-line argument,
2197 // meaning that when it appears in a command-line, it probably
2198 // doesn't have some special meaning other than its own name.
2199 // Obviously args beginning with - are not safe (they look like flags).
2200 // Less obviously, args beginning with @ are not safe (they look like
2201 // GNU binutils flagfile specifiers, sometimes called "response files").
2202 // To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.
2203 // We accept leading . _ and / as likely in file system paths.
2204 // There is a copy of this function in cmd/compile/internal/gc/noder.go.
2205 func SafeArg(name string) bool {
2206         if name == "" {
2207                 return false
2208         }
2209         c := name[0]
2210         return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
2211 }
2212
2213 // LinkerDeps returns the list of linker-induced dependencies for main package p.
2214 func LinkerDeps(p *Package) []string {
2215         // Everything links runtime.
2216         deps := []string{"runtime"}
2217
2218         // External linking mode forces an import of runtime/cgo.
2219         if externalLinkingForced(p) && cfg.BuildContext.Compiler != "gccgo" {
2220                 deps = append(deps, "runtime/cgo")
2221         }
2222         // On ARM with GOARM=5, it forces an import of math, for soft floating point.
2223         if cfg.Goarch == "arm" {
2224                 deps = append(deps, "math")
2225         }
2226         // Using the race detector forces an import of runtime/race.
2227         if cfg.BuildRace {
2228                 deps = append(deps, "runtime/race")
2229         }
2230         // Using memory sanitizer forces an import of runtime/msan.
2231         if cfg.BuildMSan {
2232                 deps = append(deps, "runtime/msan")
2233         }
2234
2235         return deps
2236 }
2237
2238 // externalLinkingForced reports whether external linking is being
2239 // forced even for programs that do not use cgo.
2240 func externalLinkingForced(p *Package) bool {
2241         if !cfg.BuildContext.CgoEnabled {
2242                 return false
2243         }
2244
2245         // Some targets must use external linking even inside GOROOT.
2246         switch cfg.BuildContext.GOOS {
2247         case "android":
2248                 if cfg.BuildContext.GOARCH != "arm64" {
2249                         return true
2250                 }
2251         case "ios":
2252                 return true
2253         }
2254
2255         // Currently build modes c-shared, pie (on systems that do not
2256         // support PIE with internal linking mode (currently all
2257         // systems: issue #18968)), plugin, and -linkshared force
2258         // external linking mode, as of course does
2259         // -ldflags=-linkmode=external. External linking mode forces
2260         // an import of runtime/cgo.
2261         // If there are multiple -linkmode options, the last one wins.
2262         pieCgo := cfg.BuildBuildmode == "pie" && !sys.InternalLinkPIESupported(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH)
2263         linkmodeExternal := false
2264         if p != nil {
2265                 ldflags := BuildLdflags.For(p)
2266                 for i := len(ldflags) - 1; i >= 0; i-- {
2267                         a := ldflags[i]
2268                         if a == "-linkmode=external" ||
2269                                 a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "external" {
2270                                 linkmodeExternal = true
2271                                 break
2272                         } else if a == "-linkmode=internal" ||
2273                                 a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "internal" {
2274                                 break
2275                         }
2276                 }
2277         }
2278
2279         return cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal
2280 }
2281
2282 // mkAbs rewrites list, which must be paths relative to p.Dir,
2283 // into a sorted list of absolute paths. It edits list in place but for
2284 // convenience also returns list back to its caller.
2285 func (p *Package) mkAbs(list []string) []string {
2286         for i, f := range list {
2287                 list[i] = filepath.Join(p.Dir, f)
2288         }
2289         sort.Strings(list)
2290         return list
2291 }
2292
2293 // InternalGoFiles returns the list of Go files being built for the package,
2294 // using absolute paths.
2295 func (p *Package) InternalGoFiles() []string {
2296         return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles))
2297 }
2298
2299 // InternalXGoFiles returns the list of Go files being built for the XTest package,
2300 // using absolute paths.
2301 func (p *Package) InternalXGoFiles() []string {
2302         return p.mkAbs(p.XTestGoFiles)
2303 }
2304
2305 // InternalGoFiles returns the list of all Go files possibly relevant for the package,
2306 // using absolute paths. "Possibly relevant" means that files are not excluded
2307 // due to build tags, but files with names beginning with . or _ are still excluded.
2308 func (p *Package) InternalAllGoFiles() []string {
2309         return p.mkAbs(str.StringList(p.IgnoredGoFiles, p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
2310 }
2311
2312 // usesSwig reports whether the package needs to run SWIG.
2313 func (p *Package) UsesSwig() bool {
2314         return len(p.SwigFiles) > 0 || len(p.SwigCXXFiles) > 0
2315 }
2316
2317 // usesCgo reports whether the package needs to run cgo
2318 func (p *Package) UsesCgo() bool {
2319         return len(p.CgoFiles) > 0
2320 }
2321
2322 // PackageList returns the list of packages in the dag rooted at roots
2323 // as visited in a depth-first post-order traversal.
2324 func PackageList(roots []*Package) []*Package {
2325         seen := map[*Package]bool{}
2326         all := []*Package{}
2327         var walk func(*Package)
2328         walk = func(p *Package) {
2329                 if seen[p] {
2330                         return
2331                 }
2332                 seen[p] = true
2333                 for _, p1 := range p.Internal.Imports {
2334                         walk(p1)
2335                 }
2336                 all = append(all, p)
2337         }
2338         for _, root := range roots {
2339                 walk(root)
2340         }
2341         return all
2342 }
2343
2344 // TestPackageList returns the list of packages in the dag rooted at roots
2345 // as visited in a depth-first post-order traversal, including the test
2346 // imports of the roots. This ignores errors in test packages.
2347 func TestPackageList(ctx context.Context, opts PackageOpts, roots []*Package) []*Package {
2348         seen := map[*Package]bool{}
2349         all := []*Package{}
2350         var walk func(*Package)
2351         walk = func(p *Package) {
2352                 if seen[p] {
2353                         return
2354                 }
2355                 seen[p] = true
2356                 for _, p1 := range p.Internal.Imports {
2357                         walk(p1)
2358                 }
2359                 all = append(all, p)
2360         }
2361         walkTest := func(root *Package, path string) {
2362                 var stk ImportStack
2363                 p1 := LoadImport(ctx, opts, path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
2364                 if p1.Error == nil {
2365                         walk(p1)
2366                 }
2367         }
2368         for _, root := range roots {
2369                 walk(root)
2370                 for _, path := range root.TestImports {
2371                         walkTest(root, path)
2372                 }
2373                 for _, path := range root.XTestImports {
2374                         walkTest(root, path)
2375                 }
2376         }
2377         return all
2378 }
2379
2380 // LoadImportWithFlags loads the package with the given import path and
2381 // sets tool flags on that package. This function is useful loading implicit
2382 // dependencies (like sync/atomic for coverage).
2383 // TODO(jayconrod): delete this function and set flags automatically
2384 // in LoadImport instead.
2385 func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
2386         p := LoadImport(context.TODO(), PackageOpts{}, path, srcDir, parent, stk, importPos, mode)
2387         setToolFlags(p)
2388         return p
2389 }
2390
2391 // PackageOpts control the behavior of PackagesAndErrors and other package
2392 // loading functions.
2393 type PackageOpts struct {
2394         // IgnoreImports controls whether we ignore explicit and implicit imports
2395         // when loading packages.  Implicit imports are added when supporting Cgo
2396         // or SWIG and when linking main packages.
2397         IgnoreImports bool
2398
2399         // ModResolveTests indicates whether calls to the module loader should also
2400         // resolve test dependencies of the requested packages.
2401         //
2402         // If ModResolveTests is true, then the module loader needs to resolve test
2403         // dependencies at the same time as packages; otherwise, the test dependencies
2404         // of those packages could be missing, and resolving those missing dependencies
2405         // could change the selected versions of modules that provide other packages.
2406         ModResolveTests bool
2407
2408         // MainOnly is true if the caller only wants to load main packages.
2409         // For a literal argument matching a non-main package, a stub may be returned
2410         // with an error. For a non-literal argument (with "..."), non-main packages
2411         // are not be matched, and their dependencies may not be loaded. A warning
2412         // may be printed for non-literal arguments that match no main packages.
2413         MainOnly bool
2414 }
2415
2416 // PackagesAndErrors returns the packages named by the command line arguments
2417 // 'patterns'. If a named package cannot be loaded, PackagesAndErrors returns
2418 // a *Package with the Error field describing the failure. If errors are found
2419 // loading imported packages, the DepsErrors field is set. The Incomplete field
2420 // may be set as well.
2421 //
2422 // To obtain a flat list of packages, use PackageList.
2423 // To report errors loading packages, use ReportPackageErrors.
2424 func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string) []*Package {
2425         ctx, span := trace.StartSpan(ctx, "load.PackagesAndErrors")
2426         defer span.Done()
2427
2428         for _, p := range patterns {
2429                 // Listing is only supported with all patterns referring to either:
2430                 // - Files that are part of the same directory.
2431                 // - Explicit package paths or patterns.
2432                 if strings.HasSuffix(p, ".go") {
2433                         // We need to test whether the path is an actual Go file and not a
2434                         // package path or pattern ending in '.go' (see golang.org/issue/34653).
2435                         if fi, err := fsys.Stat(p); err == nil && !fi.IsDir() {
2436                                 return []*Package{GoFilesPackage(ctx, opts, patterns)}
2437                         }
2438                 }
2439         }
2440
2441         var matches []*search.Match
2442         if modload.Init(); cfg.ModulesEnabled {
2443                 modOpts := modload.PackageOpts{
2444                         ResolveMissingImports: true,
2445                         LoadTests:             opts.ModResolveTests,
2446                         SilencePackageErrors:  true,
2447                 }
2448                 matches, _ = modload.LoadPackages(ctx, modOpts, patterns...)
2449         } else {
2450                 matches = search.ImportPaths(patterns)
2451         }
2452
2453         var (
2454                 pkgs    []*Package
2455                 stk     ImportStack
2456                 seenPkg = make(map[*Package]bool)
2457         )
2458
2459         pre := newPreload()
2460         defer pre.flush()
2461         pre.preloadMatches(ctx, opts, matches)
2462
2463         for _, m := range matches {
2464                 for _, pkg := range m.Pkgs {
2465                         if pkg == "" {
2466                                 panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
2467                         }
2468                         p := loadImport(ctx, opts, pre, pkg, base.Cwd(), nil, &stk, nil, 0)
2469                         p.Match = append(p.Match, m.Pattern())
2470                         p.Internal.CmdlinePkg = true
2471                         if m.IsLiteral() {
2472                                 // Note: do not set = m.IsLiteral unconditionally
2473                                 // because maybe we'll see p matching both
2474                                 // a literal and also a non-literal pattern.
2475                                 p.Internal.CmdlinePkgLiteral = true
2476                         }
2477                         if seenPkg[p] {
2478                                 continue
2479                         }
2480                         seenPkg[p] = true
2481                         pkgs = append(pkgs, p)
2482                 }
2483
2484                 if len(m.Errs) > 0 {
2485                         // In addition to any packages that were actually resolved from the
2486                         // pattern, there was some error in resolving the pattern itself.
2487                         // Report it as a synthetic package.
2488                         p := new(Package)
2489                         p.ImportPath = m.Pattern()
2490                         // Pass an empty ImportStack and nil importPos: the error arose from a pattern, not an import.
2491                         var stk ImportStack
2492                         var importPos []token.Position
2493                         p.setLoadPackageDataError(m.Errs[0], m.Pattern(), &stk, importPos)
2494                         p.Incomplete = true
2495                         p.Match = append(p.Match, m.Pattern())
2496                         p.Internal.CmdlinePkg = true
2497                         if m.IsLiteral() {
2498                                 p.Internal.CmdlinePkgLiteral = true
2499                         }
2500                         pkgs = append(pkgs, p)
2501                 }
2502         }
2503
2504         if opts.MainOnly {
2505                 pkgs = mainPackagesOnly(pkgs, matches)
2506         }
2507
2508         // Now that CmdlinePkg is set correctly,
2509         // compute the effective flags for all loaded packages
2510         // (not just the ones matching the patterns but also
2511         // their dependencies).
2512         setToolFlags(pkgs...)
2513
2514         return pkgs
2515 }
2516
2517 // CheckPackageErrors prints errors encountered loading pkgs and their
2518 // dependencies, then exits with a non-zero status if any errors were found.
2519 func CheckPackageErrors(pkgs []*Package) {
2520         printed := map[*PackageError]bool{}
2521         for _, pkg := range pkgs {
2522                 if pkg.Error != nil {
2523                         base.Errorf("%v", pkg.Error)
2524                         printed[pkg.Error] = true
2525                 }
2526                 for _, err := range pkg.DepsErrors {
2527                         // Since these are errors in dependencies,
2528                         // the same error might show up multiple times,
2529                         // once in each package that depends on it.
2530                         // Only print each once.
2531                         if !printed[err] {
2532                                 printed[err] = true
2533                                 base.Errorf("%v", err)
2534                         }
2535                 }
2536         }
2537         base.ExitIfErrors()
2538
2539         // Check for duplicate loads of the same package.
2540         // That should be impossible, but if it does happen then
2541         // we end up trying to build the same package twice,
2542         // usually in parallel overwriting the same files,
2543         // which doesn't work very well.
2544         seen := map[string]bool{}
2545         reported := map[string]bool{}
2546         for _, pkg := range PackageList(pkgs) {
2547                 if seen[pkg.ImportPath] && !reported[pkg.ImportPath] {
2548                         reported[pkg.ImportPath] = true
2549                         base.Errorf("internal error: duplicate loads of %s", pkg.ImportPath)
2550                 }
2551                 seen[pkg.ImportPath] = true
2552         }
2553         base.ExitIfErrors()
2554 }
2555
2556 // mainPackagesOnly filters out non-main packages matched only by arguments
2557 // containing "..." and returns the remaining main packages.
2558 //
2559 // Packages with missing, invalid, or ambiguous names may be treated as
2560 // possibly-main packages.
2561 //
2562 // mainPackagesOnly sets a non-main package's Error field and returns it if it
2563 // is named by a literal argument.
2564 //
2565 // mainPackagesOnly prints warnings for non-literal arguments that only match
2566 // non-main packages.
2567 func mainPackagesOnly(pkgs []*Package, matches []*search.Match) []*Package {
2568         treatAsMain := map[string]bool{}
2569         for _, m := range matches {
2570                 if m.IsLiteral() {
2571                         for _, path := range m.Pkgs {
2572                                 treatAsMain[path] = true
2573                         }
2574                 }
2575         }
2576
2577         var mains []*Package
2578         for _, pkg := range pkgs {
2579                 if pkg.Name == "main" {
2580                         treatAsMain[pkg.ImportPath] = true
2581                         mains = append(mains, pkg)
2582                         continue
2583                 }
2584
2585                 if len(pkg.InvalidGoFiles) > 0 { // TODO(#45999): && pkg.Name == "", but currently go/build sets pkg.Name arbitrarily if it is ambiguous.
2586                         // The package has (or may have) conflicting names, and we can't easily
2587                         // tell whether one of them is "main". So assume that it could be, and
2588                         // report an error for the package.
2589                         treatAsMain[pkg.ImportPath] = true
2590                 }
2591                 if treatAsMain[pkg.ImportPath] {
2592                         if pkg.Error == nil {
2593                                 pkg.Error = &PackageError{Err: &mainPackageError{importPath: pkg.ImportPath}}
2594                         }
2595                         mains = append(mains, pkg)
2596                 }
2597         }
2598
2599         for _, m := range matches {
2600                 if m.IsLiteral() || len(m.Pkgs) == 0 {
2601                         continue
2602                 }
2603                 foundMain := false
2604                 for _, path := range m.Pkgs {
2605                         if treatAsMain[path] {
2606                                 foundMain = true
2607                                 break
2608                         }
2609                 }
2610                 if !foundMain {
2611                         fmt.Fprintf(os.Stderr, "go: warning: %q matched only non-main packages\n", m.Pattern())
2612                 }
2613         }
2614
2615         return mains
2616 }
2617
2618 type mainPackageError struct {
2619         importPath string
2620 }
2621
2622 func (e *mainPackageError) Error() string {
2623         return fmt.Sprintf("package %s is not a main package", e.importPath)
2624 }
2625
2626 func (e *mainPackageError) ImportPath() string {
2627         return e.importPath
2628 }
2629
2630 func setToolFlags(pkgs ...*Package) {
2631         for _, p := range PackageList(pkgs) {
2632                 appendFlags(p, &p.Internal.Asmflags, &BuildAsmflags)
2633                 appendFlags(p, &p.Internal.Gcflags, &BuildGcflags)
2634                 appendFlags(p, &p.Internal.Ldflags, &BuildLdflags)
2635                 appendFlags(p, &p.Internal.Gccgoflags, &BuildGccgoflags)
2636         }
2637 }
2638
2639 func appendFlags(p *Package, flags *[]string, packageFlag *PerPackageFlag) {
2640         if !packageFlag.seenPackages[p] {
2641                 if packageFlag.seenPackages == nil {
2642                         packageFlag.seenPackages = make(map[*Package]bool)
2643                 }
2644                 packageFlag.seenPackages[p] = true
2645                 *flags = append(*flags, packageFlag.For(p)...)
2646         }
2647 }
2648
2649 // GoFilesPackage creates a package for building a collection of Go files
2650 // (typically named on the command line). The target is named p.a for
2651 // package p or named after the first Go file for package main.
2652 func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Package {
2653         modload.Init()
2654
2655         for _, f := range gofiles {
2656                 if !strings.HasSuffix(f, ".go") {
2657                         pkg := new(Package)
2658                         pkg.Internal.Local = true
2659                         pkg.Internal.CmdlineFiles = true
2660                         pkg.Name = f
2661                         pkg.Error = &PackageError{
2662                                 Err: fmt.Errorf("named files must be .go files: %s", pkg.Name),
2663                         }
2664                         return pkg
2665                 }
2666         }
2667
2668         var stk ImportStack
2669         ctxt := cfg.BuildContext
2670         ctxt.UseAllFiles = true
2671
2672         // Synthesize fake "directory" that only shows the named files,
2673         // to make it look like this is a standard package or
2674         // command directory. So that local imports resolve
2675         // consistently, the files must all be in the same directory.
2676         var dirent []fs.FileInfo
2677         var dir string
2678         for _, file := range gofiles {
2679                 fi, err := fsys.Stat(file)
2680                 if err != nil {
2681                         base.Fatalf("%s", err)
2682                 }
2683                 if fi.IsDir() {
2684                         base.Fatalf("%s is a directory, should be a Go file", file)
2685                 }
2686                 dir1, _ := filepath.Split(file)
2687                 if dir1 == "" {
2688                         dir1 = "./"
2689                 }
2690                 if dir == "" {
2691                         dir = dir1
2692                 } else if dir != dir1 {
2693                         base.Fatalf("named files must all be in one directory; have %s and %s", dir, dir1)
2694                 }
2695                 dirent = append(dirent, fi)
2696         }
2697         ctxt.ReadDir = func(string) ([]fs.FileInfo, error) { return dirent, nil }
2698
2699         if cfg.ModulesEnabled {
2700                 modload.ImportFromFiles(ctx, gofiles)
2701         }
2702
2703         var err error
2704         if dir == "" {
2705                 dir = base.Cwd()
2706         }
2707         dir, err = filepath.Abs(dir)
2708         if err != nil {
2709                 base.Fatalf("%s", err)
2710         }
2711
2712         bp, err := ctxt.ImportDir(dir, 0)
2713         pkg := new(Package)
2714         pkg.Internal.Local = true
2715         pkg.Internal.CmdlineFiles = true
2716         pkg.load(ctx, opts, "command-line-arguments", &stk, nil, bp, err)
2717         pkg.Internal.LocalPrefix = dirToImportPath(dir)
2718         pkg.ImportPath = "command-line-arguments"
2719         pkg.Target = ""
2720         pkg.Match = gofiles
2721
2722         if pkg.Name == "main" {
2723                 exe := pkg.DefaultExecName() + cfg.ExeSuffix
2724
2725                 if cfg.GOBIN != "" {
2726                         pkg.Target = filepath.Join(cfg.GOBIN, exe)
2727                 } else if cfg.ModulesEnabled {
2728                         pkg.Target = filepath.Join(modload.BinDir(), exe)
2729                 }
2730         }
2731
2732         if opts.MainOnly && pkg.Name != "main" && pkg.Error == nil {
2733                 pkg.Error = &PackageError{Err: &mainPackageError{importPath: pkg.ImportPath}}
2734         }
2735         setToolFlags(pkg)
2736
2737         return pkg
2738 }
2739
2740 // PackagesAndErrorsOutsideModule is like PackagesAndErrors but runs in
2741 // module-aware mode and ignores the go.mod file in the current directory or any
2742 // parent directory, if there is one. This is used in the implementation of 'go
2743 // install pkg@version' and other commands that support similar forms.
2744 //
2745 // modload.ForceUseModules must be true, and modload.RootMode must be NoRoot
2746 // before calling this function.
2747 //
2748 // PackagesAndErrorsOutsideModule imposes several constraints to avoid
2749 // ambiguity. All arguments must have the same version suffix (not just a suffix
2750 // that resolves to the same version). They must refer to packages in the same
2751 // module, which must not be std or cmd. That module is not considered the main
2752 // module, but its go.mod file (if it has one) must not contain directives that
2753 // would cause it to be interpreted differently if it were the main module
2754 // (replace, exclude).
2755 func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args []string) ([]*Package, error) {
2756         if !modload.ForceUseModules {
2757                 panic("modload.ForceUseModules must be true")
2758         }
2759         if modload.RootMode != modload.NoRoot {
2760                 panic("modload.RootMode must be NoRoot")
2761         }
2762
2763         // Check that the arguments satisfy syntactic constraints.
2764         var version string
2765         for _, arg := range args {
2766                 if i := strings.Index(arg, "@"); i >= 0 {
2767                         version = arg[i+1:]
2768                         if version == "" {
2769                                 return nil, fmt.Errorf("%s: version must not be empty", arg)
2770                         }
2771                         break
2772                 }
2773         }
2774         patterns := make([]string, len(args))
2775         for i, arg := range args {
2776                 if !strings.HasSuffix(arg, "@"+version) {
2777                         return nil, fmt.Errorf("%s: all arguments must have the same version (@%s)", arg, version)
2778                 }
2779                 p := arg[:len(arg)-len(version)-1]
2780                 switch {
2781                 case build.IsLocalImport(p):
2782                         return nil, fmt.Errorf("%s: argument must be a package path, not a relative path", arg)
2783                 case filepath.IsAbs(p):
2784                         return nil, fmt.Errorf("%s: argument must be a package path, not an absolute path", arg)
2785                 case search.IsMetaPackage(p):
2786                         return nil, fmt.Errorf("%s: argument must be a package path, not a meta-package", arg)
2787                 case path.Clean(p) != p:
2788                         return nil, fmt.Errorf("%s: argument must be a clean package path", arg)
2789                 case !strings.Contains(p, "...") && search.IsStandardImportPath(p) && goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, p):
2790                         return nil, fmt.Errorf("%s: argument must not be a package in the standard library", arg)
2791                 default:
2792                         patterns[i] = p
2793                 }
2794         }
2795
2796         // Query the module providing the first argument, load its go.mod file, and
2797         // check that it doesn't contain directives that would cause it to be
2798         // interpreted differently if it were the main module.
2799         //
2800         // If multiple modules match the first argument, accept the longest match
2801         // (first result). It's possible this module won't provide packages named by
2802         // later arguments, and other modules would. Let's not try to be too
2803         // magical though.
2804         allowed := modload.CheckAllowed
2805         if modload.IsRevisionQuery(version) {
2806                 // Don't check for retractions if a specific revision is requested.
2807                 allowed = nil
2808         }
2809         noneSelected := func(path string) (version string) { return "none" }
2810         qrs, err := modload.QueryPackages(ctx, patterns[0], version, noneSelected, allowed)
2811         if err != nil {
2812                 return nil, fmt.Errorf("%s: %w", args[0], err)
2813         }
2814         rootMod := qrs[0].Mod
2815         data, err := modfetch.GoMod(rootMod.Path, rootMod.Version)
2816         if err != nil {
2817                 return nil, fmt.Errorf("%s: %w", args[0], err)
2818         }
2819         f, err := modfile.Parse("go.mod", data, nil)
2820         if err != nil {
2821                 return nil, fmt.Errorf("%s (in %s): %w", args[0], rootMod, err)
2822         }
2823         directiveFmt := "%s (in %s):\n" +
2824                 "\tThe go.mod file for the module providing named packages contains one or\n" +
2825                 "\tmore %s directives. It must not contain directives that would cause\n" +
2826                 "\tit to be interpreted differently than if it were the main module."
2827         if len(f.Replace) > 0 {
2828                 return nil, fmt.Errorf(directiveFmt, args[0], rootMod, "replace")
2829         }
2830         if len(f.Exclude) > 0 {
2831                 return nil, fmt.Errorf(directiveFmt, args[0], rootMod, "exclude")
2832         }
2833
2834         // Since we are in NoRoot mode, the build list initially contains only
2835         // the dummy command-line-arguments module. Add a requirement on the
2836         // module that provides the packages named on the command line.
2837         if _, err := modload.EditBuildList(ctx, nil, []module.Version{rootMod}); err != nil {
2838                 return nil, fmt.Errorf("%s: %w", args[0], err)
2839         }
2840
2841         // Load packages for all arguments.
2842         pkgs := PackagesAndErrors(ctx, opts, patterns)
2843
2844         // Check that named packages are all provided by the same module.
2845         for _, pkg := range pkgs {
2846                 var pkgErr error
2847                 if pkg.Module == nil {
2848                         // Packages in std, cmd, and their vendored dependencies
2849                         // don't have this field set.
2850                         pkgErr = fmt.Errorf("package %s not provided by module %s", pkg.ImportPath, rootMod)
2851                 } else if pkg.Module.Path != rootMod.Path || pkg.Module.Version != rootMod.Version {
2852                         pkgErr = fmt.Errorf("package %s provided by module %s@%s\n\tAll packages must be provided by the same module (%s).", pkg.ImportPath, pkg.Module.Path, pkg.Module.Version, rootMod)
2853                 }
2854                 if pkgErr != nil && pkg.Error == nil {
2855                         pkg.Error = &PackageError{Err: pkgErr}
2856                 }
2857         }
2858
2859         matchers := make([]func(string) bool, len(patterns))
2860         for i, p := range patterns {
2861                 if strings.Contains(p, "...") {
2862                         matchers[i] = search.MatchPattern(p)
2863                 }
2864         }
2865         return pkgs, nil
2866 }