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.
5 // Package load loads packages.
30 "cmd/go/internal/base"
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"
38 "cmd/go/internal/search"
40 "cmd/go/internal/trace"
43 "golang.org/x/mod/modfile"
44 "golang.org/x/mod/module"
47 // A Package describes a single package found in a directory.
49 PackagePublic // visible in 'go list'
50 Internal PackageInternal // for use inside go command only
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?
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?
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
103 EmbedPatterns []string `json:",omitempty"` // //go:embed patterns
104 EmbedFiles []string `json:",omitempty"` // files matched by EmbedPatterns
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
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
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
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
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)
142 func (p *Package) AllFiles() []string {
143 files := str.StringList(
146 // no p.CompiledGoFiles, because they are from GoFiles or generated by us
148 // no p.InvalidGoFiles, because they are from GoFiles
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, "/") {
171 have = make(map[string]bool)
172 for _, file := range files {
180 files = append(files, file)
185 // Desc returns the package "description", for use in b.showOutput.
186 func (p *Package) Desc() string {
188 return p.ImportPath + " [" + p.ForTest + ".test]"
193 type PackageInternal struct {
194 // Unexported fields are not part of the public API.
196 Imports []*Package // this package's direct imports
197 CompiledImports []string // additional Imports necessary when using CompiledGoFiles (all from standard library)
198 RawImports []string // this package's original imports as they appear in the text of the program
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
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
221 // A NoGoError indicates that no Go files for the package were applicable to the
222 // build for that package.
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 {
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
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
241 return "no Go files in " + e.Package.Dir
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.
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.
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 != "" {
270 err = &NoGoError{Package: p}
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.
278 if scanErr, ok := err.(scanner.ErrorList); ok && len(scanErr) > 0 {
279 isScanErr = true // For stack push/pop below.
281 scanPos := scanErr[0].Pos
282 scanPos.Filename = base.ShortPath(scanPos.Filename)
283 pos = scanPos.String()
284 err = errors.New(scanErr[0].Msg)
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).
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) {
307 p.Error = &PackageError{
308 ImportStack: stk.Copy(),
313 if path != stk.Top() {
314 p.Error.setPos(importPos)
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")
332 seen := make(map[string]bool)
334 for _, path := range imports {
335 path = ResolveImportPath(p, path)
338 all = append(all, path)
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
351 func (p *Package) copyBuild(opts PackageOpts, pp *build.Package) {
352 p.Internal.Build = pp
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))
362 p.ImportPath = pp.ImportPath
363 p.ImportComment = pp.ImportComment
367 p.ConflictDir = pp.ConflictDir
368 p.BinaryOnly = pp.BinaryOnly
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
379 p.CXXFiles = pp.CXXFiles
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 {
403 p.Internal.RawImports = nil
407 p.EmbedPatterns = pp.EmbedPatterns
408 p.TestEmbedPatterns = pp.TestEmbedPatterns
409 p.XTestEmbedPatterns = pp.XTestEmbedPatterns
410 p.Internal.OrigImportPath = pp.ImportPath
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
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()
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 {
444 optpos = "\n\t" + p.Pos
446 return "package " + strings.Join(p.ImportStack, "\n\timports ") + optpos + ": " + p.Err.Error()
449 func (p *PackageError) Unwrap() error { return p.Err }
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) {
458 }{p.ImportStack, p.Pos, p.Err.Error()}
459 return json.Marshal(perr)
462 func (p *PackageError) setPos(posList []token.Position) {
463 if len(posList) == 0 {
467 pos.Filename = base.ShortPath(pos.Filename)
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.
476 // The string returned by ImportPath must appear in the string returned by
477 // Error. Errors that wrap ImportPathError (such as PackageError) may omit
479 type ImportPathError interface {
485 _ ImportPathError = (*importError)(nil)
486 _ ImportPathError = (*mainPackageError)(nil)
487 _ ImportPathError = (*modload.ImportMissingError)(nil)
488 _ ImportPathError = (*modload.ImportMissingSumError)(nil)
489 _ ImportPathError = (*modload.DirectImportFromImplicitDependencyError)(nil)
492 type importError struct {
494 err error // created with fmt.Errorf
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))
505 func (e *importError) Error() string {
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)
515 func (e *importError) ImportPath() string {
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
524 func (s *ImportStack) Push(p string) {
528 func (s *ImportStack) Pop() {
529 *s = (*s)[0 : len(*s)-1]
532 func (s *ImportStack) Copy() []string {
533 return append([]string{}, *s...)
536 func (s *ImportStack) Top() string {
540 return (*s)[len(*s)-1]
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 {
548 if len(s) != len(t) {
549 return len(s) < len(t)
551 // If they are the same length, settle ties using string ordering.
557 return false // they are equal
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{}
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)
572 resolvedImportCache.Clear()
573 packageDataCache.Clear()
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)
588 resolvedImportCache.DeleteIf(func(key interface{}) bool {
589 return shouldDelete[key.(importSpec).path]
591 packageDataCache.DeleteIf(func(key interface{}) bool {
592 return shouldDelete[key.(string)]
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]
603 delete(packageCache, arg)
604 resolvedImportCache.DeleteIf(func(key interface{}) bool {
605 return key.(importSpec).path == p.ImportPath
607 packageDataCache.Delete(p.ImportPath)
609 return LoadImport(context.TODO(), PackageOpts{}, arg, base.Cwd(), nil, stk, nil, 0)
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
619 func dirToImportPath(dir string) string {
620 return pathpkg.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
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) {
632 // Mode flags for loadImport and download (in get.go).
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
645 // ResolveModule is for download (part of "go get") and indicates
646 // that the module adjustment should be done, but not vendor adjustment.
649 // GetTestDeps is for download (part of "go get") and indicates
650 // that test dependencies should be fetched too.
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)
665 func loadImport(ctx context.Context, opts PackageOpts, pre *preload, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
667 panic("LoadImport called with empty package path")
670 var parentPath, parentRoot string
673 parentPath = parent.ImportPath
674 parentRoot = parent.Root
675 parentIsStd = parent.Standard
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)
683 PackagePublic: PackagePublic{
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
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.
699 p.setLoadPackageDataError(err, path, stk, nil)
703 importPath := bp.ImportPath
704 p := packageCache[importPath]
707 p = reusePackage(p, stk)
711 p.Internal.Local = build.IsLocalImport(path)
712 p.ImportPath = importPath
713 packageCache[importPath] = p
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)
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)),
726 p.Error.setPos(importPos)
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)
735 if mode&ResolveImport != 0 {
736 if perr := disallowVendor(srcDir, path, parentPath, p, stk); perr != p {
737 perr.Error.setPos(importPos)
742 if p.Name == "main" && parent != nil && parent.Dir != p.Dir {
744 perr.Error = &PackageError{
745 ImportStack: stk.Copy(),
746 Err: ImportErrorf(path, "import %q is a program, not an importable package", path),
748 perr.Error.setPos(importPos)
752 if p.Internal.Local && parent != nil && !parent.Internal.Local {
756 err = ImportErrorf(path, "%s: cannot import current directory", path)
758 err = ImportErrorf(path, "local import %q in non-local package", path)
760 perr.Error = &PackageError{
761 ImportStack: stk.Copy(),
764 perr.Error.setPos(importPos)
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
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 "@").
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) {
782 panic("loadPackageData called with empty package path")
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)
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")
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.
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{
810 parentPath: parentPath,
811 parentDir: parentDir,
812 parentRoot: parentRoot,
813 parentIsStd: parentIsStd,
816 r := resolvedImportCache.Do(importKey, func() interface{} {
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)
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.
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{} {
847 var buildMode build.ImportMode
848 if !cfg.ModulesEnabled {
849 buildMode = build.ImportComment
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
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
874 // TODO(#41410: Figure out how this actually ought to work and fix
880 } else if r.err != nil {
881 data.p = new(build.Package)
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)
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
892 data.p, data.err = cfg.BuildContext.Import(r.path, parentDir, buildMode)
894 data.p.ImportPath = r.path
896 // Set data.p.BinDir in cases where go/build.Context.Import
897 // may give us a path we don't want.
900 data.p.BinDir = cfg.GOBIN
901 } else if cfg.ModulesEnabled {
902 data.p.BinDir = modload.BinDir()
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)
914 return data.p, loaded, data.err
917 // importSpec describes an import declaration in source code. It is used as a
918 // cache key for resolvedImportCache.
919 type importSpec struct {
921 parentPath, parentDir, parentRoot string
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 {
934 // packageData holds information loaded from a package. It is the value type
935 // in packageDataCache.
936 type packageData struct {
941 // resolvedImportCache maps import strings (importSpec) to canonical package names
943 var resolvedImportCache par.Cache
945 // packageDataCache maps canonical package names (string) to package metadata
947 var packageDataCache par.Cache
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.
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
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)
963 // preload holds state for managing concurrent preloading of package data.
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 {
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 {
981 cancel: make(chan struct{}),
982 sema: make(chan struct{}, preloadWorkerCount),
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 {
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)
1001 if bp != nil && loaded && err == nil && !opts.IgnoreImports {
1002 pre.preloadImports(ctx, opts, bp.Imports, bp)
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" {
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)
1026 if bp != nil && loaded && err == nil && !opts.IgnoreImports {
1027 pre.preloadImports(ctx, opts, bp.Imports, bp)
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 {
1045 for i := 0; i < preloadWorkerCount; i++ {
1046 pre.sema <- struct{}{}
1050 func cleanImport(path string) string {
1052 path = pathpkg.Clean(path)
1053 if strings.HasPrefix(orig, "./") && path != ".." && !strings.HasPrefix(path, "../") {
1059 var isDirCache par.Cache
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()
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
1077 parentPath = parent.ImportPath
1078 parentDir = parent.Dir
1079 parentRoot = parent.Root
1080 parentIsStd = parent.Standard
1082 return resolveImportPath(path, parentPath, parentDir, parentRoot, parentIsStd)
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 {
1092 found = vendoredImportPath(path, parentPath, parentDir, parentRoot)
1096 return moduleImportPath(path, parentPath, parentDir, parentRoot)
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)
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"+
1116 " expand root: %s\n"+
1117 " expand dir: %s\n"+
1120 filepath.Join(origRoot, "src"),
1121 filepath.Clean(origDir),
1124 string(filepath.Separator))
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 == "" {
1139 dir, root := dirAndRoot(parentPath, parentDir, parentRoot)
1141 vpath := "vendor/" + path
1142 for i := len(dir); i >= len(root); i-- {
1143 if i < len(dir) && dir[i] != filepath.Separator {
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")) {
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:]
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.
1177 return importPath[:len(importPath)-chopped] + "/" + vpath
1184 modulePrefix = []byte("\nmodule ")
1185 goModPathCache par.Cache
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"))
1196 if bytes.HasPrefix(data, modulePrefix[1:]) {
1199 i = bytes.Index(data, modulePrefix)
1207 // Cut line at \n, drop trailing \r if present.
1208 if j := bytes.IndexByte(line, '\n'); j >= 0 {
1211 if line[len(line)-1] == '\r' {
1212 line = line[:len(line)-1]
1214 line = line[len("module "):]
1216 // If quoted, unquote.
1217 path = strings.TrimSpace(string(line))
1218 if path != "" && path[0] == '"' {
1219 s, err := strconv.Unquote(path)
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) {
1233 for i = len(path) - 1; i >= 0; i-- {
1235 if isVersionElement(path[i+1 : j]) {
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 {
1250 for i := 1; i < len(s); i++ {
1251 if s[i] < '0' || '9' < s[i] {
1258 // moduleImportPath translates import paths found in go modules
1259 // back down to paths that can be resolved in ordinary builds.
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 == "" {
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 {
1279 dir, root := dirAndRoot(parentPath, parentDir, parentRoot)
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 {
1286 if goModPath(dir[:i]) != "" {
1290 // This code is not in a tree with a go.mod,
1291 // so apply no changes to the path.
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",
1299 // If x/y/v2/z exists, use it unmodified.
1300 if bp, _ := cfg.BuildContext.Import(path, "", build.IgnoreVendor); bp.Dir != "" {
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.
1311 i, j := findVersionElement(path[:limit])
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:]
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.
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") {
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 {
1358 p.Error = &PackageError{
1359 ImportStack: stk.Copy(),
1360 Err: errors.New("import cycle not allowed"),
1361 IsImportCycle: true,
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
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()
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.
1389 // There was an error loading the package; stop here.
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" {
1402 // We can't check standard packages with gccgo.
1403 if cfg.BuildContext.Compiler == "gccgo" && p.Standard {
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/") {
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 == "" {
1421 // Check for "internal" element: three cases depending on begin of string and/or end of string.
1422 i, ok := findInternal(p.ImportPath)
1427 // Internal is present.
1428 // Map import path back to directory corresponding to parent of internal.
1430 i-- // rewind over slash in ".../internal"
1433 if p.Module == nil {
1434 parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
1436 if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
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)) {
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)
1457 parentOfInternal := p.ImportPath[:i]
1458 if str.HasPathPrefix(importerPath, parentOfInternal) {
1463 // Internal is present, and srcDir is outside parent's tree. Not allowed.
1465 perr.Error = &PackageError{
1466 alwaysPrintStack: true,
1467 ImportStack: stk.Copy(),
1468 Err: ImportErrorf(p.ImportPath, "use of internal package "+p.ImportPath+" not allowed"),
1470 perr.Incomplete = true
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
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/"):
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 == "" {
1504 if perr := disallowVendorVisibility(srcDir, p, importerPath, stk); perr != p {
1508 // Paths like x/vendor/y must be imported as y, never as x/vendor/y.
1509 if i, ok := FindVendor(path); ok {
1511 perr.Error = &PackageError{
1512 ImportStack: stk.Copy(),
1513 Err: ImportErrorf(path, "%s must be imported as %s", path, path[i+len("vendor/"):]),
1515 perr.Incomplete = true
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 == "" {
1536 // Check for "vendor" element.
1537 i, ok := FindVendor(p.ImportPath)
1542 // Vendor is present.
1543 // Map import path back to directory corresponding to parent of vendor.
1545 i-- // rewind over slash in ".../vendor"
1547 truncateTo := i + len(p.Dir) - len(p.ImportPath)
1548 if truncateTo < 0 || len(p.Dir) < truncateTo {
1551 parent := p.Dir[:truncateTo]
1552 if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
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)) {
1563 // Vendor is present, and srcDir is outside parent's tree. Not allowed.
1565 perr.Error = &PackageError{
1566 ImportStack: stk.Copy(),
1567 Err: errors.New("use of vendored package not allowed"),
1569 perr.Incomplete = true
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".
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.
1586 case strings.Contains(path, "/vendor/"):
1587 return strings.LastIndex(path, "/vendor/") + 1, true
1588 case strings.HasPrefix(path, "vendor/"):
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
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/") {
1607 if p.Goroot && strings.HasPrefix(p.ImportPath, "cmd/") && p.Name == "main" {
1608 switch p.ImportPath {
1609 case "cmd/go", "cmd/gofmt":
1617 var cgoExclude = map[string]bool{
1618 "runtime/cgo": true,
1621 var cgoSyscallExclude = map[string]bool{
1622 "runtime/cgo": true,
1623 "runtime/race": true,
1624 "runtime/msan": true,
1627 var foldPath = make(map[string]string)
1629 // exeFromImportPath returns an executable name
1630 // for a package using the import path.
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))
1649 // exeFromFiles returns an executable name for a package
1650 // using the first element in GoFiles or CgoFiles collections without the prefix.
1652 // Returns empty string in case of empty collection.
1653 func (p *Package) exeFromFiles() string {
1655 if len(p.GoFiles) > 0 {
1657 } else if len(p.CgoFiles) > 0 {
1662 _, elem := filepath.Split(src)
1663 return elem[:len(elem)-len(".go")]
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()
1671 return p.exeFromImportPath()
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)
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)
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) {
1691 p.Error = &PackageError{
1692 ImportStack: stk.Copy(),
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)
1710 p.setLoadPackageDataError(err, path, stk, importPos)
1713 useBindir := p.Name == "main"
1715 switch cfg.BuildBuildmode {
1716 case "c-archive", "c-shared", "plugin":
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)
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.
1737 if p.Internal.Build.BinDir == "" && cfg.ModulesEnabled {
1738 p.Internal.Build.BinDir = modload.BinDir()
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.
1746 p.Internal.GobinSubdir = true
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)
1755 p.Target = filepath.Join(cfg.GOROOTpkg, "tool", full)
1758 if p.Target != "" && cfg.BuildContext.GOOS == "windows" {
1761 } else if p.Internal.Local {
1762 // Local import turned into absolute path.
1763 // No permanent install target.
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)
1777 libname := strings.TrimSpace(string(shlib))
1778 if cfg.BuildContext.Compiler == "gccgo" {
1779 p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, "shlibs", libname)
1781 p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, libname)
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 {
1796 importPaths = append(importPaths, path)
1798 p.Internal.CompiledImports = append(p.Internal.CompiledImports, path)
1802 if !opts.IgnoreImports {
1803 // Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
1804 // except for certain packages, to avoid circular dependencies.
1806 addImport("unsafe", true)
1808 if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
1809 addImport("runtime/cgo", true)
1811 if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
1812 addImport("syscall", true)
1815 // SWIG adds imports of some standard packages.
1817 addImport("unsafe", true)
1818 if cfg.BuildContext.Compiler != "gccgo" {
1819 addImport("runtime/cgo", true)
1821 addImport("syscall", true)
1822 addImport("sync", true)
1824 // TODO: The .swig and .swigcxx files can use
1825 // %go_import directives to import other packages.
1828 // The linker loads implicit dependencies.
1829 if p.Name == "main" && !p.Internal.ForceLibrary {
1830 for _, dep := range LinkerDeps(p) {
1831 addImport(dep, false)
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))
1845 if !SafeArg(p.ImportPath) {
1846 setError(ImportErrorf(p.ImportPath, "invalid import path %q", p.ImportPath))
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.
1856 pkgPath := p.ImportPath
1857 if p.Internal.CmdlineFiles {
1858 pkgPath = "command-line-arguments"
1860 if cfg.ModulesEnabled {
1861 p.Module = modload.PackageModuleInfo(ctx, pkgPath)
1864 p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns)
1868 embedErr := err.(*EmbedError)
1869 p.Error.setPos(p.Internal.Build.EmbedPatternPos[embedErr.Pattern])
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
1876 inputs := p.AllFiles()
1877 f1, f2 := str.FoldDup(inputs)
1879 setError(fmt.Errorf("case-insensitive file name collision: %q and %q", f1, f2))
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))
1896 if name := pathpkg.Base(p.ImportPath); !SafeArg(name) {
1897 setError(fmt.Errorf("invalid input directory name %q", name))
1901 // Build list of imported packages and full dependency list.
1902 imports := make([]*Package, 0, len(p.Imports))
1903 for i, path := range importPaths {
1907 p1 := LoadImport(ctx, opts, path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport)
1909 path = p1.ImportPath
1910 importPaths[i] = path
1911 if i < len(p.Imports) {
1915 imports = append(imports, p1)
1920 p.Internal.Imports = imports
1923 if cfg.ModulesEnabled && p.Error == nil && p.Name == "main" && len(p.DepsErrors) == 0 {
1924 p.Internal.BuildInfo = modload.PackageBuildInfo(pkgPath, p.Deps)
1927 // unsafe is a fake package.
1928 if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
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 {
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).
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, " ")))
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, " ")))
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, " ")))
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, " ")))
1968 // An EmbedError indicates a problem with a go:embed directive.
1969 type EmbedError struct {
1974 func (e *EmbedError) Error() string {
1975 return fmt.Sprintf("pattern %s: %v", e.Pattern, e.Err)
1978 func (e *EmbedError) Unwrap() error {
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)
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) {
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 {
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")
2020 // Glob to find matches.
2021 match, err := fsys.Glob(pkgdir + string(filepath.Separator) + filepath.FromSlash(pattern))
2023 return nil, nil, err
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.)
2031 for _, file := range match {
2032 rel := filepath.ToSlash(file[len(pkgdir)+1:]) // file, relative to p.Dir
2035 info, err := fsys.Lstat(file)
2037 return nil, nil, err
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)
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:])
2055 if elem := filepath.Base(dir); isBadEmbedName(elem) {
2057 return nil, nil, fmt.Errorf("cannot embed %s %s: invalid name %s", what, rel, elem)
2059 return nil, nil, fmt.Errorf("cannot embed %s %s: in invalid directory %s", what, rel, elem)
2066 return nil, nil, fmt.Errorf("cannot embed irregular file %s", rel)
2068 case info.Mode().IsRegular():
2069 if have[rel] != pid {
2071 list = append(list, rel)
2075 // Gather all files in the named directory, stopping at module boundaries
2076 // and ignoring files that wouldn't be packaged into a module.
2078 err := fsys.Walk(file, func(path string, info os.FileInfo, err error) error {
2082 rel := filepath.ToSlash(path[len(pkgdir)+1:])
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.
2094 if _, err := fsys.Stat(filepath.Join(path, "go.mod")); err == nil {
2095 return filepath.SkipDir
2099 if !info.Mode().IsRegular() {
2103 if have[rel] != pid {
2105 list = append(list, rel)
2110 return nil, nil, err
2113 return nil, nil, fmt.Errorf("cannot embed directory %s: contains no embeddable files", rel)
2119 return nil, nil, fmt.Errorf("no matching files found")
2122 pmap[pattern] = list
2125 for file := range have {
2126 files = append(files, file)
2129 return files, pmap, nil
2132 func validEmbedPattern(pattern string) bool {
2133 return pattern != "." && fs.ValidPath(pattern)
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 {
2144 // Empty string should be impossible but make it bad.
2147 // Version control directories won't be present in module.
2148 case ".bzr", ".hg", ".git", ".svn":
2154 // collectDeps populates p.Deps and p.DepsErrors by iterating over
2155 // p.Internal.Imports.
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)
2162 q = append(q, p.Internal.Imports...)
2163 for i := 0; i < len(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.
2170 if p0 == nil || p1.Error != nil && (p0.Error == nil || len(p0.Error.ImportStack) > len(p1.Error.ImportStack)) {
2172 for _, p2 := range p1.Internal.Imports {
2173 if deps[p2.ImportPath] != p2 {
2180 p.Deps = make([]string, 0, len(deps))
2181 for dep := range deps {
2182 p.Deps = append(p.Deps, dep)
2184 sort.Strings(p.Deps)
2185 for _, dep := range p.Deps {
2188 panic("impossible: missing entry in package cache for " + dep + " imported by " + p.ImportPath)
2190 if p1.Error != nil {
2191 p.DepsErrors = append(p.DepsErrors, p1.Error)
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 {
2210 return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
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"}
2218 // External linking mode forces an import of runtime/cgo.
2219 if externalLinkingForced(p) && cfg.BuildContext.Compiler != "gccgo" {
2220 deps = append(deps, "runtime/cgo")
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")
2226 // Using the race detector forces an import of runtime/race.
2228 deps = append(deps, "runtime/race")
2230 // Using memory sanitizer forces an import of runtime/msan.
2232 deps = append(deps, "runtime/msan")
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 {
2245 // Some targets must use external linking even inside GOROOT.
2246 switch cfg.BuildContext.GOOS {
2248 if cfg.BuildContext.GOARCH != "arm64" {
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
2265 ldflags := BuildLdflags.For(p)
2266 for i := len(ldflags) - 1; i >= 0; i-- {
2268 if a == "-linkmode=external" ||
2269 a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "external" {
2270 linkmodeExternal = true
2272 } else if a == "-linkmode=internal" ||
2273 a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "internal" {
2279 return cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal
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)
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))
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)
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))
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
2317 // usesCgo reports whether the package needs to run cgo
2318 func (p *Package) UsesCgo() bool {
2319 return len(p.CgoFiles) > 0
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{}
2327 var walk func(*Package)
2328 walk = func(p *Package) {
2333 for _, p1 := range p.Internal.Imports {
2336 all = append(all, p)
2338 for _, root := range roots {
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{}
2350 var walk func(*Package)
2351 walk = func(p *Package) {
2356 for _, p1 := range p.Internal.Imports {
2359 all = append(all, p)
2361 walkTest := func(root *Package, path string) {
2363 p1 := LoadImport(ctx, opts, path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
2364 if p1.Error == nil {
2368 for _, root := range roots {
2370 for _, path := range root.TestImports {
2371 walkTest(root, path)
2373 for _, path := range root.XTestImports {
2374 walkTest(root, path)
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)
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.
2399 // ModResolveTests indicates whether calls to the module loader should also
2400 // resolve test dependencies of the requested packages.
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
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.
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.
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")
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)}
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,
2448 matches, _ = modload.LoadPackages(ctx, modOpts, patterns...)
2450 matches = search.ImportPaths(patterns)
2456 seenPkg = make(map[*Package]bool)
2461 pre.preloadMatches(ctx, opts, matches)
2463 for _, m := range matches {
2464 for _, pkg := range m.Pkgs {
2466 panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
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
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
2481 pkgs = append(pkgs, p)
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.
2489 p.ImportPath = m.Pattern()
2490 // Pass an empty ImportStack and nil importPos: the error arose from a pattern, not an import.
2492 var importPos []token.Position
2493 p.setLoadPackageDataError(m.Errs[0], m.Pattern(), &stk, importPos)
2495 p.Match = append(p.Match, m.Pattern())
2496 p.Internal.CmdlinePkg = true
2498 p.Internal.CmdlinePkgLiteral = true
2500 pkgs = append(pkgs, p)
2505 pkgs = mainPackagesOnly(pkgs, matches)
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...)
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
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.
2533 base.Errorf("%v", err)
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)
2551 seen[pkg.ImportPath] = true
2556 // mainPackagesOnly filters out non-main packages matched only by arguments
2557 // containing "..." and returns the remaining main packages.
2559 // Packages with missing, invalid, or ambiguous names may be treated as
2560 // possibly-main packages.
2562 // mainPackagesOnly sets a non-main package's Error field and returns it if it
2563 // is named by a literal argument.
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 {
2571 for _, path := range m.Pkgs {
2572 treatAsMain[path] = true
2577 var mains []*Package
2578 for _, pkg := range pkgs {
2579 if pkg.Name == "main" {
2580 treatAsMain[pkg.ImportPath] = true
2581 mains = append(mains, pkg)
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
2591 if treatAsMain[pkg.ImportPath] {
2592 if pkg.Error == nil {
2593 pkg.Error = &PackageError{Err: &mainPackageError{importPath: pkg.ImportPath}}
2595 mains = append(mains, pkg)
2599 for _, m := range matches {
2600 if m.IsLiteral() || len(m.Pkgs) == 0 {
2604 for _, path := range m.Pkgs {
2605 if treatAsMain[path] {
2611 fmt.Fprintf(os.Stderr, "go: warning: %q matched only non-main packages\n", m.Pattern())
2618 type mainPackageError struct {
2622 func (e *mainPackageError) Error() string {
2623 return fmt.Sprintf("package %s is not a main package", e.importPath)
2626 func (e *mainPackageError) ImportPath() string {
2630 func setToolFlags(pkgs ...*Package) {
2631 for _, p := range PackageList(pkgs) {
2632 p.Internal.Asmflags = BuildAsmflags.For(p)
2633 p.Internal.Gcflags = BuildGcflags.For(p)
2634 p.Internal.Ldflags = BuildLdflags.For(p)
2635 p.Internal.Gccgoflags = BuildGccgoflags.For(p)
2639 // GoFilesPackage creates a package for building a collection of Go files
2640 // (typically named on the command line). The target is named p.a for
2641 // package p or named after the first Go file for package main.
2642 func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Package {
2645 for _, f := range gofiles {
2646 if !strings.HasSuffix(f, ".go") {
2648 pkg.Internal.Local = true
2649 pkg.Internal.CmdlineFiles = true
2651 pkg.Error = &PackageError{
2652 Err: fmt.Errorf("named files must be .go files: %s", pkg.Name),
2659 ctxt := cfg.BuildContext
2660 ctxt.UseAllFiles = true
2662 // Synthesize fake "directory" that only shows the named files,
2663 // to make it look like this is a standard package or
2664 // command directory. So that local imports resolve
2665 // consistently, the files must all be in the same directory.
2666 var dirent []fs.FileInfo
2668 for _, file := range gofiles {
2669 fi, err := fsys.Stat(file)
2671 base.Fatalf("%s", err)
2674 base.Fatalf("%s is a directory, should be a Go file", file)
2676 dir1, _ := filepath.Split(file)
2682 } else if dir != dir1 {
2683 base.Fatalf("named files must all be in one directory; have %s and %s", dir, dir1)
2685 dirent = append(dirent, fi)
2687 ctxt.ReadDir = func(string) ([]fs.FileInfo, error) { return dirent, nil }
2689 if cfg.ModulesEnabled {
2690 modload.ImportFromFiles(ctx, gofiles)
2697 dir, err = filepath.Abs(dir)
2699 base.Fatalf("%s", err)
2702 bp, err := ctxt.ImportDir(dir, 0)
2704 pkg.Internal.Local = true
2705 pkg.Internal.CmdlineFiles = true
2706 pkg.load(ctx, opts, "command-line-arguments", &stk, nil, bp, err)
2707 pkg.Internal.LocalPrefix = dirToImportPath(dir)
2708 pkg.ImportPath = "command-line-arguments"
2712 if pkg.Name == "main" {
2713 exe := pkg.DefaultExecName() + cfg.ExeSuffix
2715 if cfg.GOBIN != "" {
2716 pkg.Target = filepath.Join(cfg.GOBIN, exe)
2717 } else if cfg.ModulesEnabled {
2718 pkg.Target = filepath.Join(modload.BinDir(), exe)
2722 if opts.MainOnly && pkg.Name != "main" && pkg.Error == nil {
2723 pkg.Error = &PackageError{Err: &mainPackageError{importPath: pkg.ImportPath}}
2730 // PackagesAndErrorsOutsideModule is like PackagesAndErrors but runs in
2731 // module-aware mode and ignores the go.mod file in the current directory or any
2732 // parent directory, if there is one. This is used in the implementation of 'go
2733 // install pkg@version' and other commands that support similar forms.
2735 // modload.ForceUseModules must be true, and modload.RootMode must be NoRoot
2736 // before calling this function.
2738 // PackagesAndErrorsOutsideModule imposes several constraints to avoid
2739 // ambiguity. All arguments must have the same version suffix (not just a suffix
2740 // that resolves to the same version). They must refer to packages in the same
2741 // module, which must not be std or cmd. That module is not considered the main
2742 // module, but its go.mod file (if it has one) must not contain directives that
2743 // would cause it to be interpreted differently if it were the main module
2744 // (replace, exclude).
2745 func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args []string) ([]*Package, error) {
2746 if !modload.ForceUseModules {
2747 panic("modload.ForceUseModules must be true")
2749 if modload.RootMode != modload.NoRoot {
2750 panic("modload.RootMode must be NoRoot")
2753 // Check that the arguments satisfy syntactic constraints.
2755 for _, arg := range args {
2756 if i := strings.Index(arg, "@"); i >= 0 {
2759 return nil, fmt.Errorf("%s: version must not be empty", arg)
2764 patterns := make([]string, len(args))
2765 for i, arg := range args {
2766 if !strings.HasSuffix(arg, "@"+version) {
2767 return nil, fmt.Errorf("%s: all arguments must have the same version (@%s)", arg, version)
2769 p := arg[:len(arg)-len(version)-1]
2771 case build.IsLocalImport(p):
2772 return nil, fmt.Errorf("%s: argument must be a package path, not a relative path", arg)
2773 case filepath.IsAbs(p):
2774 return nil, fmt.Errorf("%s: argument must be a package path, not an absolute path", arg)
2775 case search.IsMetaPackage(p):
2776 return nil, fmt.Errorf("%s: argument must be a package path, not a meta-package", arg)
2777 case path.Clean(p) != p:
2778 return nil, fmt.Errorf("%s: argument must be a clean package path", arg)
2779 case !strings.Contains(p, "...") && search.IsStandardImportPath(p) && goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, p):
2780 return nil, fmt.Errorf("%s: argument must not be a package in the standard library", arg)
2786 // Query the module providing the first argument, load its go.mod file, and
2787 // check that it doesn't contain directives that would cause it to be
2788 // interpreted differently if it were the main module.
2790 // If multiple modules match the first argument, accept the longest match
2791 // (first result). It's possible this module won't provide packages named by
2792 // later arguments, and other modules would. Let's not try to be too
2794 allowed := modload.CheckAllowed
2795 if modload.IsRevisionQuery(version) {
2796 // Don't check for retractions if a specific revision is requested.
2799 noneSelected := func(path string) (version string) { return "none" }
2800 qrs, err := modload.QueryPackages(ctx, patterns[0], version, noneSelected, allowed)
2802 return nil, fmt.Errorf("%s: %w", args[0], err)
2804 rootMod := qrs[0].Mod
2805 data, err := modfetch.GoMod(rootMod.Path, rootMod.Version)
2807 return nil, fmt.Errorf("%s: %w", args[0], err)
2809 f, err := modfile.Parse("go.mod", data, nil)
2811 return nil, fmt.Errorf("%s (in %s): %w", args[0], rootMod, err)
2813 directiveFmt := "%s (in %s):\n" +
2814 "\tThe go.mod file for the module providing named packages contains one or\n" +
2815 "\tmore %s directives. It must not contain directives that would cause\n" +
2816 "\tit to be interpreted differently than if it were the main module."
2817 if len(f.Replace) > 0 {
2818 return nil, fmt.Errorf(directiveFmt, args[0], rootMod, "replace")
2820 if len(f.Exclude) > 0 {
2821 return nil, fmt.Errorf(directiveFmt, args[0], rootMod, "exclude")
2824 // Since we are in NoRoot mode, the build list initially contains only
2825 // the dummy command-line-arguments module. Add a requirement on the
2826 // module that provides the packages named on the command line.
2827 if _, err := modload.EditBuildList(ctx, nil, []module.Version{rootMod}); err != nil {
2828 return nil, fmt.Errorf("%s: %w", args[0], err)
2831 // Load packages for all arguments.
2832 pkgs := PackagesAndErrors(ctx, opts, patterns)
2834 // Check that named packages are all provided by the same module.
2835 for _, pkg := range pkgs {
2837 if pkg.Module == nil {
2838 // Packages in std, cmd, and their vendored dependencies
2839 // don't have this field set.
2840 pkgErr = fmt.Errorf("package %s not provided by module %s", pkg.ImportPath, rootMod)
2841 } else if pkg.Module.Path != rootMod.Path || pkg.Module.Version != rootMod.Version {
2842 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)
2844 if pkgErr != nil && pkg.Error == nil {
2845 pkg.Error = &PackageError{Err: pkgErr}
2849 matchers := make([]func(string) bool, len(patterns))
2850 for i, p := range patterns {
2851 if strings.Contains(p, "...") {
2852 matchers[i] = search.MatchPattern(p)