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