Source file src/cmd/go/internal/load/pkg.go

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

View as plain text