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

View as plain text