github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/go/internal/load/pkg.go (about)

     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  	"fmt"
    11  	"go/build"
    12  	"go/token"
    13  	"io/ioutil"
    14  	"os"
    15  	pathpkg "path"
    16  	"path/filepath"
    17  	"sort"
    18  	"strconv"
    19  	"strings"
    20  	"unicode"
    21  	"unicode/utf8"
    22  
    23  	"cmd/go/internal/base"
    24  	"cmd/go/internal/cfg"
    25  	"cmd/go/internal/str"
    26  )
    27  
    28  var IgnoreImports bool // control whether we ignore imports in packages
    29  
    30  // A Package describes a single package found in a directory.
    31  type Package struct {
    32  	PackagePublic                 // visible in 'go list'
    33  	Internal      PackageInternal // for use inside go command only
    34  }
    35  
    36  type PackagePublic struct {
    37  	// Note: These fields are part of the go command's public API.
    38  	// See list.go. It is okay to add fields, but not to change or
    39  	// remove existing ones. Keep in sync with list.go
    40  	Dir           string `json:",omitempty"` // directory containing package sources
    41  	ImportPath    string `json:",omitempty"` // import path of package in dir
    42  	ImportComment string `json:",omitempty"` // path in import comment on package statement
    43  	Name          string `json:",omitempty"` // package name
    44  	Doc           string `json:",omitempty"` // package documentation string
    45  	Target        string `json:",omitempty"` // installed target for this package (may be executable)
    46  	Shlib         string `json:",omitempty"` // the shared library that contains this package (only set when -linkshared)
    47  	Goroot        bool   `json:",omitempty"` // is this package found in the Go root?
    48  	Standard      bool   `json:",omitempty"` // is this package part of the standard Go library?
    49  	Root          string `json:",omitempty"` // Go root or Go path dir containing this package
    50  	ConflictDir   string `json:",omitempty"` // Dir is hidden by this other directory
    51  	BinaryOnly    bool   `json:",omitempty"` // package cannot be recompiled
    52  
    53  	// Stale and StaleReason remain here *only* for the list command.
    54  	// They are only initialized in preparation for list execution.
    55  	// The regular build determines staleness on the fly during action execution.
    56  	Stale       bool   `json:",omitempty"` // would 'go install' do anything for this package?
    57  	StaleReason string `json:",omitempty"` // why is Stale true?
    58  
    59  	// Source files
    60  	// If you add to this list you MUST add to p.AllFiles (below) too.
    61  	// Otherwise file name security lists will not apply to any new additions.
    62  	GoFiles        []string `json:",omitempty"` // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
    63  	CgoFiles       []string `json:",omitempty"` // .go sources files that import "C"
    64  	IgnoredGoFiles []string `json:",omitempty"` // .go sources ignored due to build constraints
    65  	CFiles         []string `json:",omitempty"` // .c source files
    66  	CXXFiles       []string `json:",omitempty"` // .cc, .cpp and .cxx source files
    67  	MFiles         []string `json:",omitempty"` // .m source files
    68  	HFiles         []string `json:",omitempty"` // .h, .hh, .hpp and .hxx source files
    69  	FFiles         []string `json:",omitempty"` // .f, .F, .for and .f90 Fortran source files
    70  	SFiles         []string `json:",omitempty"` // .s source files
    71  	SwigFiles      []string `json:",omitempty"` // .swig files
    72  	SwigCXXFiles   []string `json:",omitempty"` // .swigcxx files
    73  	SysoFiles      []string `json:",omitempty"` // .syso system object files added to package
    74  
    75  	// Cgo directives
    76  	CgoCFLAGS    []string `json:",omitempty"` // cgo: flags for C compiler
    77  	CgoCPPFLAGS  []string `json:",omitempty"` // cgo: flags for C preprocessor
    78  	CgoCXXFLAGS  []string `json:",omitempty"` // cgo: flags for C++ compiler
    79  	CgoFFLAGS    []string `json:",omitempty"` // cgo: flags for Fortran compiler
    80  	CgoLDFLAGS   []string `json:",omitempty"` // cgo: flags for linker
    81  	CgoPkgConfig []string `json:",omitempty"` // cgo: pkg-config names
    82  
    83  	// Dependency information
    84  	Imports []string `json:",omitempty"` // import paths used by this package
    85  	Deps    []string `json:",omitempty"` // all (recursively) imported dependencies
    86  
    87  	// Error information
    88  	Incomplete bool            `json:",omitempty"` // was there an error loading this package or dependencies?
    89  	Error      *PackageError   `json:",omitempty"` // error loading this package (not dependencies)
    90  	DepsErrors []*PackageError `json:",omitempty"` // errors loading dependencies
    91  
    92  	// Test information
    93  	// If you add to this list you MUST add to p.AllFiles (below) too.
    94  	// Otherwise file name security lists will not apply to any new additions.
    95  	TestGoFiles  []string `json:",omitempty"` // _test.go files in package
    96  	TestImports  []string `json:",omitempty"` // imports from TestGoFiles
    97  	XTestGoFiles []string `json:",omitempty"` // _test.go files outside package
    98  	XTestImports []string `json:",omitempty"` // imports from XTestGoFiles
    99  }
   100  
   101  // AllFiles returns the names of all the files considered for the package.
   102  // This is used for sanity and security checks, so we include all files,
   103  // even IgnoredGoFiles, because some subcommands consider them.
   104  // The go/build package filtered others out (like foo_wrongGOARCH.s)
   105  // and that's OK.
   106  func (p *Package) AllFiles() []string {
   107  	return str.StringList(
   108  		p.GoFiles,
   109  		p.CgoFiles,
   110  		p.IgnoredGoFiles,
   111  		p.CFiles,
   112  		p.CXXFiles,
   113  		p.MFiles,
   114  		p.HFiles,
   115  		p.FFiles,
   116  		p.SFiles,
   117  		p.SwigFiles,
   118  		p.SwigCXXFiles,
   119  		p.SysoFiles,
   120  		p.TestGoFiles,
   121  		p.XTestGoFiles,
   122  	)
   123  }
   124  
   125  type PackageInternal struct {
   126  	// Unexported fields are not part of the public API.
   127  	Build        *build.Package
   128  	Imports      []*Package           // this package's direct imports
   129  	RawImports   []string             // this package's original imports as they appear in the text of the program
   130  	ForceLibrary bool                 // this package is a library (even if named "main")
   131  	CmdlineFiles bool                 // package built from files listed on command line
   132  	CmdlinePkg   bool                 // package listed on command line
   133  	Local        bool                 // imported via local path (./ or ../)
   134  	LocalPrefix  string               // interpret ./ and ../ imports relative to this prefix
   135  	ExeName      string               // desired name for temporary executable
   136  	CoverMode    string               // preprocess Go source files with the coverage tool in this mode
   137  	CoverVars    map[string]*CoverVar // variables created by coverage analysis
   138  	OmitDebug    bool                 // tell linker not to write debug information
   139  	GobinSubdir  bool                 // install target would be subdir of GOBIN
   140  
   141  	Asmflags   []string // -asmflags for this package
   142  	Gcflags    []string // -gcflags for this package
   143  	Ldflags    []string // -ldflags for this package
   144  	Gccgoflags []string // -gccgoflags for this package
   145  }
   146  
   147  type NoGoError struct {
   148  	Package *Package
   149  }
   150  
   151  func (e *NoGoError) Error() string {
   152  	// Count files beginning with _ and ., which we will pretend don't exist at all.
   153  	dummy := 0
   154  	for _, name := range e.Package.IgnoredGoFiles {
   155  		if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") {
   156  			dummy++
   157  		}
   158  	}
   159  
   160  	if len(e.Package.IgnoredGoFiles) > dummy {
   161  		// Go files exist, but they were ignored due to build constraints.
   162  		return "build constraints exclude all Go files in " + e.Package.Dir
   163  	}
   164  	if len(e.Package.TestGoFiles)+len(e.Package.XTestGoFiles) > 0 {
   165  		// Test Go files exist, but we're not interested in them.
   166  		// The double-negative is unfortunate but we want e.Package.Dir
   167  		// to appear at the end of error message.
   168  		return "no non-test Go files in " + e.Package.Dir
   169  	}
   170  	return "no Go files in " + e.Package.Dir
   171  }
   172  
   173  // Resolve returns the resolved version of imports,
   174  // which should be p.TestImports or p.XTestImports, NOT p.Imports.
   175  // The imports in p.TestImports and p.XTestImports are not recursively
   176  // loaded during the initial load of p, so they list the imports found in
   177  // the source file, but most processing should be over the vendor-resolved
   178  // import paths. We do this resolution lazily both to avoid file system work
   179  // and because the eventual real load of the test imports (during 'go test')
   180  // can produce better error messages if it starts with the original paths.
   181  // The initial load of p loads all the non-test imports and rewrites
   182  // the vendored paths, so nothing should ever call p.vendored(p.Imports).
   183  func (p *Package) Resolve(imports []string) []string {
   184  	if len(imports) > 0 && len(p.Imports) > 0 && &imports[0] == &p.Imports[0] {
   185  		panic("internal error: p.Resolve(p.Imports) called")
   186  	}
   187  	seen := make(map[string]bool)
   188  	var all []string
   189  	for _, path := range imports {
   190  		path = ResolveImportPath(p, path)
   191  		if !seen[path] {
   192  			seen[path] = true
   193  			all = append(all, path)
   194  		}
   195  	}
   196  	sort.Strings(all)
   197  	return all
   198  }
   199  
   200  // CoverVar holds the name of the generated coverage variables targeting the named file.
   201  type CoverVar struct {
   202  	File string // local file name
   203  	Var  string // name of count struct
   204  }
   205  
   206  func (p *Package) copyBuild(pp *build.Package) {
   207  	p.Internal.Build = pp
   208  
   209  	if pp.PkgTargetRoot != "" && cfg.BuildPkgdir != "" {
   210  		old := pp.PkgTargetRoot
   211  		pp.PkgRoot = cfg.BuildPkgdir
   212  		pp.PkgTargetRoot = cfg.BuildPkgdir
   213  		pp.PkgObj = filepath.Join(cfg.BuildPkgdir, strings.TrimPrefix(pp.PkgObj, old))
   214  	}
   215  
   216  	p.Dir = pp.Dir
   217  	p.ImportPath = pp.ImportPath
   218  	p.ImportComment = pp.ImportComment
   219  	p.Name = pp.Name
   220  	p.Doc = pp.Doc
   221  	p.Root = pp.Root
   222  	p.ConflictDir = pp.ConflictDir
   223  	p.BinaryOnly = pp.BinaryOnly
   224  
   225  	// TODO? Target
   226  	p.Goroot = pp.Goroot
   227  	p.Standard = p.Goroot && p.ImportPath != "" && isStandardImportPath(p.ImportPath)
   228  	p.GoFiles = pp.GoFiles
   229  	p.CgoFiles = pp.CgoFiles
   230  	p.IgnoredGoFiles = pp.IgnoredGoFiles
   231  	p.CFiles = pp.CFiles
   232  	p.CXXFiles = pp.CXXFiles
   233  	p.MFiles = pp.MFiles
   234  	p.HFiles = pp.HFiles
   235  	p.FFiles = pp.FFiles
   236  	p.SFiles = pp.SFiles
   237  	p.SwigFiles = pp.SwigFiles
   238  	p.SwigCXXFiles = pp.SwigCXXFiles
   239  	p.SysoFiles = pp.SysoFiles
   240  	p.CgoCFLAGS = pp.CgoCFLAGS
   241  	p.CgoCPPFLAGS = pp.CgoCPPFLAGS
   242  	p.CgoCXXFLAGS = pp.CgoCXXFLAGS
   243  	p.CgoFFLAGS = pp.CgoFFLAGS
   244  	p.CgoLDFLAGS = pp.CgoLDFLAGS
   245  	p.CgoPkgConfig = pp.CgoPkgConfig
   246  	// We modify p.Imports in place, so make copy now.
   247  	p.Imports = make([]string, len(pp.Imports))
   248  	copy(p.Imports, pp.Imports)
   249  	p.Internal.RawImports = pp.Imports
   250  	p.TestGoFiles = pp.TestGoFiles
   251  	p.TestImports = pp.TestImports
   252  	p.XTestGoFiles = pp.XTestGoFiles
   253  	p.XTestImports = pp.XTestImports
   254  	if IgnoreImports {
   255  		p.Imports = nil
   256  		p.TestImports = nil
   257  		p.XTestImports = nil
   258  	}
   259  }
   260  
   261  // isStandardImportPath reports whether $GOROOT/src/path should be considered
   262  // part of the standard distribution. For historical reasons we allow people to add
   263  // their own code to $GOROOT instead of using $GOPATH, but we assume that
   264  // code will start with a domain name (dot in the first element).
   265  func isStandardImportPath(path string) bool {
   266  	i := strings.Index(path, "/")
   267  	if i < 0 {
   268  		i = len(path)
   269  	}
   270  	elem := path[:i]
   271  	return !strings.Contains(elem, ".")
   272  }
   273  
   274  // A PackageError describes an error loading information about a package.
   275  type PackageError struct {
   276  	ImportStack   []string // shortest path from package named on command line to this one
   277  	Pos           string   // position of error
   278  	Err           string   // the error itself
   279  	IsImportCycle bool     `json:"-"` // the error is an import cycle
   280  	Hard          bool     `json:"-"` // whether the error is soft or hard; soft errors are ignored in some places
   281  }
   282  
   283  func (p *PackageError) Error() string {
   284  	// Import cycles deserve special treatment.
   285  	if p.IsImportCycle {
   286  		return fmt.Sprintf("%s\npackage %s\n", p.Err, strings.Join(p.ImportStack, "\n\timports "))
   287  	}
   288  	if p.Pos != "" {
   289  		// Omit import stack. The full path to the file where the error
   290  		// is the most important thing.
   291  		return p.Pos + ": " + p.Err
   292  	}
   293  	if len(p.ImportStack) == 0 {
   294  		return p.Err
   295  	}
   296  	return "package " + strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Err
   297  }
   298  
   299  // An ImportStack is a stack of import paths.
   300  type ImportStack []string
   301  
   302  func (s *ImportStack) Push(p string) {
   303  	*s = append(*s, p)
   304  }
   305  
   306  func (s *ImportStack) Pop() {
   307  	*s = (*s)[0 : len(*s)-1]
   308  }
   309  
   310  func (s *ImportStack) Copy() []string {
   311  	return append([]string{}, *s...)
   312  }
   313  
   314  // shorterThan reports whether sp is shorter than t.
   315  // We use this to record the shortest import sequence
   316  // that leads to a particular package.
   317  func (sp *ImportStack) shorterThan(t []string) bool {
   318  	s := *sp
   319  	if len(s) != len(t) {
   320  		return len(s) < len(t)
   321  	}
   322  	// If they are the same length, settle ties using string ordering.
   323  	for i := range s {
   324  		if s[i] != t[i] {
   325  			return s[i] < t[i]
   326  		}
   327  	}
   328  	return false // they are equal
   329  }
   330  
   331  // packageCache is a lookup cache for loadPackage,
   332  // so that if we look up a package multiple times
   333  // we return the same pointer each time.
   334  var packageCache = map[string]*Package{}
   335  
   336  func ClearPackageCache() {
   337  	for name := range packageCache {
   338  		delete(packageCache, name)
   339  	}
   340  }
   341  
   342  func ClearPackageCachePartial(args []string) {
   343  	for _, arg := range args {
   344  		p := packageCache[arg]
   345  		if p != nil {
   346  			delete(packageCache, p.Dir)
   347  			delete(packageCache, p.ImportPath)
   348  		}
   349  	}
   350  }
   351  
   352  // reloadPackage is like loadPackage but makes sure
   353  // not to use the package cache.
   354  func ReloadPackage(arg string, stk *ImportStack) *Package {
   355  	p := packageCache[arg]
   356  	if p != nil {
   357  		delete(packageCache, p.Dir)
   358  		delete(packageCache, p.ImportPath)
   359  	}
   360  	return LoadPackage(arg, stk)
   361  }
   362  
   363  // dirToImportPath returns the pseudo-import path we use for a package
   364  // outside the Go path. It begins with _/ and then contains the full path
   365  // to the directory. If the package lives in c:\home\gopher\my\pkg then
   366  // the pseudo-import path is _/c_/home/gopher/my/pkg.
   367  // Using a pseudo-import path like this makes the ./ imports no longer
   368  // a special case, so that all the code to deal with ordinary imports works
   369  // automatically.
   370  func dirToImportPath(dir string) string {
   371  	return pathpkg.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
   372  }
   373  
   374  func makeImportValid(r rune) rune {
   375  	// Should match Go spec, compilers, and ../../go/parser/parser.go:/isValidImport.
   376  	const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
   377  	if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
   378  		return '_'
   379  	}
   380  	return r
   381  }
   382  
   383  // Mode flags for loadImport and download (in get.go).
   384  const (
   385  	// ResolveImport means that loadImport should do import path expansion.
   386  	// That is, ResolveImport means that the import path came from
   387  	// a source file and has not been expanded yet to account for
   388  	// vendoring or possible module adjustment.
   389  	// Every import path should be loaded initially with ResolveImport,
   390  	// and then the expanded version (for example with the /vendor/ in it)
   391  	// gets recorded as the canonical import path. At that point, future loads
   392  	// of that package must not pass ResolveImport, because
   393  	// disallowVendor will reject direct use of paths containing /vendor/.
   394  	ResolveImport = 1 << iota
   395  
   396  	// ResolveModule is for download (part of "go get") and indicates
   397  	// that the module adjustment should be done, but not vendor adjustment.
   398  	ResolveModule
   399  
   400  	// GetTestDeps is for download (part of "go get") and indicates
   401  	// that test dependencies should be fetched too.
   402  	GetTestDeps
   403  )
   404  
   405  // LoadImport scans the directory named by path, which must be an import path,
   406  // but possibly a local import path (an absolute file system path or one beginning
   407  // with ./ or ../). A local relative path is interpreted relative to srcDir.
   408  // It returns a *Package describing the package found in that directory.
   409  func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
   410  	stk.Push(path)
   411  	defer stk.Pop()
   412  
   413  	// Determine canonical identifier for this package.
   414  	// For a local import the identifier is the pseudo-import path
   415  	// we create from the full directory to the package.
   416  	// Otherwise it is the usual import path.
   417  	// For vendored imports, it is the expanded form.
   418  	importPath := path
   419  	origPath := path
   420  	isLocal := build.IsLocalImport(path)
   421  	if isLocal {
   422  		importPath = dirToImportPath(filepath.Join(srcDir, path))
   423  	} else if mode&ResolveImport != 0 {
   424  		// We do our own path resolution, because we want to
   425  		// find out the key to use in packageCache without the
   426  		// overhead of repeated calls to buildContext.Import.
   427  		// The code is also needed in a few other places anyway.
   428  		path = ResolveImportPath(parent, path)
   429  		importPath = path
   430  	} else if mode&ResolveModule != 0 {
   431  		path = ModuleImportPath(parent, path)
   432  		importPath = path
   433  	}
   434  
   435  	p := packageCache[importPath]
   436  	if p != nil {
   437  		p = reusePackage(p, stk)
   438  	} else {
   439  		p = new(Package)
   440  		p.Internal.Local = isLocal
   441  		p.ImportPath = importPath
   442  		packageCache[importPath] = p
   443  
   444  		// Load package.
   445  		// Import always returns bp != nil, even if an error occurs,
   446  		// in order to return partial information.
   447  		buildMode := build.ImportComment
   448  		if mode&ResolveImport == 0 || path != origPath {
   449  			// Not vendoring, or we already found the vendored path.
   450  			buildMode |= build.IgnoreVendor
   451  		}
   452  		bp, err := cfg.BuildContext.Import(path, srcDir, buildMode)
   453  		bp.ImportPath = importPath
   454  		if cfg.GOBIN != "" {
   455  			bp.BinDir = cfg.GOBIN
   456  		}
   457  		if err == nil && !isLocal && bp.ImportComment != "" && bp.ImportComment != path &&
   458  			!strings.Contains(path, "/vendor/") && !strings.HasPrefix(path, "vendor/") {
   459  			err = fmt.Errorf("code in directory %s expects import %q", bp.Dir, bp.ImportComment)
   460  		}
   461  		p.load(stk, bp, err)
   462  		if p.Error != nil && p.Error.Pos == "" {
   463  			p = setErrorPos(p, importPos)
   464  		}
   465  
   466  		if origPath != cleanImport(origPath) {
   467  			p.Error = &PackageError{
   468  				ImportStack: stk.Copy(),
   469  				Err:         fmt.Sprintf("non-canonical import path: %q should be %q", origPath, pathpkg.Clean(origPath)),
   470  			}
   471  			p.Incomplete = true
   472  		}
   473  	}
   474  
   475  	// Checked on every import because the rules depend on the code doing the importing.
   476  	if perr := disallowInternal(srcDir, p, stk); perr != p {
   477  		return setErrorPos(perr, importPos)
   478  	}
   479  	if mode&ResolveImport != 0 {
   480  		if perr := disallowVendor(srcDir, origPath, p, stk); perr != p {
   481  			return setErrorPos(perr, importPos)
   482  		}
   483  	}
   484  
   485  	if p.Name == "main" && parent != nil && parent.Dir != p.Dir {
   486  		perr := *p
   487  		perr.Error = &PackageError{
   488  			ImportStack: stk.Copy(),
   489  			Err:         fmt.Sprintf("import %q is a program, not an importable package", path),
   490  		}
   491  		return setErrorPos(&perr, importPos)
   492  	}
   493  
   494  	if p.Internal.Local && parent != nil && !parent.Internal.Local {
   495  		perr := *p
   496  		perr.Error = &PackageError{
   497  			ImportStack: stk.Copy(),
   498  			Err:         fmt.Sprintf("local import %q in non-local package", path),
   499  		}
   500  		return setErrorPos(&perr, importPos)
   501  	}
   502  
   503  	return p
   504  }
   505  
   506  func setErrorPos(p *Package, importPos []token.Position) *Package {
   507  	if len(importPos) > 0 {
   508  		pos := importPos[0]
   509  		pos.Filename = base.ShortPath(pos.Filename)
   510  		p.Error.Pos = pos.String()
   511  	}
   512  	return p
   513  }
   514  
   515  func cleanImport(path string) string {
   516  	orig := path
   517  	path = pathpkg.Clean(path)
   518  	if strings.HasPrefix(orig, "./") && path != ".." && !strings.HasPrefix(path, "../") {
   519  		path = "./" + path
   520  	}
   521  	return path
   522  }
   523  
   524  var isDirCache = map[string]bool{}
   525  
   526  func isDir(path string) bool {
   527  	result, ok := isDirCache[path]
   528  	if ok {
   529  		return result
   530  	}
   531  
   532  	fi, err := os.Stat(path)
   533  	result = err == nil && fi.IsDir()
   534  	isDirCache[path] = result
   535  	return result
   536  }
   537  
   538  // ResolveImportPath returns the true meaning of path when it appears in parent.
   539  // There are two different resolutions applied.
   540  // First, there is Go 1.5 vendoring (golang.org/s/go15vendor).
   541  // If vendor expansion doesn't trigger, then the path is also subject to
   542  // Go 1.11 vgo legacy conversion (golang.org/issue/25069).
   543  func ResolveImportPath(parent *Package, path string) (found string) {
   544  	found = VendoredImportPath(parent, path)
   545  	if found != path {
   546  		return found
   547  	}
   548  	return ModuleImportPath(parent, path)
   549  }
   550  
   551  // dirAndRoot returns the source directory and workspace root
   552  // for the package p, guaranteeing that root is a path prefix of dir.
   553  func dirAndRoot(p *Package) (dir, root string) {
   554  	dir = filepath.Clean(p.Dir)
   555  	root = filepath.Join(p.Root, "src")
   556  	if !str.HasFilePathPrefix(dir, root) || p.ImportPath != "command-line-arguments" && filepath.Join(root, p.ImportPath) != dir {
   557  		// Look for symlinks before reporting error.
   558  		dir = expandPath(dir)
   559  		root = expandPath(root)
   560  	}
   561  
   562  	if !str.HasFilePathPrefix(dir, root) || len(dir) <= len(root) || dir[len(root)] != filepath.Separator || p.ImportPath != "command-line-arguments" && !p.Internal.Local && filepath.Join(root, p.ImportPath) != dir {
   563  		base.Fatalf("unexpected directory layout:\n"+
   564  			"	import path: %s\n"+
   565  			"	root: %s\n"+
   566  			"	dir: %s\n"+
   567  			"	expand root: %s\n"+
   568  			"	expand dir: %s\n"+
   569  			"	separator: %s",
   570  			p.ImportPath,
   571  			filepath.Join(p.Root, "src"),
   572  			filepath.Clean(p.Dir),
   573  			root,
   574  			dir,
   575  			string(filepath.Separator))
   576  	}
   577  
   578  	return dir, root
   579  }
   580  
   581  // VendoredImportPath returns the vendor-expansion of path when it appears in parent.
   582  // If parent is x/y/z, then path might expand to x/y/z/vendor/path, x/y/vendor/path,
   583  // x/vendor/path, vendor/path, or else stay path if none of those exist.
   584  // VendoredImportPath returns the expanded path or, if no expansion is found, the original.
   585  func VendoredImportPath(parent *Package, path string) (found string) {
   586  	if parent == nil || parent.Root == "" {
   587  		return path
   588  	}
   589  
   590  	dir, root := dirAndRoot(parent)
   591  
   592  	vpath := "vendor/" + path
   593  	for i := len(dir); i >= len(root); i-- {
   594  		if i < len(dir) && dir[i] != filepath.Separator {
   595  			continue
   596  		}
   597  		// Note: checking for the vendor directory before checking
   598  		// for the vendor/path directory helps us hit the
   599  		// isDir cache more often. It also helps us prepare a more useful
   600  		// list of places we looked, to report when an import is not found.
   601  		if !isDir(filepath.Join(dir[:i], "vendor")) {
   602  			continue
   603  		}
   604  		targ := filepath.Join(dir[:i], vpath)
   605  		if isDir(targ) && hasGoFiles(targ) {
   606  			importPath := parent.ImportPath
   607  			if importPath == "command-line-arguments" {
   608  				// If parent.ImportPath is 'command-line-arguments'.
   609  				// set to relative directory to root (also chopped root directory)
   610  				importPath = dir[len(root)+1:]
   611  			}
   612  			// We started with parent's dir c:\gopath\src\foo\bar\baz\quux\xyzzy.
   613  			// We know the import path for parent's dir.
   614  			// We chopped off some number of path elements and
   615  			// added vendor\path to produce c:\gopath\src\foo\bar\baz\vendor\path.
   616  			// Now we want to know the import path for that directory.
   617  			// Construct it by chopping the same number of path elements
   618  			// (actually the same number of bytes) from parent's import path
   619  			// and then append /vendor/path.
   620  			chopped := len(dir) - i
   621  			if chopped == len(importPath)+1 {
   622  				// We walked up from c:\gopath\src\foo\bar
   623  				// and found c:\gopath\src\vendor\path.
   624  				// We chopped \foo\bar (length 8) but the import path is "foo/bar" (length 7).
   625  				// Use "vendor/path" without any prefix.
   626  				return vpath
   627  			}
   628  			return importPath[:len(importPath)-chopped] + "/" + vpath
   629  		}
   630  	}
   631  	return path
   632  }
   633  
   634  var (
   635  	modulePrefix   = []byte("\nmodule ")
   636  	goModPathCache = make(map[string]string)
   637  )
   638  
   639  // goModPath returns the module path in the go.mod in dir, if any.
   640  func goModPath(dir string) (path string) {
   641  	path, ok := goModPathCache[dir]
   642  	if ok {
   643  		return path
   644  	}
   645  	defer func() {
   646  		goModPathCache[dir] = path
   647  	}()
   648  
   649  	data, err := ioutil.ReadFile(filepath.Join(dir, "go.mod"))
   650  	if err != nil {
   651  		return ""
   652  	}
   653  	var i int
   654  	if bytes.HasPrefix(data, modulePrefix[1:]) {
   655  		i = 0
   656  	} else {
   657  		i = bytes.Index(data, modulePrefix)
   658  		if i < 0 {
   659  			return ""
   660  		}
   661  		i++
   662  	}
   663  	line := data[i:]
   664  
   665  	// Cut line at \n, drop trailing \r if present.
   666  	if j := bytes.IndexByte(line, '\n'); j >= 0 {
   667  		line = line[:j]
   668  	}
   669  	if line[len(line)-1] == '\r' {
   670  		line = line[:len(line)-1]
   671  	}
   672  	line = line[len("module "):]
   673  
   674  	// If quoted, unquote.
   675  	path = strings.TrimSpace(string(line))
   676  	if path != "" && path[0] == '"' {
   677  		s, err := strconv.Unquote(path)
   678  		if err != nil {
   679  			return ""
   680  		}
   681  		path = s
   682  	}
   683  	return path
   684  }
   685  
   686  // findVersionElement returns the slice indices of the final version element /vN in path.
   687  // If there is no such element, it returns -1, -1.
   688  func findVersionElement(path string) (i, j int) {
   689  	j = len(path)
   690  	for i = len(path) - 1; i >= 0; i-- {
   691  		if path[i] == '/' {
   692  			if isVersionElement(path[i:j]) {
   693  				return i, j
   694  			}
   695  			j = i
   696  		}
   697  	}
   698  	return -1, -1
   699  }
   700  
   701  // isVersionElement reports whether s is a well-formed path version element:
   702  // v2, v3, v10, etc, but not v0, v05, v1.
   703  func isVersionElement(s string) bool {
   704  	if len(s) < 3 || s[0] != '/' || s[1] != 'v' || s[2] == '0' || s[2] == '1' && len(s) == 3 {
   705  		return false
   706  	}
   707  	for i := 2; i < len(s); i++ {
   708  		if s[i] < '0' || '9' < s[i] {
   709  			return false
   710  		}
   711  	}
   712  	return true
   713  }
   714  
   715  // ModuleImportPath translates import paths found in go modules
   716  // back down to paths that can be resolved in ordinary builds.
   717  //
   718  // Define “new” code as code with a go.mod file in the same directory
   719  // or a parent directory. If an import in new code says x/y/v2/z but
   720  // x/y/v2/z does not exist and x/y/go.mod says “module x/y/v2”,
   721  // then go build will read the import as x/y/z instead.
   722  // See golang.org/issue/25069.
   723  func ModuleImportPath(parent *Package, path string) (found string) {
   724  	if parent == nil || parent.Root == "" {
   725  		return path
   726  	}
   727  
   728  	// If there are no vN elements in path, leave it alone.
   729  	// (The code below would do the same, but only after
   730  	// some other file system accesses that we can avoid
   731  	// here by returning early.)
   732  	if i, _ := findVersionElement(path); i < 0 {
   733  		return path
   734  	}
   735  
   736  	dir, root := dirAndRoot(parent)
   737  
   738  	// Consider dir and parents, up to and including root.
   739  	for i := len(dir); i >= len(root); i-- {
   740  		if i < len(dir) && dir[i] != filepath.Separator {
   741  			continue
   742  		}
   743  		if goModPath(dir[:i]) != "" {
   744  			goto HaveGoMod
   745  		}
   746  	}
   747  	// This code is not in a tree with a go.mod,
   748  	// so apply no changes to the path.
   749  	return path
   750  
   751  HaveGoMod:
   752  	// This import is in a tree with a go.mod.
   753  	// Allow it to refer to code in GOPATH/src/x/y/z as x/y/v2/z
   754  	// if GOPATH/src/x/y/go.mod says module "x/y/v2",
   755  
   756  	// If x/y/v2/z exists, use it unmodified.
   757  	if bp, _ := cfg.BuildContext.Import(path, "", build.IgnoreVendor); bp.Dir != "" {
   758  		return path
   759  	}
   760  
   761  	// Otherwise look for a go.mod supplying a version element.
   762  	// Some version-like elements may appear in paths but not
   763  	// be module versions; we skip over those to look for module
   764  	// versions. For example the module m/v2 might have a
   765  	// package m/v2/api/v1/foo.
   766  	limit := len(path)
   767  	for limit > 0 {
   768  		i, j := findVersionElement(path[:limit])
   769  		if i < 0 {
   770  			return path
   771  		}
   772  		if bp, _ := cfg.BuildContext.Import(path[:i], "", build.IgnoreVendor); bp.Dir != "" {
   773  			if mpath := goModPath(bp.Dir); mpath != "" {
   774  				// Found a valid go.mod file, so we're stopping the search.
   775  				// If the path is m/v2/p and we found m/go.mod that says
   776  				// "module m/v2", then we return "m/p".
   777  				if mpath == path[:j] {
   778  					return path[:i] + path[j:]
   779  				}
   780  				// Otherwise just return the original path.
   781  				// We didn't find anything worth rewriting,
   782  				// and the go.mod indicates that we should
   783  				// not consider parent directories.
   784  				return path
   785  			}
   786  		}
   787  		limit = i
   788  	}
   789  	return path
   790  }
   791  
   792  // hasGoFiles reports whether dir contains any files with names ending in .go.
   793  // For a vendor check we must exclude directories that contain no .go files.
   794  // Otherwise it is not possible to vendor just a/b/c and still import the
   795  // non-vendored a/b. See golang.org/issue/13832.
   796  func hasGoFiles(dir string) bool {
   797  	fis, _ := ioutil.ReadDir(dir)
   798  	for _, fi := range fis {
   799  		if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".go") {
   800  			return true
   801  		}
   802  	}
   803  	return false
   804  }
   805  
   806  // reusePackage reuses package p to satisfy the import at the top
   807  // of the import stack stk. If this use causes an import loop,
   808  // reusePackage updates p's error information to record the loop.
   809  func reusePackage(p *Package, stk *ImportStack) *Package {
   810  	// We use p.Internal.Imports==nil to detect a package that
   811  	// is in the midst of its own loadPackage call
   812  	// (all the recursion below happens before p.Internal.Imports gets set).
   813  	if p.Internal.Imports == nil {
   814  		if p.Error == nil {
   815  			p.Error = &PackageError{
   816  				ImportStack:   stk.Copy(),
   817  				Err:           "import cycle not allowed",
   818  				IsImportCycle: true,
   819  			}
   820  		}
   821  		p.Incomplete = true
   822  	}
   823  	// Don't rewrite the import stack in the error if we have an import cycle.
   824  	// If we do, we'll lose the path that describes the cycle.
   825  	if p.Error != nil && !p.Error.IsImportCycle && stk.shorterThan(p.Error.ImportStack) {
   826  		p.Error.ImportStack = stk.Copy()
   827  	}
   828  	return p
   829  }
   830  
   831  // disallowInternal checks that srcDir is allowed to import p.
   832  // If the import is allowed, disallowInternal returns the original package p.
   833  // If not, it returns a new package containing just an appropriate error.
   834  func disallowInternal(srcDir string, p *Package, stk *ImportStack) *Package {
   835  	// golang.org/s/go14internal:
   836  	// An import of a path containing the element “internal”
   837  	// is disallowed if the importing code is outside the tree
   838  	// rooted at the parent of the “internal” directory.
   839  
   840  	// There was an error loading the package; stop here.
   841  	if p.Error != nil {
   842  		return p
   843  	}
   844  
   845  	// The generated 'testmain' package is allowed to access testing/internal/...,
   846  	// as if it were generated into the testing directory tree
   847  	// (it's actually in a temporary directory outside any Go tree).
   848  	// This cleans up a former kludge in passing functionality to the testing package.
   849  	if strings.HasPrefix(p.ImportPath, "testing/internal") && len(*stk) >= 2 && (*stk)[len(*stk)-2] == "testmain" {
   850  		return p
   851  	}
   852  
   853  	// We can't check standard packages with gccgo.
   854  	if cfg.BuildContext.Compiler == "gccgo" && p.Standard {
   855  		return p
   856  	}
   857  
   858  	// The stack includes p.ImportPath.
   859  	// If that's the only thing on the stack, we started
   860  	// with a name given on the command line, not an
   861  	// import. Anything listed on the command line is fine.
   862  	if len(*stk) == 1 {
   863  		return p
   864  	}
   865  
   866  	// Check for "internal" element: three cases depending on begin of string and/or end of string.
   867  	i, ok := findInternal(p.ImportPath)
   868  	if !ok {
   869  		return p
   870  	}
   871  
   872  	// Internal is present.
   873  	// Map import path back to directory corresponding to parent of internal.
   874  	if i > 0 {
   875  		i-- // rewind over slash in ".../internal"
   876  	}
   877  	parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
   878  	if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   879  		return p
   880  	}
   881  
   882  	// Look for symlinks before reporting error.
   883  	srcDir = expandPath(srcDir)
   884  	parent = expandPath(parent)
   885  	if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   886  		return p
   887  	}
   888  
   889  	// Internal is present, and srcDir is outside parent's tree. Not allowed.
   890  	perr := *p
   891  	perr.Error = &PackageError{
   892  		ImportStack: stk.Copy(),
   893  		Err:         "use of internal package not allowed",
   894  	}
   895  	perr.Incomplete = true
   896  	return &perr
   897  }
   898  
   899  // findInternal looks for the final "internal" path element in the given import path.
   900  // If there isn't one, findInternal returns ok=false.
   901  // Otherwise, findInternal returns ok=true and the index of the "internal".
   902  func findInternal(path string) (index int, ok bool) {
   903  	// Three cases, depending on internal at start/end of string or not.
   904  	// The order matters: we must return the index of the final element,
   905  	// because the final one produces the most restrictive requirement
   906  	// on the importer.
   907  	switch {
   908  	case strings.HasSuffix(path, "/internal"):
   909  		return len(path) - len("internal"), true
   910  	case strings.Contains(path, "/internal/"):
   911  		return strings.LastIndex(path, "/internal/") + 1, true
   912  	case path == "internal", strings.HasPrefix(path, "internal/"):
   913  		return 0, true
   914  	}
   915  	return 0, false
   916  }
   917  
   918  // disallowVendor checks that srcDir is allowed to import p as path.
   919  // If the import is allowed, disallowVendor returns the original package p.
   920  // If not, it returns a new package containing just an appropriate error.
   921  func disallowVendor(srcDir, path string, p *Package, stk *ImportStack) *Package {
   922  	// The stack includes p.ImportPath.
   923  	// If that's the only thing on the stack, we started
   924  	// with a name given on the command line, not an
   925  	// import. Anything listed on the command line is fine.
   926  	if len(*stk) == 1 {
   927  		return p
   928  	}
   929  
   930  	if perr := disallowVendorVisibility(srcDir, p, stk); perr != p {
   931  		return perr
   932  	}
   933  
   934  	// Paths like x/vendor/y must be imported as y, never as x/vendor/y.
   935  	if i, ok := FindVendor(path); ok {
   936  		perr := *p
   937  		perr.Error = &PackageError{
   938  			ImportStack: stk.Copy(),
   939  			Err:         "must be imported as " + path[i+len("vendor/"):],
   940  		}
   941  		perr.Incomplete = true
   942  		return &perr
   943  	}
   944  
   945  	return p
   946  }
   947  
   948  // disallowVendorVisibility checks that srcDir is allowed to import p.
   949  // The rules are the same as for /internal/ except that a path ending in /vendor
   950  // is not subject to the rules, only subdirectories of vendor.
   951  // This allows people to have packages and commands named vendor,
   952  // for maximal compatibility with existing source trees.
   953  func disallowVendorVisibility(srcDir string, p *Package, stk *ImportStack) *Package {
   954  	// The stack includes p.ImportPath.
   955  	// If that's the only thing on the stack, we started
   956  	// with a name given on the command line, not an
   957  	// import. Anything listed on the command line is fine.
   958  	if len(*stk) == 1 {
   959  		return p
   960  	}
   961  
   962  	// Check for "vendor" element.
   963  	i, ok := FindVendor(p.ImportPath)
   964  	if !ok {
   965  		return p
   966  	}
   967  
   968  	// Vendor is present.
   969  	// Map import path back to directory corresponding to parent of vendor.
   970  	if i > 0 {
   971  		i-- // rewind over slash in ".../vendor"
   972  	}
   973  	truncateTo := i + len(p.Dir) - len(p.ImportPath)
   974  	if truncateTo < 0 || len(p.Dir) < truncateTo {
   975  		return p
   976  	}
   977  	parent := p.Dir[:truncateTo]
   978  	if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   979  		return p
   980  	}
   981  
   982  	// Look for symlinks before reporting error.
   983  	srcDir = expandPath(srcDir)
   984  	parent = expandPath(parent)
   985  	if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   986  		return p
   987  	}
   988  
   989  	// Vendor is present, and srcDir is outside parent's tree. Not allowed.
   990  	perr := *p
   991  	perr.Error = &PackageError{
   992  		ImportStack: stk.Copy(),
   993  		Err:         "use of vendored package not allowed",
   994  	}
   995  	perr.Incomplete = true
   996  	return &perr
   997  }
   998  
   999  // FindVendor looks for the last non-terminating "vendor" path element in the given import path.
  1000  // If there isn't one, FindVendor returns ok=false.
  1001  // Otherwise, FindVendor returns ok=true and the index of the "vendor".
  1002  //
  1003  // Note that terminating "vendor" elements don't count: "x/vendor" is its own package,
  1004  // not the vendored copy of an import "" (the empty import path).
  1005  // This will allow people to have packages or commands named vendor.
  1006  // This may help reduce breakage, or it may just be confusing. We'll see.
  1007  func FindVendor(path string) (index int, ok bool) {
  1008  	// Two cases, depending on internal at start of string or not.
  1009  	// The order matters: we must return the index of the final element,
  1010  	// because the final one is where the effective import path starts.
  1011  	switch {
  1012  	case strings.Contains(path, "/vendor/"):
  1013  		return strings.LastIndex(path, "/vendor/") + 1, true
  1014  	case strings.HasPrefix(path, "vendor/"):
  1015  		return 0, true
  1016  	}
  1017  	return 0, false
  1018  }
  1019  
  1020  type TargetDir int
  1021  
  1022  const (
  1023  	ToTool    TargetDir = iota // to GOROOT/pkg/tool (default for cmd/*)
  1024  	ToBin                      // to bin dir inside package root (default for non-cmd/*)
  1025  	StalePath                  // an old import path; fail to build
  1026  )
  1027  
  1028  // InstallTargetDir reports the target directory for installing the command p.
  1029  func InstallTargetDir(p *Package) TargetDir {
  1030  	if strings.HasPrefix(p.ImportPath, "code.google.com/p/go.tools/cmd/") {
  1031  		return StalePath
  1032  	}
  1033  	if p.Goroot && strings.HasPrefix(p.ImportPath, "cmd/") && p.Name == "main" {
  1034  		switch p.ImportPath {
  1035  		case "cmd/go", "cmd/gofmt":
  1036  			return ToBin
  1037  		}
  1038  		return ToTool
  1039  	}
  1040  	return ToBin
  1041  }
  1042  
  1043  var cgoExclude = map[string]bool{
  1044  	"runtime/cgo": true,
  1045  }
  1046  
  1047  var cgoSyscallExclude = map[string]bool{
  1048  	"runtime/cgo":  true,
  1049  	"runtime/race": true,
  1050  	"runtime/msan": true,
  1051  }
  1052  
  1053  var foldPath = make(map[string]string)
  1054  
  1055  // load populates p using information from bp, err, which should
  1056  // be the result of calling build.Context.Import.
  1057  func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
  1058  	p.copyBuild(bp)
  1059  
  1060  	// Decide whether p was listed on the command line.
  1061  	// Given that load is called while processing the command line,
  1062  	// you might think we could simply pass a flag down into load
  1063  	// saying whether we are loading something named on the command
  1064  	// line or something to satisfy an import. But the first load of a
  1065  	// package named on the command line may be as a dependency
  1066  	// of an earlier package named on the command line, not when we
  1067  	// get to that package during command line processing.
  1068  	// For example "go test fmt reflect" will load reflect as a dependency
  1069  	// of fmt before it attempts to load as a command-line argument.
  1070  	// Because loads are cached, the later load will be a no-op,
  1071  	// so it is important that the first load can fill in CmdlinePkg correctly.
  1072  	// Hence the call to an explicit matching check here.
  1073  	p.Internal.CmdlinePkg = isCmdlinePkg(p)
  1074  
  1075  	p.Internal.Asmflags = BuildAsmflags.For(p)
  1076  	p.Internal.Gcflags = BuildGcflags.For(p)
  1077  	p.Internal.Ldflags = BuildLdflags.For(p)
  1078  	p.Internal.Gccgoflags = BuildGccgoflags.For(p)
  1079  
  1080  	// The localPrefix is the path we interpret ./ imports relative to.
  1081  	// Synthesized main packages sometimes override this.
  1082  	if p.Internal.Local {
  1083  		p.Internal.LocalPrefix = dirToImportPath(p.Dir)
  1084  	}
  1085  
  1086  	if err != nil {
  1087  		if _, ok := err.(*build.NoGoError); ok {
  1088  			err = &NoGoError{Package: p}
  1089  		}
  1090  		p.Incomplete = true
  1091  		err = base.ExpandScanner(err)
  1092  		p.Error = &PackageError{
  1093  			ImportStack: stk.Copy(),
  1094  			Err:         err.Error(),
  1095  		}
  1096  		return
  1097  	}
  1098  
  1099  	useBindir := p.Name == "main"
  1100  	if !p.Standard {
  1101  		switch cfg.BuildBuildmode {
  1102  		case "c-archive", "c-shared", "plugin":
  1103  			useBindir = false
  1104  		}
  1105  	}
  1106  
  1107  	if useBindir {
  1108  		// Report an error when the old code.google.com/p/go.tools paths are used.
  1109  		if InstallTargetDir(p) == StalePath {
  1110  			newPath := strings.Replace(p.ImportPath, "code.google.com/p/go.", "golang.org/x/", 1)
  1111  			e := fmt.Sprintf("the %v command has moved; use %v instead.", p.ImportPath, newPath)
  1112  			p.Error = &PackageError{Err: e}
  1113  			return
  1114  		}
  1115  		_, elem := filepath.Split(p.Dir)
  1116  		full := cfg.BuildContext.GOOS + "_" + cfg.BuildContext.GOARCH + "/" + elem
  1117  		if cfg.BuildContext.GOOS != base.ToolGOOS || cfg.BuildContext.GOARCH != base.ToolGOARCH {
  1118  			// Install cross-compiled binaries to subdirectories of bin.
  1119  			elem = full
  1120  		}
  1121  		if p.Internal.Build.BinDir != "" {
  1122  			// Install to GOBIN or bin of GOPATH entry.
  1123  			p.Target = filepath.Join(p.Internal.Build.BinDir, elem)
  1124  			if !p.Goroot && strings.Contains(elem, "/") && cfg.GOBIN != "" {
  1125  				// Do not create $GOBIN/goos_goarch/elem.
  1126  				p.Target = ""
  1127  				p.Internal.GobinSubdir = true
  1128  			}
  1129  		}
  1130  		if InstallTargetDir(p) == ToTool {
  1131  			// This is for 'go tool'.
  1132  			// Override all the usual logic and force it into the tool directory.
  1133  			p.Target = filepath.Join(cfg.GOROOTpkg, "tool", full)
  1134  		}
  1135  		if p.Target != "" && cfg.BuildContext.GOOS == "windows" {
  1136  			p.Target += ".exe"
  1137  		}
  1138  	} else if p.Internal.Local {
  1139  		// Local import turned into absolute path.
  1140  		// No permanent install target.
  1141  		p.Target = ""
  1142  	} else {
  1143  		p.Target = p.Internal.Build.PkgObj
  1144  		if cfg.BuildLinkshared {
  1145  			shlibnamefile := p.Target[:len(p.Target)-2] + ".shlibname"
  1146  			shlib, err := ioutil.ReadFile(shlibnamefile)
  1147  			if err != nil && !os.IsNotExist(err) {
  1148  				base.Fatalf("reading shlibname: %v", err)
  1149  			}
  1150  			if err == nil {
  1151  				libname := strings.TrimSpace(string(shlib))
  1152  				if cfg.BuildContext.Compiler == "gccgo" {
  1153  					p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, "shlibs", libname)
  1154  				} else {
  1155  					p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, libname)
  1156  				}
  1157  			}
  1158  		}
  1159  	}
  1160  
  1161  	// Build augmented import list to add implicit dependencies.
  1162  	// Be careful not to add imports twice, just to avoid confusion.
  1163  	importPaths := p.Imports
  1164  	addImport := func(path string) {
  1165  		for _, p := range importPaths {
  1166  			if path == p {
  1167  				return
  1168  			}
  1169  		}
  1170  		importPaths = append(importPaths, path)
  1171  	}
  1172  
  1173  	// Cgo translation adds imports of "runtime/cgo" and "syscall",
  1174  	// except for certain packages, to avoid circular dependencies.
  1175  	if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) {
  1176  		addImport("runtime/cgo")
  1177  	}
  1178  	if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
  1179  		addImport("syscall")
  1180  	}
  1181  
  1182  	// SWIG adds imports of some standard packages.
  1183  	if p.UsesSwig() {
  1184  		addImport("runtime/cgo")
  1185  		addImport("syscall")
  1186  		addImport("sync")
  1187  
  1188  		// TODO: The .swig and .swigcxx files can use
  1189  		// %go_import directives to import other packages.
  1190  	}
  1191  
  1192  	// The linker loads implicit dependencies.
  1193  	if p.Name == "main" && !p.Internal.ForceLibrary {
  1194  		for _, dep := range LinkerDeps(p) {
  1195  			addImport(dep)
  1196  		}
  1197  	}
  1198  
  1199  	// Check for case-insensitive collision of input files.
  1200  	// To avoid problems on case-insensitive files, we reject any package
  1201  	// where two different input files have equal names under a case-insensitive
  1202  	// comparison.
  1203  	inputs := p.AllFiles()
  1204  	f1, f2 := str.FoldDup(inputs)
  1205  	if f1 != "" {
  1206  		p.Error = &PackageError{
  1207  			ImportStack: stk.Copy(),
  1208  			Err:         fmt.Sprintf("case-insensitive file name collision: %q and %q", f1, f2),
  1209  		}
  1210  		return
  1211  	}
  1212  
  1213  	// If first letter of input file is ASCII, it must be alphanumeric.
  1214  	// This avoids files turning into flags when invoking commands,
  1215  	// and other problems we haven't thought of yet.
  1216  	// Also, _cgo_ files must be generated by us, not supplied.
  1217  	// They are allowed to have //go:cgo_ldflag directives.
  1218  	// The directory scan ignores files beginning with _,
  1219  	// so we shouldn't see any _cgo_ files anyway, but just be safe.
  1220  	for _, file := range inputs {
  1221  		if !SafeArg(file) || strings.HasPrefix(file, "_cgo_") {
  1222  			p.Error = &PackageError{
  1223  				ImportStack: stk.Copy(),
  1224  				Err:         fmt.Sprintf("invalid input file name %q", file),
  1225  			}
  1226  			return
  1227  		}
  1228  	}
  1229  	if name := pathpkg.Base(p.ImportPath); !SafeArg(name) {
  1230  		p.Error = &PackageError{
  1231  			ImportStack: stk.Copy(),
  1232  			Err:         fmt.Sprintf("invalid input directory name %q", name),
  1233  		}
  1234  		return
  1235  	}
  1236  	if !SafeArg(p.ImportPath) {
  1237  		p.Error = &PackageError{
  1238  			ImportStack: stk.Copy(),
  1239  			Err:         fmt.Sprintf("invalid import path %q", p.ImportPath),
  1240  		}
  1241  		return
  1242  	}
  1243  
  1244  	// Build list of imported packages and full dependency list.
  1245  	imports := make([]*Package, 0, len(p.Imports))
  1246  	for i, path := range importPaths {
  1247  		if path == "C" {
  1248  			continue
  1249  		}
  1250  		p1 := LoadImport(path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport)
  1251  		if p.Standard && p.Error == nil && !p1.Standard && p1.Error == nil {
  1252  			p.Error = &PackageError{
  1253  				ImportStack: stk.Copy(),
  1254  				Err:         fmt.Sprintf("non-standard import %q in standard package %q", path, p.ImportPath),
  1255  			}
  1256  			pos := p.Internal.Build.ImportPos[path]
  1257  			if len(pos) > 0 {
  1258  				p.Error.Pos = pos[0].String()
  1259  			}
  1260  		}
  1261  
  1262  		path = p1.ImportPath
  1263  		importPaths[i] = path
  1264  		if i < len(p.Imports) {
  1265  			p.Imports[i] = path
  1266  		}
  1267  
  1268  		imports = append(imports, p1)
  1269  		if p1.Incomplete {
  1270  			p.Incomplete = true
  1271  		}
  1272  	}
  1273  	p.Internal.Imports = imports
  1274  
  1275  	deps := make(map[string]*Package)
  1276  	var q []*Package
  1277  	q = append(q, imports...)
  1278  	for i := 0; i < len(q); i++ {
  1279  		p1 := q[i]
  1280  		path := p1.ImportPath
  1281  		// The same import path could produce an error or not,
  1282  		// depending on what tries to import it.
  1283  		// Prefer to record entries with errors, so we can report them.
  1284  		p0 := deps[path]
  1285  		if p0 == nil || p1.Error != nil && (p0.Error == nil || len(p0.Error.ImportStack) > len(p1.Error.ImportStack)) {
  1286  			deps[path] = p1
  1287  			for _, p2 := range p1.Internal.Imports {
  1288  				if deps[p2.ImportPath] != p2 {
  1289  					q = append(q, p2)
  1290  				}
  1291  			}
  1292  		}
  1293  	}
  1294  
  1295  	p.Deps = make([]string, 0, len(deps))
  1296  	for dep := range deps {
  1297  		p.Deps = append(p.Deps, dep)
  1298  	}
  1299  	sort.Strings(p.Deps)
  1300  	for _, dep := range p.Deps {
  1301  		p1 := deps[dep]
  1302  		if p1 == nil {
  1303  			panic("impossible: missing entry in package cache for " + dep + " imported by " + p.ImportPath)
  1304  		}
  1305  		if p1.Error != nil {
  1306  			p.DepsErrors = append(p.DepsErrors, p1.Error)
  1307  		}
  1308  	}
  1309  
  1310  	// unsafe is a fake package.
  1311  	if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
  1312  		p.Target = ""
  1313  	}
  1314  
  1315  	// If cgo is not enabled, ignore cgo supporting sources
  1316  	// just as we ignore go files containing import "C".
  1317  	if !cfg.BuildContext.CgoEnabled {
  1318  		p.CFiles = nil
  1319  		p.CXXFiles = nil
  1320  		p.MFiles = nil
  1321  		p.SwigFiles = nil
  1322  		p.SwigCXXFiles = nil
  1323  		// Note that SFiles are okay (they go to the Go assembler)
  1324  		// and HFiles are okay (they might be used by the SFiles).
  1325  		// Also Sysofiles are okay (they might not contain object
  1326  		// code; see issue #16050).
  1327  	}
  1328  
  1329  	setError := func(msg string) {
  1330  		p.Error = &PackageError{
  1331  			ImportStack: stk.Copy(),
  1332  			Err:         msg,
  1333  		}
  1334  	}
  1335  
  1336  	// The gc toolchain only permits C source files with cgo or SWIG.
  1337  	if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" {
  1338  		setError(fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")))
  1339  		return
  1340  	}
  1341  
  1342  	// C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,
  1343  	// regardless of toolchain.
  1344  	if len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
  1345  		setError(fmt.Sprintf("C++ source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CXXFiles, " ")))
  1346  		return
  1347  	}
  1348  	if len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
  1349  		setError(fmt.Sprintf("Objective-C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.MFiles, " ")))
  1350  		return
  1351  	}
  1352  	if len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
  1353  		setError(fmt.Sprintf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
  1354  		return
  1355  	}
  1356  
  1357  	// Check for case-insensitive collisions of import paths.
  1358  	fold := str.ToFold(p.ImportPath)
  1359  	if other := foldPath[fold]; other == "" {
  1360  		foldPath[fold] = p.ImportPath
  1361  	} else if other != p.ImportPath {
  1362  		setError(fmt.Sprintf("case-insensitive import collision: %q and %q", p.ImportPath, other))
  1363  		return
  1364  	}
  1365  }
  1366  
  1367  // SafeArg reports whether arg is a "safe" command-line argument,
  1368  // meaning that when it appears in a command-line, it probably
  1369  // doesn't have some special meaning other than its own name.
  1370  // Obviously args beginning with - are not safe (they look like flags).
  1371  // Less obviously, args beginning with @ are not safe (they look like
  1372  // GNU binutils flagfile specifiers, sometimes called "response files").
  1373  // To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.
  1374  // We accept leading . _ and / as likely in file system paths.
  1375  // There is a copy of this function in cmd/compile/internal/gc/noder.go.
  1376  func SafeArg(name string) bool {
  1377  	if name == "" {
  1378  		return false
  1379  	}
  1380  	c := name[0]
  1381  	return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
  1382  }
  1383  
  1384  // LinkerDeps returns the list of linker-induced dependencies for main package p.
  1385  func LinkerDeps(p *Package) []string {
  1386  	// Everything links runtime.
  1387  	deps := []string{"runtime"}
  1388  
  1389  	// External linking mode forces an import of runtime/cgo.
  1390  	if externalLinkingForced(p) {
  1391  		deps = append(deps, "runtime/cgo")
  1392  	}
  1393  	// On ARM with GOARM=5, it forces an import of math, for soft floating point.
  1394  	if cfg.Goarch == "arm" {
  1395  		deps = append(deps, "math")
  1396  	}
  1397  	// Using the race detector forces an import of runtime/race.
  1398  	if cfg.BuildRace {
  1399  		deps = append(deps, "runtime/race")
  1400  	}
  1401  	// Using memory sanitizer forces an import of runtime/msan.
  1402  	if cfg.BuildMSan {
  1403  		deps = append(deps, "runtime/msan")
  1404  	}
  1405  
  1406  	return deps
  1407  }
  1408  
  1409  // externalLinkingForced reports whether external linking is being
  1410  // forced even for programs that do not use cgo.
  1411  func externalLinkingForced(p *Package) bool {
  1412  	// Some targets must use external linking even inside GOROOT.
  1413  	switch cfg.BuildContext.GOOS {
  1414  	case "android":
  1415  		return true
  1416  	case "darwin":
  1417  		switch cfg.BuildContext.GOARCH {
  1418  		case "arm", "arm64":
  1419  			return true
  1420  		}
  1421  	}
  1422  
  1423  	if !cfg.BuildContext.CgoEnabled {
  1424  		return false
  1425  	}
  1426  	// Currently build modes c-shared, pie (on systems that do not
  1427  	// support PIE with internal linking mode (currently all
  1428  	// systems: issue #18968)), plugin, and -linkshared force
  1429  	// external linking mode, as of course does
  1430  	// -ldflags=-linkmode=external. External linking mode forces
  1431  	// an import of runtime/cgo.
  1432  	pieCgo := cfg.BuildBuildmode == "pie"
  1433  	linkmodeExternal := false
  1434  	if p != nil {
  1435  		ldflags := BuildLdflags.For(p)
  1436  		for i, a := range ldflags {
  1437  			if a == "-linkmode=external" {
  1438  				linkmodeExternal = true
  1439  			}
  1440  			if a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "external" {
  1441  				linkmodeExternal = true
  1442  			}
  1443  		}
  1444  	}
  1445  
  1446  	return cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal
  1447  }
  1448  
  1449  // mkAbs rewrites list, which must be paths relative to p.Dir,
  1450  // into a sorted list of absolute paths. It edits list in place but for
  1451  // convenience also returns list back to its caller.
  1452  func (p *Package) mkAbs(list []string) []string {
  1453  	for i, f := range list {
  1454  		list[i] = filepath.Join(p.Dir, f)
  1455  	}
  1456  	sort.Strings(list)
  1457  	return list
  1458  }
  1459  
  1460  // InternalGoFiles returns the list of Go files being built for the package,
  1461  // using absolute paths.
  1462  func (p *Package) InternalGoFiles() []string {
  1463  	return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
  1464  }
  1465  
  1466  // InternalGoFiles returns the list of all Go files possibly relevant for the package,
  1467  // using absolute paths. "Possibly relevant" means that files are not excluded
  1468  // due to build tags, but files with names beginning with . or _ are still excluded.
  1469  func (p *Package) InternalAllGoFiles() []string {
  1470  	var extra []string
  1471  	for _, f := range p.IgnoredGoFiles {
  1472  		if f != "" && f[0] != '.' || f[0] != '_' {
  1473  			extra = append(extra, f)
  1474  		}
  1475  	}
  1476  	return p.mkAbs(str.StringList(extra, p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
  1477  }
  1478  
  1479  // usesSwig reports whether the package needs to run SWIG.
  1480  func (p *Package) UsesSwig() bool {
  1481  	return len(p.SwigFiles) > 0 || len(p.SwigCXXFiles) > 0
  1482  }
  1483  
  1484  // usesCgo reports whether the package needs to run cgo
  1485  func (p *Package) UsesCgo() bool {
  1486  	return len(p.CgoFiles) > 0
  1487  }
  1488  
  1489  // packageList returns the list of packages in the dag rooted at roots
  1490  // as visited in a depth-first post-order traversal.
  1491  func PackageList(roots []*Package) []*Package {
  1492  	seen := map[*Package]bool{}
  1493  	all := []*Package{}
  1494  	var walk func(*Package)
  1495  	walk = func(p *Package) {
  1496  		if seen[p] {
  1497  			return
  1498  		}
  1499  		seen[p] = true
  1500  		for _, p1 := range p.Internal.Imports {
  1501  			walk(p1)
  1502  		}
  1503  		all = append(all, p)
  1504  	}
  1505  	for _, root := range roots {
  1506  		walk(root)
  1507  	}
  1508  	return all
  1509  }
  1510  
  1511  var cmdCache = map[string]*Package{}
  1512  
  1513  func ClearCmdCache() {
  1514  	for name := range cmdCache {
  1515  		delete(cmdCache, name)
  1516  	}
  1517  }
  1518  
  1519  // loadPackage is like loadImport but is used for command-line arguments,
  1520  // not for paths found in import statements. In addition to ordinary import paths,
  1521  // loadPackage accepts pseudo-paths beginning with cmd/ to denote commands
  1522  // in the Go command directory, as well as paths to those directories.
  1523  func LoadPackage(arg string, stk *ImportStack) *Package {
  1524  	if build.IsLocalImport(arg) {
  1525  		dir := arg
  1526  		if !filepath.IsAbs(dir) {
  1527  			if abs, err := filepath.Abs(dir); err == nil {
  1528  				// interpret relative to current directory
  1529  				dir = abs
  1530  			}
  1531  		}
  1532  		if sub, ok := hasSubdir(cfg.GOROOTsrc, dir); ok && strings.HasPrefix(sub, "cmd/") && !strings.Contains(sub[4:], "/") {
  1533  			arg = sub
  1534  		}
  1535  	}
  1536  	if strings.HasPrefix(arg, "cmd/") && !strings.Contains(arg[4:], "/") {
  1537  		if p := cmdCache[arg]; p != nil {
  1538  			return p
  1539  		}
  1540  		stk.Push(arg)
  1541  		defer stk.Pop()
  1542  
  1543  		bp, err := cfg.BuildContext.ImportDir(filepath.Join(cfg.GOROOTsrc, arg), 0)
  1544  		bp.ImportPath = arg
  1545  		bp.Goroot = true
  1546  		bp.BinDir = cfg.GOROOTbin
  1547  		if cfg.GOROOTbin != "" {
  1548  			bp.BinDir = cfg.GOROOTbin
  1549  		}
  1550  		bp.Root = cfg.GOROOT
  1551  		bp.SrcRoot = cfg.GOROOTsrc
  1552  		p := new(Package)
  1553  		cmdCache[arg] = p
  1554  		p.load(stk, bp, err)
  1555  		if p.Error == nil && p.Name != "main" {
  1556  			p.Error = &PackageError{
  1557  				ImportStack: stk.Copy(),
  1558  				Err:         fmt.Sprintf("expected package main but found package %s in %s", p.Name, p.Dir),
  1559  			}
  1560  		}
  1561  		return p
  1562  	}
  1563  
  1564  	// Wasn't a command; must be a package.
  1565  	// If it is a local import path but names a standard package,
  1566  	// we treat it as if the user specified the standard package.
  1567  	// This lets you run go test ./ioutil in package io and be
  1568  	// referring to io/ioutil rather than a hypothetical import of
  1569  	// "./ioutil".
  1570  	if build.IsLocalImport(arg) {
  1571  		bp, _ := cfg.BuildContext.ImportDir(filepath.Join(base.Cwd, arg), build.FindOnly)
  1572  		if bp.ImportPath != "" && bp.ImportPath != "." {
  1573  			arg = bp.ImportPath
  1574  		}
  1575  	}
  1576  
  1577  	return LoadImport(arg, base.Cwd, nil, stk, nil, 0)
  1578  }
  1579  
  1580  // packages returns the packages named by the
  1581  // command line arguments 'args'. If a named package
  1582  // cannot be loaded at all (for example, if the directory does not exist),
  1583  // then packages prints an error and does not include that
  1584  // package in the results. However, if errors occur trying
  1585  // to load dependencies of a named package, the named
  1586  // package is still returned, with p.Incomplete = true
  1587  // and details in p.DepsErrors.
  1588  func Packages(args []string) []*Package {
  1589  	var pkgs []*Package
  1590  	for _, pkg := range PackagesAndErrors(args) {
  1591  		if pkg.Error != nil {
  1592  			base.Errorf("can't load package: %s", pkg.Error)
  1593  			continue
  1594  		}
  1595  		pkgs = append(pkgs, pkg)
  1596  	}
  1597  	return pkgs
  1598  }
  1599  
  1600  // packagesAndErrors is like 'packages' but returns a
  1601  // *Package for every argument, even the ones that
  1602  // cannot be loaded at all.
  1603  // The packages that fail to load will have p.Error != nil.
  1604  func PackagesAndErrors(args []string) []*Package {
  1605  	if len(args) > 0 && strings.HasSuffix(args[0], ".go") {
  1606  		return []*Package{GoFilesPackage(args)}
  1607  	}
  1608  
  1609  	args = ImportPaths(args)
  1610  	var (
  1611  		pkgs    []*Package
  1612  		stk     ImportStack
  1613  		seenArg = make(map[string]bool)
  1614  		seenPkg = make(map[*Package]bool)
  1615  	)
  1616  
  1617  	for _, arg := range args {
  1618  		if seenArg[arg] {
  1619  			continue
  1620  		}
  1621  		seenArg[arg] = true
  1622  		pkg := LoadPackage(arg, &stk)
  1623  		if seenPkg[pkg] {
  1624  			continue
  1625  		}
  1626  		seenPkg[pkg] = true
  1627  		pkgs = append(pkgs, pkg)
  1628  	}
  1629  
  1630  	return pkgs
  1631  }
  1632  
  1633  // packagesForBuild is like 'packages' but fails if any of
  1634  // the packages or their dependencies have errors
  1635  // (cannot be built).
  1636  func PackagesForBuild(args []string) []*Package {
  1637  	pkgs := PackagesAndErrors(args)
  1638  	printed := map[*PackageError]bool{}
  1639  	for _, pkg := range pkgs {
  1640  		if pkg.Error != nil {
  1641  			base.Errorf("can't load package: %s", pkg.Error)
  1642  		}
  1643  		for _, err := range pkg.DepsErrors {
  1644  			// Since these are errors in dependencies,
  1645  			// the same error might show up multiple times,
  1646  			// once in each package that depends on it.
  1647  			// Only print each once.
  1648  			if !printed[err] {
  1649  				printed[err] = true
  1650  				base.Errorf("%s", err)
  1651  			}
  1652  		}
  1653  	}
  1654  	base.ExitIfErrors()
  1655  
  1656  	// Check for duplicate loads of the same package.
  1657  	// That should be impossible, but if it does happen then
  1658  	// we end up trying to build the same package twice,
  1659  	// usually in parallel overwriting the same files,
  1660  	// which doesn't work very well.
  1661  	seen := map[string]bool{}
  1662  	reported := map[string]bool{}
  1663  	for _, pkg := range PackageList(pkgs) {
  1664  		if seen[pkg.ImportPath] && !reported[pkg.ImportPath] {
  1665  			reported[pkg.ImportPath] = true
  1666  			base.Errorf("internal error: duplicate loads of %s", pkg.ImportPath)
  1667  		}
  1668  		seen[pkg.ImportPath] = true
  1669  	}
  1670  	base.ExitIfErrors()
  1671  
  1672  	return pkgs
  1673  }
  1674  
  1675  // GoFilesPackage creates a package for building a collection of Go files
  1676  // (typically named on the command line). The target is named p.a for
  1677  // package p or named after the first Go file for package main.
  1678  func GoFilesPackage(gofiles []string) *Package {
  1679  	// TODO: Remove this restriction.
  1680  	for _, f := range gofiles {
  1681  		if !strings.HasSuffix(f, ".go") {
  1682  			base.Fatalf("named files must be .go files")
  1683  		}
  1684  	}
  1685  
  1686  	var stk ImportStack
  1687  	ctxt := cfg.BuildContext
  1688  	ctxt.UseAllFiles = true
  1689  
  1690  	// Synthesize fake "directory" that only shows the named files,
  1691  	// to make it look like this is a standard package or
  1692  	// command directory. So that local imports resolve
  1693  	// consistently, the files must all be in the same directory.
  1694  	var dirent []os.FileInfo
  1695  	var dir string
  1696  	for _, file := range gofiles {
  1697  		fi, err := os.Stat(file)
  1698  		if err != nil {
  1699  			base.Fatalf("%s", err)
  1700  		}
  1701  		if fi.IsDir() {
  1702  			base.Fatalf("%s is a directory, should be a Go file", file)
  1703  		}
  1704  		dir1, _ := filepath.Split(file)
  1705  		if dir1 == "" {
  1706  			dir1 = "./"
  1707  		}
  1708  		if dir == "" {
  1709  			dir = dir1
  1710  		} else if dir != dir1 {
  1711  			base.Fatalf("named files must all be in one directory; have %s and %s", dir, dir1)
  1712  		}
  1713  		dirent = append(dirent, fi)
  1714  	}
  1715  	ctxt.ReadDir = func(string) ([]os.FileInfo, error) { return dirent, nil }
  1716  
  1717  	var err error
  1718  	if dir == "" {
  1719  		dir = base.Cwd
  1720  	}
  1721  	dir, err = filepath.Abs(dir)
  1722  	if err != nil {
  1723  		base.Fatalf("%s", err)
  1724  	}
  1725  
  1726  	bp, err := ctxt.ImportDir(dir, 0)
  1727  	pkg := new(Package)
  1728  	pkg.Internal.Local = true
  1729  	pkg.Internal.CmdlineFiles = true
  1730  	stk.Push("main")
  1731  	pkg.load(&stk, bp, err)
  1732  	stk.Pop()
  1733  	pkg.Internal.LocalPrefix = dirToImportPath(dir)
  1734  	pkg.ImportPath = "command-line-arguments"
  1735  	pkg.Target = ""
  1736  
  1737  	if pkg.Name == "main" {
  1738  		_, elem := filepath.Split(gofiles[0])
  1739  		exe := elem[:len(elem)-len(".go")] + cfg.ExeSuffix
  1740  		if cfg.BuildO == "" {
  1741  			cfg.BuildO = exe
  1742  		}
  1743  		if cfg.GOBIN != "" {
  1744  			pkg.Target = filepath.Join(cfg.GOBIN, exe)
  1745  		}
  1746  	}
  1747  
  1748  	return pkg
  1749  }
  1750  
  1751  // TestPackagesFor returns package structs ptest, the package p plus
  1752  // its test files, and pxtest, the external tests of package p.
  1753  // pxtest may be nil. If there are no test files, forceTest decides
  1754  // whether this returns a new package struct or just returns p.
  1755  func TestPackagesFor(p *Package, forceTest bool) (ptest, pxtest *Package, err error) {
  1756  	var imports, ximports []*Package
  1757  	var stk ImportStack
  1758  	stk.Push(p.ImportPath + " (test)")
  1759  	rawTestImports := str.StringList(p.TestImports)
  1760  	for i, path := range p.TestImports {
  1761  		p1 := LoadImport(path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], ResolveImport)
  1762  		if p1.Error != nil {
  1763  			return nil, nil, p1.Error
  1764  		}
  1765  		if len(p1.DepsErrors) > 0 {
  1766  			err := p1.DepsErrors[0]
  1767  			err.Pos = "" // show full import stack
  1768  			return nil, nil, err
  1769  		}
  1770  		if str.Contains(p1.Deps, p.ImportPath) || p1.ImportPath == p.ImportPath {
  1771  			// Same error that loadPackage returns (via reusePackage) in pkg.go.
  1772  			// Can't change that code, because that code is only for loading the
  1773  			// non-test copy of a package.
  1774  			err := &PackageError{
  1775  				ImportStack:   testImportStack(stk[0], p1, p.ImportPath),
  1776  				Err:           "import cycle not allowed in test",
  1777  				IsImportCycle: true,
  1778  			}
  1779  			return nil, nil, err
  1780  		}
  1781  		p.TestImports[i] = p1.ImportPath
  1782  		imports = append(imports, p1)
  1783  	}
  1784  	stk.Pop()
  1785  	stk.Push(p.ImportPath + "_test")
  1786  	pxtestNeedsPtest := false
  1787  	rawXTestImports := str.StringList(p.XTestImports)
  1788  	for i, path := range p.XTestImports {
  1789  		p1 := LoadImport(path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], ResolveImport)
  1790  		if p1.Error != nil {
  1791  			return nil, nil, p1.Error
  1792  		}
  1793  		if len(p1.DepsErrors) > 0 {
  1794  			err := p1.DepsErrors[0]
  1795  			err.Pos = "" // show full import stack
  1796  			return nil, nil, err
  1797  		}
  1798  		if p1.ImportPath == p.ImportPath {
  1799  			pxtestNeedsPtest = true
  1800  		} else {
  1801  			ximports = append(ximports, p1)
  1802  		}
  1803  		p.XTestImports[i] = p1.ImportPath
  1804  	}
  1805  	stk.Pop()
  1806  
  1807  	// Test package.
  1808  	if len(p.TestGoFiles) > 0 || forceTest {
  1809  		ptest = new(Package)
  1810  		*ptest = *p
  1811  		ptest.GoFiles = nil
  1812  		ptest.GoFiles = append(ptest.GoFiles, p.GoFiles...)
  1813  		ptest.GoFiles = append(ptest.GoFiles, p.TestGoFiles...)
  1814  		ptest.Target = ""
  1815  		// Note: The preparation of the vet config requires that common
  1816  		// indexes in ptest.Imports, ptest.Internal.Imports, and ptest.Internal.RawImports
  1817  		// all line up (but RawImports can be shorter than the others).
  1818  		// That is, for 0 ≤ i < len(RawImports),
  1819  		// RawImports[i] is the import string in the program text,
  1820  		// Imports[i] is the expanded import string (vendoring applied or relative path expanded away),
  1821  		// and Internal.Imports[i] is the corresponding *Package.
  1822  		// Any implicitly added imports appear in Imports and Internal.Imports
  1823  		// but not RawImports (because they were not in the source code).
  1824  		// We insert TestImports, imports, and rawTestImports at the start of
  1825  		// these lists to preserve the alignment.
  1826  		ptest.Imports = str.StringList(p.TestImports, p.Imports)
  1827  		ptest.Internal.Imports = append(imports, p.Internal.Imports...)
  1828  		ptest.Internal.RawImports = str.StringList(rawTestImports, p.Internal.RawImports)
  1829  		ptest.Internal.ForceLibrary = true
  1830  		ptest.Internal.Build = new(build.Package)
  1831  		*ptest.Internal.Build = *p.Internal.Build
  1832  		m := map[string][]token.Position{}
  1833  		for k, v := range p.Internal.Build.ImportPos {
  1834  			m[k] = append(m[k], v...)
  1835  		}
  1836  		for k, v := range p.Internal.Build.TestImportPos {
  1837  			m[k] = append(m[k], v...)
  1838  		}
  1839  		ptest.Internal.Build.ImportPos = m
  1840  	} else {
  1841  		ptest = p
  1842  	}
  1843  
  1844  	// External test package.
  1845  	if len(p.XTestGoFiles) > 0 {
  1846  		pxtest = &Package{
  1847  			PackagePublic: PackagePublic{
  1848  				Name:       p.Name + "_test",
  1849  				ImportPath: p.ImportPath + "_test",
  1850  				Root:       p.Root,
  1851  				Dir:        p.Dir,
  1852  				GoFiles:    p.XTestGoFiles,
  1853  				Imports:    p.XTestImports,
  1854  			},
  1855  			Internal: PackageInternal{
  1856  				LocalPrefix: p.Internal.LocalPrefix,
  1857  				Build: &build.Package{
  1858  					ImportPos: p.Internal.Build.XTestImportPos,
  1859  				},
  1860  				Imports:    ximports,
  1861  				RawImports: rawXTestImports,
  1862  
  1863  				Asmflags:   p.Internal.Asmflags,
  1864  				Gcflags:    p.Internal.Gcflags,
  1865  				Ldflags:    p.Internal.Ldflags,
  1866  				Gccgoflags: p.Internal.Gccgoflags,
  1867  			},
  1868  		}
  1869  		if pxtestNeedsPtest {
  1870  			pxtest.Internal.Imports = append(pxtest.Internal.Imports, ptest)
  1871  		}
  1872  	}
  1873  
  1874  	return ptest, pxtest, nil
  1875  }
  1876  
  1877  func testImportStack(top string, p *Package, target string) []string {
  1878  	stk := []string{top, p.ImportPath}
  1879  Search:
  1880  	for p.ImportPath != target {
  1881  		for _, p1 := range p.Internal.Imports {
  1882  			if p1.ImportPath == target || str.Contains(p1.Deps, target) {
  1883  				stk = append(stk, p1.ImportPath)
  1884  				p = p1
  1885  				continue Search
  1886  			}
  1887  		}
  1888  		// Can't happen, but in case it does...
  1889  		stk = append(stk, "<lost path to cycle>")
  1890  		break
  1891  	}
  1892  	return stk
  1893  }