type Package … type PackagePublic … // AllFiles returns the names of all the files considered for the package. // This is used for sanity and security checks, so we include all files, // even IgnoredGoFiles, because some subcommands consider them. // The go/build package filtered others out (like foo_wrongGOARCH.s) // and that's OK. func (p *Package) AllFiles() []string { … } // Desc returns the package "description", for use in b.showOutput. func (p *Package) Desc() string { … } // IsTestOnly reports whether p is a test-only package. // // A “test-only” package is one that: // - is a test-only variant of an ordinary package, or // - is a synthesized "main" package for a test binary, or // - contains only _test.go files. func (p *Package) IsTestOnly() bool { … } type PackageInternal … type NoGoError … func (e *NoGoError) Error() string { … } // setLoadPackageDataError presents an error found when loading package data // as a *PackageError. It has special cases for some common errors to improve // messages shown to users and reduce redundancy. // // setLoadPackageDataError returns true if it's safe to load information about // imported packages, for example, if there was a parse error loading imports // in one file, but other files are okay. func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportStack, importPos []token.Position) { … } // Resolve returns the resolved version of imports, // which should be p.TestImports or p.XTestImports, NOT p.Imports. // The imports in p.TestImports and p.XTestImports are not recursively // loaded during the initial load of p, so they list the imports found in // the source file, but most processing should be over the vendor-resolved // import paths. We do this resolution lazily both to avoid file system work // and because the eventual real load of the test imports (during 'go test') // can produce better error messages if it starts with the original paths. // The initial load of p loads all the non-test imports and rewrites // the vendored paths, so nothing should ever call p.vendored(p.Imports). func (p *Package) Resolve(imports []string) []string { … } type CoverVar … type CoverSetup … func (p *Package) copyBuild(opts PackageOpts, pp *build.Package) { … } type PackageError … func (p *PackageError) Error() string { … } func (p *PackageError) Unwrap() error { … } // PackageError implements MarshalJSON so that Err is marshaled as a string // and non-essential fields are omitted. func (p *PackageError) MarshalJSON() ([]byte, error) { … } func (p *PackageError) setPos(posList []token.Position) { … } type ImportPathError … var _ … var _ … var _ … var _ … var _ … type importError … func ImportErrorf(path, format string, args ...any) ImportPathError { … } func (e *importError) Error() string { … } func (e *importError) Unwrap() error { … } func (e *importError) ImportPath() string { … } type ImportInfo … type ImportStack … func NewImportInfo(pkg string, pos *token.Position) ImportInfo { … } func (s *ImportStack) Push(p ImportInfo) { … } func (s *ImportStack) Pop() { … } func (s *ImportStack) Copy() ImportStack { … } func (s *ImportStack) Pkgs() []string { … } func (s *ImportStack) PkgsWithPos() []string { … } func (s *ImportStack) Top() (ImportInfo, bool) { … } // shorterThan reports whether sp is shorter than t. // We use this to record the shortest import sequence // that leads to a particular package. func (sp *ImportStack) shorterThan(t []string) bool { … } var packageCache … // dirToImportPath returns the pseudo-import path we use for a package // outside the Go path. It begins with _/ and then contains the full path // to the directory. If the package lives in c:\home\gopher\my\pkg then // the pseudo-import path is _/c_/home/gopher/my/pkg. // Using a pseudo-import path like this makes the ./ imports no longer // a special case, so that all the code to deal with ordinary imports works // automatically. func dirToImportPath(dir string) string { … } func makeImportValid(r rune) rune { … } const ResolveImport … const ResolveModule … const GetTestDeps … const cmdlinePkg … const cmdlinePkgLiteral … // LoadPackage does Load import, but without a parent package load context func LoadPackage(ctx context.Context, opts PackageOpts, path, srcDir string, stk *ImportStack, importPos []token.Position, mode int) *Package { … } // loadImport scans the directory named by path, which must be an import path, // but possibly a local import path (an absolute file system path or one beginning // with ./ or ../). A local relative path is interpreted relative to srcDir. // It returns a *Package describing the package found in that directory. // loadImport does not set tool flags and should only be used by // this package, as part of a bigger load operation. // The returned PackageError, if any, describes why parent is not allowed // to import the named package, with the error referring to importPos. // The PackageError can only be non-nil when parent is not nil. func loadImport(ctx context.Context, opts PackageOpts, pre *preload, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) (*Package, *PackageError) { … } func extractFirstImport(importPos []token.Position) *token.Position { … } // loadPackageData loads information needed to construct a *Package. The result // is cached, and later calls to loadPackageData for the same package will return // the same data. // // loadPackageData returns a non-nil package even if err is non-nil unless // the package path is malformed (for example, the path contains "mod/" or "@"). // // loadPackageData returns a boolean, loaded, which is true if this is the // first time the package was loaded. Callers may preload imports in this case. func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoot string, parentIsStd bool, mode int) (bp *build.Package, loaded bool, err error) { … } type importSpec … type resolvedImport … var resolvedImportCache … var packageDataCache … var preloadWorkerCount … type preload … // newPreload creates a new preloader. flush must be called later to avoid // accessing global state while it is being modified. func newPreload() *preload { … } // preloadMatches loads data for package paths matched by patterns. // When preloadMatches returns, some packages may not be loaded yet, but // loadPackageData and loadImport are always safe to call. func (pre *preload) preloadMatches(ctx context.Context, opts PackageOpts, matches []*search.Match) { … } // preloadImports queues a list of imports for preloading. // When preloadImports returns, some packages may not be loaded yet, // but loadPackageData and loadImport are always safe to call. func (pre *preload) preloadImports(ctx context.Context, opts PackageOpts, imports []string, parent *build.Package) { … } // flush stops pending preload operations. flush blocks until preload calls to // loadPackageData have completed. The preloader will not make any new calls // to loadPackageData. func (pre *preload) flush() { … } func cleanImport(path string) string { … } var isDirCache … func isDir(path string) bool { … } // ResolveImportPath returns the true meaning of path when it appears in parent. // There are two different resolutions applied. // First, there is Go 1.5 vendoring (golang.org/s/go15vendor). // If vendor expansion doesn't trigger, then the path is also subject to // Go 1.11 module legacy conversion (golang.org/issue/25069). func ResolveImportPath(parent *Package, path string) (found string) { … } func resolveImportPath(path, parentPath, parentDir, parentRoot string, parentIsStd bool) (found string) { … } // dirAndRoot returns the source directory and workspace root // for the package p, guaranteeing that root is a path prefix of dir. func dirAndRoot(path string, dir, root string) (string, string) { … } // vendoredImportPath returns the vendor-expansion of path when it appears in parent. // If parent is x/y/z, then path might expand to x/y/z/vendor/path, x/y/vendor/path, // x/vendor/path, vendor/path, or else stay path if none of those exist. // vendoredImportPath returns the expanded path or, if no expansion is found, the original. func vendoredImportPath(path, parentPath, parentDir, parentRoot string) (found string) { … } var modulePrefix … var goModPathCache … // goModPath returns the module path in the go.mod in dir, if any. func goModPath(dir string) (path string) { … } // findVersionElement returns the slice indices of the final version element /vN in path. // If there is no such element, it returns -1, -1. func findVersionElement(path string) (i, j int) { … } // isVersionElement reports whether s is a well-formed path version element: // v2, v3, v10, etc, but not v0, v05, v1. func isVersionElement(s string) bool { … } // moduleImportPath translates import paths found in go modules // back down to paths that can be resolved in ordinary builds. // // Define “new” code as code with a go.mod file in the same directory // or a parent directory. If an import in new code says x/y/v2/z but // x/y/v2/z does not exist and x/y/go.mod says “module x/y/v2”, // then go build will read the import as x/y/z instead. // See golang.org/issue/25069. func moduleImportPath(path, parentPath, parentDir, parentRoot string) (found string) { … } // hasGoFiles reports whether dir contains any files with names ending in .go. // For a vendor check we must exclude directories that contain no .go files. // Otherwise it is not possible to vendor just a/b/c and still import the // non-vendored a/b. See golang.org/issue/13832. func hasGoFiles(dir string) bool { … } // reusePackage reuses package p to satisfy the import at the top // of the import stack stk. If this use causes an import loop, // reusePackage updates p's error information to record the loop. func reusePackage(p *Package, stk *ImportStack) *Package { … } // disallowInternal checks that srcDir (containing package importerPath, if non-empty) // is allowed to import p. // If the import is allowed, disallowInternal returns the original package p. // If not, it returns a new package containing just an appropriate error. func disallowInternal(ctx context.Context, srcDir string, importer *Package, importerPath string, p *Package, stk *ImportStack) *PackageError { … } // findInternal looks for the final "internal" path element in the given import path. // If there isn't one, findInternal returns ok=false. // Otherwise, findInternal returns ok=true and the index of the "internal". func findInternal(path string) (index int, ok bool) { … } // disallowVendor checks that srcDir is allowed to import p as path. // If the import is allowed, disallowVendor returns the original package p. // If not, it returns a PackageError. func disallowVendor(srcDir string, path string, importerPath string, p *Package, stk *ImportStack) *PackageError { … } // disallowVendorVisibility checks that srcDir is allowed to import p. // The rules are the same as for /internal/ except that a path ending in /vendor // is not subject to the rules, only subdirectories of vendor. // This allows people to have packages and commands named vendor, // for maximal compatibility with existing source trees. func disallowVendorVisibility(srcDir string, p *Package, importerPath string, stk *ImportStack) *PackageError { … } // FindVendor looks for the last non-terminating "vendor" path element in the given import path. // If there isn't one, FindVendor returns ok=false. // Otherwise, FindVendor returns ok=true and the index of the "vendor". // // Note that terminating "vendor" elements don't count: "x/vendor" is its own package, // not the vendored copy of an import "" (the empty import path). // This will allow people to have packages or commands named vendor. // This may help reduce breakage, or it may just be confusing. We'll see. func FindVendor(path string) (index int, ok bool) { … } type TargetDir … const ToTool … const ToBin … const StalePath … // InstallTargetDir reports the target directory for installing the command p. func InstallTargetDir(p *Package) TargetDir { … } var cgoExclude … var cgoSyscallExclude … var foldPath … // exeFromImportPath returns an executable name // for a package using the import path. // // The executable name is the last element of the import path. // In module-aware mode, an additional rule is used on import paths // consisting of two or more path elements. If the last element is // a vN path element specifying the major version, then the // second last element of the import path is used instead. func (p *Package) exeFromImportPath() string { … } // exeFromFiles returns an executable name for a package // using the first element in GoFiles or CgoFiles collections without the prefix. // // Returns empty string in case of empty collection. func (p *Package) exeFromFiles() string { … } // DefaultExecName returns the default executable name for a package func (p *Package) DefaultExecName() string { … } // load populates p using information from bp, err, which should // be the result of calling build.Context.Import. // stk contains the import stack, not including path itself. func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *ImportStack, importPos []token.Position, bp *build.Package, err error) { … } type EmbedError … func (e *EmbedError) Error() string { … } func (e *EmbedError) Unwrap() error { … } // ResolveEmbed resolves //go:embed patterns and returns only the file list. // For use by go mod vendor to find embedded files it should copy into the // vendor directory. // TODO(#42504): Once go mod vendor uses load.PackagesAndErrors, just // call (*Package).ResolveEmbed func ResolveEmbed(dir string, patterns []string) ([]string, error) { … } // resolveEmbed resolves //go:embed patterns to precise file lists. // It sets files to the list of unique files matched (for go list), // and it sets pmap to the more precise mapping from // patterns to files. func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[string][]string, err error) { … } func validEmbedPattern(pattern string) bool { … } // isBadEmbedName reports whether name is the base name of a file that // can't or won't be included in modules and therefore shouldn't be treated // as existing for embedding. func isBadEmbedName(name string) bool { … } var vcsStatusCache … func appendBuildSetting(info *debug.BuildInfo, key, value string) { … } // setBuildInfo gathers build information and sets it into // p.Internal.BuildInfo, which will later be formatted as a string and embedded // in the binary. setBuildInfo should only be called on a main package with no // errors. // // This information can be retrieved using debug.ReadBuildInfo. // // Note that the GoVersion field is not set here to avoid encoding it twice. // It is stored separately in the binary, mostly for historical reasons. func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) { … } // SafeArg reports whether arg is a "safe" command-line argument, // meaning that when it appears in a command-line, it probably // doesn't have some special meaning other than its own name. // Obviously args beginning with - are not safe (they look like flags). // Less obviously, args beginning with @ are not safe (they look like // GNU binutils flagfile specifiers, sometimes called "response files"). // To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII. // We accept leading . _ and / as likely in file system paths. // There is a copy of this function in cmd/compile/internal/gc/noder.go. func SafeArg(name string) bool { … } // LinkerDeps returns the list of linker-induced dependencies for main package p. func LinkerDeps(p *Package) ([]string, error) { … } // externalLinkingReason reports the reason external linking is required // even for programs that do not use cgo, or the empty string if external // linking is not required. func externalLinkingReason(p *Package) (what string) { … } // mkAbs rewrites list, which must be paths relative to p.Dir, // into a sorted list of absolute paths. It edits list in place but for // convenience also returns list back to its caller. func (p *Package) mkAbs(list []string) []string { … } // InternalGoFiles returns the list of Go files being built for the package, // using absolute paths. func (p *Package) InternalGoFiles() []string { … } // InternalXGoFiles returns the list of Go files being built for the XTest package, // using absolute paths. func (p *Package) InternalXGoFiles() []string { … } // InternalAllGoFiles returns the list of all Go files possibly relevant for the package, // using absolute paths. "Possibly relevant" means that files are not excluded // due to build tags, but files with names beginning with . or _ are still excluded. func (p *Package) InternalAllGoFiles() []string { … } // UsesSwig reports whether the package needs to run SWIG. func (p *Package) UsesSwig() bool { … } // UsesCgo reports whether the package needs to run cgo func (p *Package) UsesCgo() bool { … } // PackageList returns the list of packages in the dag rooted at roots // as visited in a depth-first post-order traversal. func PackageList(roots []*Package) []*Package { … } // TestPackageList returns the list of packages in the dag rooted at roots // as visited in a depth-first post-order traversal, including the test // imports of the roots. This ignores errors in test packages. func TestPackageList(ctx context.Context, opts PackageOpts, roots []*Package) []*Package { … } // LoadImportWithFlags loads the package with the given import path and // sets tool flags on that package. This function is useful loading implicit // dependencies (like sync/atomic for coverage). // TODO(jayconrod): delete this function and set flags automatically // in LoadImport instead. func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) (*Package, *PackageError) { … } // LoadPackageWithFlags is the same as LoadImportWithFlags but without a parent. // It's then guaranteed to not return an error func LoadPackageWithFlags(path, srcDir string, stk *ImportStack, importPos []token.Position, mode int) *Package { … } type PackageOpts … // PackagesAndErrors returns the packages named by the command line arguments // 'patterns'. If a named package cannot be loaded, PackagesAndErrors returns // a *Package with the Error field describing the failure. If errors are found // loading imported packages, the DepsErrors field is set. The Incomplete field // may be set as well. // // To obtain a flat list of packages, use PackageList. // To report errors loading packages, use ReportPackageErrors. func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string) []*Package { … } // setPGOProfilePath sets the PGO profile path for pkgs. // In -pgo=auto mode, it finds the default PGO profile. func setPGOProfilePath(pkgs []*Package) { … } // CheckPackageErrors prints errors encountered loading pkgs and their // dependencies, then exits with a non-zero status if any errors were found. func CheckPackageErrors(pkgs []*Package) { … } // mainPackagesOnly filters out non-main packages matched only by arguments // containing "..." and returns the remaining main packages. // // Packages with missing, invalid, or ambiguous names may be treated as // possibly-main packages. // // mainPackagesOnly sets a non-main package's Error field and returns it if it // is named by a literal argument. // // mainPackagesOnly prints warnings for non-literal arguments that only match // non-main packages. func mainPackagesOnly(pkgs []*Package, matches []*search.Match) []*Package { … } type mainPackageError … func (e *mainPackageError) Error() string { … } func (e *mainPackageError) ImportPath() string { … } func setToolFlags(pkgs ...*Package) { … } // GoFilesPackage creates a package for building a collection of Go files // (typically named on the command line). The target is named p.a for // package p or named after the first Go file for package main. func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Package { … } // PackagesAndErrorsOutsideModule is like PackagesAndErrors but runs in // module-aware mode and ignores the go.mod file in the current directory or any // parent directory, if there is one. This is used in the implementation of 'go // install pkg@version' and other commands that support similar forms. // // modload.ForceUseModules must be true, and modload.RootMode must be NoRoot // before calling this function. // // PackagesAndErrorsOutsideModule imposes several constraints to avoid // ambiguity. All arguments must have the same version suffix (not just a suffix // that resolves to the same version). They must refer to packages in the same // module, which must not be std or cmd. That module is not considered the main // module, but its go.mod file (if it has one) must not contain directives that // would cause it to be interpreted differently if it were the main module // (replace, exclude). func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args []string) ([]*Package, error) { … } // EnsureImport ensures that package p imports the named package. func EnsureImport(p *Package, pkg string) { … } // PrepareForCoverageBuild is a helper invoked for "go install // -cover", "go run -cover", and "go build -cover" (but not used by // "go test -cover"). It walks through the packages being built (and // dependencies) and marks them for coverage instrumentation when // appropriate, and possibly adding additional deps where needed. func PrepareForCoverageBuild(pkgs []*Package) { … } func SelectCoverPackages(roots []*Package, match []func(*Package) bool, op string) []*Package { … } // DeclareCoverVars attaches the required cover variables names // to the files, to be used when annotating the files. This // function only called when using legacy coverage test/build // (e.g. GOEXPERIMENT=coverageredesign is off). func DeclareCoverVars(p *Package, files ...string) map[string]*CoverVar { … }