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.
6package load
7
8import (
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
28var IgnoreImports bool // control whether we ignore imports in packages
29
30// A Package describes a single package found in a directory.
31type Package struct {
32	PackagePublic                 // visible in 'go list'
33	Internal      PackageInternal // for use inside go command only
34}
35
36type 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.
106func (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
125type 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
147type NoGoError struct {
148	Package *Package
149}
150
151func (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).
183func (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.
201type CoverVar struct {
202	File string // local file name
203	Var  string // name of count struct
204}
205
206func (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).
265func 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.
275type 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
283func (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.
300type ImportStack []string
301
302func (s *ImportStack) Push(p string) {
303	*s = append(*s, p)
304}
305
306func (s *ImportStack) Pop() {
307	*s = (*s)[0 : len(*s)-1]
308}
309
310func (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.
317func (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.
334var packageCache = map[string]*Package{}
335
336func ClearPackageCache() {
337	for name := range packageCache {
338		delete(packageCache, name)
339	}
340}
341
342func 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.
354func 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.
370func dirToImportPath(dir string) string {
371	return pathpkg.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
372}
373
374func 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).
384const (
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.
409func 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
506func 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
515func 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
524var isDirCache = map[string]bool{}
525
526func 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).
543func 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.
553func 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.
585func 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
634var (
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.
640func 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.
688func 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.
703func 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.
723func 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
751HaveGoMod:
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.
796func 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.
809func 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.
834func 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".
902func 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.
921func 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.
953func 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.
1007func 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
1020type TargetDir int
1021
1022const (
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.
1029func 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
1043var cgoExclude = map[string]bool{
1044	"runtime/cgo": true,
1045}
1046
1047var cgoSyscallExclude = map[string]bool{
1048	"runtime/cgo":  true,
1049	"runtime/race": true,
1050	"runtime/msan": true,
1051}
1052
1053var 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.
1057func (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			if cfg.BuildToolchainName == "gccgo" {
1134				p.Target = filepath.Join(base.ToolDir, elem)
1135			} else {
1136				p.Target = filepath.Join(cfg.GOROOTpkg, "tool", full)
1137			}
1138		}
1139		if p.Target != "" && cfg.BuildContext.GOOS == "windows" {
1140			p.Target += ".exe"
1141		}
1142	} else if p.Internal.Local {
1143		// Local import turned into absolute path.
1144		// No permanent install target.
1145		p.Target = ""
1146	} else {
1147		p.Target = p.Internal.Build.PkgObj
1148		if cfg.BuildLinkshared {
1149			shlibnamefile := p.Target[:len(p.Target)-2] + ".shlibname"
1150			shlib, err := ioutil.ReadFile(shlibnamefile)
1151			if err != nil && !os.IsNotExist(err) {
1152				base.Fatalf("reading shlibname: %v", err)
1153			}
1154			if err == nil {
1155				libname := strings.TrimSpace(string(shlib))
1156				if cfg.BuildContext.Compiler == "gccgo" {
1157					p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, "shlibs", libname)
1158				} else {
1159					p.Shlib = filepath.Join(p.Internal.Build.PkgTargetRoot, libname)
1160				}
1161			}
1162		}
1163	}
1164
1165	// Build augmented import list to add implicit dependencies.
1166	// Be careful not to add imports twice, just to avoid confusion.
1167	importPaths := p.Imports
1168	addImport := func(path string) {
1169		for _, p := range importPaths {
1170			if path == p {
1171				return
1172			}
1173		}
1174		importPaths = append(importPaths, path)
1175	}
1176
1177	// Cgo translation adds imports of "runtime/cgo" and "syscall",
1178	// except for certain packages, to avoid circular dependencies.
1179	if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
1180		addImport("runtime/cgo")
1181	}
1182	if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
1183		addImport("syscall")
1184	}
1185
1186	// SWIG adds imports of some standard packages.
1187	if p.UsesSwig() {
1188		if cfg.BuildContext.Compiler != "gccgo" {
1189			addImport("runtime/cgo")
1190		}
1191		addImport("syscall")
1192		addImport("sync")
1193
1194		// TODO: The .swig and .swigcxx files can use
1195		// %go_import directives to import other packages.
1196	}
1197
1198	// The linker loads implicit dependencies.
1199	if p.Name == "main" && !p.Internal.ForceLibrary {
1200		for _, dep := range LinkerDeps(p) {
1201			addImport(dep)
1202		}
1203	}
1204
1205	// Check for case-insensitive collision of input files.
1206	// To avoid problems on case-insensitive files, we reject any package
1207	// where two different input files have equal names under a case-insensitive
1208	// comparison.
1209	inputs := p.AllFiles()
1210	f1, f2 := str.FoldDup(inputs)
1211	if f1 != "" {
1212		p.Error = &PackageError{
1213			ImportStack: stk.Copy(),
1214			Err:         fmt.Sprintf("case-insensitive file name collision: %q and %q", f1, f2),
1215		}
1216		return
1217	}
1218
1219	// If first letter of input file is ASCII, it must be alphanumeric.
1220	// This avoids files turning into flags when invoking commands,
1221	// and other problems we haven't thought of yet.
1222	// Also, _cgo_ files must be generated by us, not supplied.
1223	// They are allowed to have //go:cgo_ldflag directives.
1224	// The directory scan ignores files beginning with _,
1225	// so we shouldn't see any _cgo_ files anyway, but just be safe.
1226	for _, file := range inputs {
1227		if !SafeArg(file) || strings.HasPrefix(file, "_cgo_") {
1228			p.Error = &PackageError{
1229				ImportStack: stk.Copy(),
1230				Err:         fmt.Sprintf("invalid input file name %q", file),
1231			}
1232			return
1233		}
1234	}
1235	if name := pathpkg.Base(p.ImportPath); !SafeArg(name) {
1236		p.Error = &PackageError{
1237			ImportStack: stk.Copy(),
1238			Err:         fmt.Sprintf("invalid input directory name %q", name),
1239		}
1240		return
1241	}
1242	if !SafeArg(p.ImportPath) {
1243		p.Error = &PackageError{
1244			ImportStack: stk.Copy(),
1245			Err:         fmt.Sprintf("invalid import path %q", p.ImportPath),
1246		}
1247		return
1248	}
1249
1250	// Build list of imported packages and full dependency list.
1251	imports := make([]*Package, 0, len(p.Imports))
1252	for i, path := range importPaths {
1253		if path == "C" {
1254			continue
1255		}
1256		p1 := LoadImport(path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport)
1257		if p.Standard && p.Error == nil && !p1.Standard && p1.Error == nil {
1258			p.Error = &PackageError{
1259				ImportStack: stk.Copy(),
1260				Err:         fmt.Sprintf("non-standard import %q in standard package %q", path, p.ImportPath),
1261			}
1262			pos := p.Internal.Build.ImportPos[path]
1263			if len(pos) > 0 {
1264				p.Error.Pos = pos[0].String()
1265			}
1266		}
1267
1268		path = p1.ImportPath
1269		importPaths[i] = path
1270		if i < len(p.Imports) {
1271			p.Imports[i] = path
1272		}
1273
1274		imports = append(imports, p1)
1275		if p1.Incomplete {
1276			p.Incomplete = true
1277		}
1278	}
1279	p.Internal.Imports = imports
1280
1281	deps := make(map[string]*Package)
1282	var q []*Package
1283	q = append(q, imports...)
1284	for i := 0; i < len(q); i++ {
1285		p1 := q[i]
1286		path := p1.ImportPath
1287		// The same import path could produce an error or not,
1288		// depending on what tries to import it.
1289		// Prefer to record entries with errors, so we can report them.
1290		p0 := deps[path]
1291		if p0 == nil || p1.Error != nil && (p0.Error == nil || len(p0.Error.ImportStack) > len(p1.Error.ImportStack)) {
1292			deps[path] = p1
1293			for _, p2 := range p1.Internal.Imports {
1294				if deps[p2.ImportPath] != p2 {
1295					q = append(q, p2)
1296				}
1297			}
1298		}
1299	}
1300
1301	p.Deps = make([]string, 0, len(deps))
1302	for dep := range deps {
1303		p.Deps = append(p.Deps, dep)
1304	}
1305	sort.Strings(p.Deps)
1306	for _, dep := range p.Deps {
1307		p1 := deps[dep]
1308		if p1 == nil {
1309			panic("impossible: missing entry in package cache for " + dep + " imported by " + p.ImportPath)
1310		}
1311		if p1.Error != nil {
1312			p.DepsErrors = append(p.DepsErrors, p1.Error)
1313		}
1314	}
1315
1316	// unsafe is a fake package.
1317	if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
1318		p.Target = ""
1319	}
1320
1321	// If cgo is not enabled, ignore cgo supporting sources
1322	// just as we ignore go files containing import "C".
1323	if !cfg.BuildContext.CgoEnabled {
1324		p.CFiles = nil
1325		p.CXXFiles = nil
1326		p.MFiles = nil
1327		p.SwigFiles = nil
1328		p.SwigCXXFiles = nil
1329		// Note that SFiles are okay (they go to the Go assembler)
1330		// and HFiles are okay (they might be used by the SFiles).
1331		// Also Sysofiles are okay (they might not contain object
1332		// code; see issue #16050).
1333	}
1334
1335	setError := func(msg string) {
1336		p.Error = &PackageError{
1337			ImportStack: stk.Copy(),
1338			Err:         msg,
1339		}
1340	}
1341
1342	// The gc toolchain only permits C source files with cgo or SWIG.
1343	if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" {
1344		setError(fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")))
1345		return
1346	}
1347
1348	// C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,
1349	// regardless of toolchain.
1350	if len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1351		setError(fmt.Sprintf("C++ source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CXXFiles, " ")))
1352		return
1353	}
1354	if len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1355		setError(fmt.Sprintf("Objective-C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.MFiles, " ")))
1356		return
1357	}
1358	if len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1359		setError(fmt.Sprintf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
1360		return
1361	}
1362
1363	// Check for case-insensitive collisions of import paths.
1364	fold := str.ToFold(p.ImportPath)
1365	if other := foldPath[fold]; other == "" {
1366		foldPath[fold] = p.ImportPath
1367	} else if other != p.ImportPath {
1368		setError(fmt.Sprintf("case-insensitive import collision: %q and %q", p.ImportPath, other))
1369		return
1370	}
1371}
1372
1373// SafeArg reports whether arg is a "safe" command-line argument,
1374// meaning that when it appears in a command-line, it probably
1375// doesn't have some special meaning other than its own name.
1376// Obviously args beginning with - are not safe (they look like flags).
1377// Less obviously, args beginning with @ are not safe (they look like
1378// GNU binutils flagfile specifiers, sometimes called "response files").
1379// To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.
1380// We accept leading . _ and / as likely in file system paths.
1381// There is a copy of this function in cmd/compile/internal/gc/noder.go.
1382func SafeArg(name string) bool {
1383	if name == "" {
1384		return false
1385	}
1386	c := name[0]
1387	return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
1388}
1389
1390// LinkerDeps returns the list of linker-induced dependencies for main package p.
1391func LinkerDeps(p *Package) []string {
1392	// Everything links runtime.
1393	deps := []string{"runtime"}
1394
1395	// External linking mode forces an import of runtime/cgo.
1396	if externalLinkingForced(p) && cfg.BuildContext.Compiler != "gccgo" {
1397		deps = append(deps, "runtime/cgo")
1398	}
1399	// On ARM with GOARM=5, it forces an import of math, for soft floating point.
1400	if cfg.Goarch == "arm" {
1401		deps = append(deps, "math")
1402	}
1403	// Using the race detector forces an import of runtime/race.
1404	if cfg.BuildRace {
1405		deps = append(deps, "runtime/race")
1406	}
1407	// Using memory sanitizer forces an import of runtime/msan.
1408	if cfg.BuildMSan {
1409		deps = append(deps, "runtime/msan")
1410	}
1411
1412	return deps
1413}
1414
1415// externalLinkingForced reports whether external linking is being
1416// forced even for programs that do not use cgo.
1417func externalLinkingForced(p *Package) bool {
1418	// Some targets must use external linking even inside GOROOT.
1419	switch cfg.BuildContext.GOOS {
1420	case "android":
1421		return true
1422	case "darwin":
1423		switch cfg.BuildContext.GOARCH {
1424		case "arm", "arm64":
1425			return true
1426		}
1427	}
1428
1429	if !cfg.BuildContext.CgoEnabled {
1430		return false
1431	}
1432	// Currently build modes c-shared, pie (on systems that do not
1433	// support PIE with internal linking mode (currently all
1434	// systems: issue #18968)), plugin, and -linkshared force
1435	// external linking mode, as of course does
1436	// -ldflags=-linkmode=external. External linking mode forces
1437	// an import of runtime/cgo.
1438	pieCgo := cfg.BuildBuildmode == "pie"
1439	linkmodeExternal := false
1440	if p != nil {
1441		ldflags := BuildLdflags.For(p)
1442		for i, a := range ldflags {
1443			if a == "-linkmode=external" {
1444				linkmodeExternal = true
1445			}
1446			if a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "external" {
1447				linkmodeExternal = true
1448			}
1449		}
1450	}
1451
1452	return cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal
1453}
1454
1455// mkAbs rewrites list, which must be paths relative to p.Dir,
1456// into a sorted list of absolute paths. It edits list in place but for
1457// convenience also returns list back to its caller.
1458func (p *Package) mkAbs(list []string) []string {
1459	for i, f := range list {
1460		list[i] = filepath.Join(p.Dir, f)
1461	}
1462	sort.Strings(list)
1463	return list
1464}
1465
1466// InternalGoFiles returns the list of Go files being built for the package,
1467// using absolute paths.
1468func (p *Package) InternalGoFiles() []string {
1469	return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
1470}
1471
1472// InternalGoFiles returns the list of all Go files possibly relevant for the package,
1473// using absolute paths. "Possibly relevant" means that files are not excluded
1474// due to build tags, but files with names beginning with . or _ are still excluded.
1475func (p *Package) InternalAllGoFiles() []string {
1476	var extra []string
1477	for _, f := range p.IgnoredGoFiles {
1478		if f != "" && f[0] != '.' || f[0] != '_' {
1479			extra = append(extra, f)
1480		}
1481	}
1482	return p.mkAbs(str.StringList(extra, p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
1483}
1484
1485// usesSwig reports whether the package needs to run SWIG.
1486func (p *Package) UsesSwig() bool {
1487	return len(p.SwigFiles) > 0 || len(p.SwigCXXFiles) > 0
1488}
1489
1490// usesCgo reports whether the package needs to run cgo
1491func (p *Package) UsesCgo() bool {
1492	return len(p.CgoFiles) > 0
1493}
1494
1495// packageList returns the list of packages in the dag rooted at roots
1496// as visited in a depth-first post-order traversal.
1497func PackageList(roots []*Package) []*Package {
1498	seen := map[*Package]bool{}
1499	all := []*Package{}
1500	var walk func(*Package)
1501	walk = func(p *Package) {
1502		if seen[p] {
1503			return
1504		}
1505		seen[p] = true
1506		for _, p1 := range p.Internal.Imports {
1507			walk(p1)
1508		}
1509		all = append(all, p)
1510	}
1511	for _, root := range roots {
1512		walk(root)
1513	}
1514	return all
1515}
1516
1517var cmdCache = map[string]*Package{}
1518
1519func ClearCmdCache() {
1520	for name := range cmdCache {
1521		delete(cmdCache, name)
1522	}
1523}
1524
1525// loadPackage is like loadImport but is used for command-line arguments,
1526// not for paths found in import statements. In addition to ordinary import paths,
1527// loadPackage accepts pseudo-paths beginning with cmd/ to denote commands
1528// in the Go command directory, as well as paths to those directories.
1529func LoadPackage(arg string, stk *ImportStack) *Package {
1530	if build.IsLocalImport(arg) {
1531		dir := arg
1532		if !filepath.IsAbs(dir) {
1533			if abs, err := filepath.Abs(dir); err == nil {
1534				// interpret relative to current directory
1535				dir = abs
1536			}
1537		}
1538		if sub, ok := hasSubdir(cfg.GOROOTsrc, dir); ok && strings.HasPrefix(sub, "cmd/") && !strings.Contains(sub[4:], "/") {
1539			arg = sub
1540		}
1541	}
1542	if strings.HasPrefix(arg, "cmd/") && !strings.Contains(arg[4:], "/") {
1543		if p := cmdCache[arg]; p != nil {
1544			return p
1545		}
1546		stk.Push(arg)
1547		defer stk.Pop()
1548
1549		bp, err := cfg.BuildContext.ImportDir(filepath.Join(cfg.GOROOTsrc, arg), 0)
1550		bp.ImportPath = arg
1551		bp.Goroot = true
1552		bp.BinDir = cfg.GOROOTbin
1553		if cfg.GOROOTbin != "" {
1554			bp.BinDir = cfg.GOROOTbin
1555		}
1556		bp.Root = cfg.GOROOT
1557		bp.SrcRoot = cfg.GOROOTsrc
1558		p := new(Package)
1559		cmdCache[arg] = p
1560		p.load(stk, bp, err)
1561		if p.Error == nil && p.Name != "main" {
1562			p.Error = &PackageError{
1563				ImportStack: stk.Copy(),
1564				Err:         fmt.Sprintf("expected package main but found package %s in %s", p.Name, p.Dir),
1565			}
1566		}
1567		return p
1568	}
1569
1570	// Wasn't a command; must be a package.
1571	// If it is a local import path but names a standard package,
1572	// we treat it as if the user specified the standard package.
1573	// This lets you run go test ./ioutil in package io and be
1574	// referring to io/ioutil rather than a hypothetical import of
1575	// "./ioutil".
1576	if build.IsLocalImport(arg) {
1577		bp, _ := cfg.BuildContext.ImportDir(filepath.Join(base.Cwd, arg), build.FindOnly)
1578		if bp.ImportPath != "" && bp.ImportPath != "." {
1579			arg = bp.ImportPath
1580		}
1581	}
1582
1583	return LoadImport(arg, base.Cwd, nil, stk, nil, 0)
1584}
1585
1586// packages returns the packages named by the
1587// command line arguments 'args'. If a named package
1588// cannot be loaded at all (for example, if the directory does not exist),
1589// then packages prints an error and does not include that
1590// package in the results. However, if errors occur trying
1591// to load dependencies of a named package, the named
1592// package is still returned, with p.Incomplete = true
1593// and details in p.DepsErrors.
1594func Packages(args []string) []*Package {
1595	var pkgs []*Package
1596	for _, pkg := range PackagesAndErrors(args) {
1597		if pkg.Error != nil {
1598			base.Errorf("can't load package: %s", pkg.Error)
1599			continue
1600		}
1601		pkgs = append(pkgs, pkg)
1602	}
1603	return pkgs
1604}
1605
1606// packagesAndErrors is like 'packages' but returns a
1607// *Package for every argument, even the ones that
1608// cannot be loaded at all.
1609// The packages that fail to load will have p.Error != nil.
1610func PackagesAndErrors(args []string) []*Package {
1611	if len(args) > 0 && strings.HasSuffix(args[0], ".go") {
1612		return []*Package{GoFilesPackage(args)}
1613	}
1614
1615	args = ImportPaths(args)
1616	var (
1617		pkgs    []*Package
1618		stk     ImportStack
1619		seenArg = make(map[string]bool)
1620		seenPkg = make(map[*Package]bool)
1621	)
1622
1623	for _, arg := range args {
1624		if seenArg[arg] {
1625			continue
1626		}
1627		seenArg[arg] = true
1628		pkg := LoadPackage(arg, &stk)
1629		if seenPkg[pkg] {
1630			continue
1631		}
1632		seenPkg[pkg] = true
1633		pkgs = append(pkgs, pkg)
1634	}
1635
1636	return pkgs
1637}
1638
1639// packagesForBuild is like 'packages' but fails if any of
1640// the packages or their dependencies have errors
1641// (cannot be built).
1642func PackagesForBuild(args []string) []*Package {
1643	pkgs := PackagesAndErrors(args)
1644	printed := map[*PackageError]bool{}
1645	for _, pkg := range pkgs {
1646		if pkg.Error != nil {
1647			base.Errorf("can't load package: %s", pkg.Error)
1648		}
1649		for _, err := range pkg.DepsErrors {
1650			// Since these are errors in dependencies,
1651			// the same error might show up multiple times,
1652			// once in each package that depends on it.
1653			// Only print each once.
1654			if !printed[err] {
1655				printed[err] = true
1656				base.Errorf("%s", err)
1657			}
1658		}
1659	}
1660	base.ExitIfErrors()
1661
1662	// Check for duplicate loads of the same package.
1663	// That should be impossible, but if it does happen then
1664	// we end up trying to build the same package twice,
1665	// usually in parallel overwriting the same files,
1666	// which doesn't work very well.
1667	seen := map[string]bool{}
1668	reported := map[string]bool{}
1669	for _, pkg := range PackageList(pkgs) {
1670		if seen[pkg.ImportPath] && !reported[pkg.ImportPath] {
1671			reported[pkg.ImportPath] = true
1672			base.Errorf("internal error: duplicate loads of %s", pkg.ImportPath)
1673		}
1674		seen[pkg.ImportPath] = true
1675	}
1676	base.ExitIfErrors()
1677
1678	return pkgs
1679}
1680
1681// GoFilesPackage creates a package for building a collection of Go files
1682// (typically named on the command line). The target is named p.a for
1683// package p or named after the first Go file for package main.
1684func GoFilesPackage(gofiles []string) *Package {
1685	// TODO: Remove this restriction.
1686	for _, f := range gofiles {
1687		if !strings.HasSuffix(f, ".go") {
1688			base.Fatalf("named files must be .go files")
1689		}
1690	}
1691
1692	var stk ImportStack
1693	ctxt := cfg.BuildContext
1694	ctxt.UseAllFiles = true
1695
1696	// Synthesize fake "directory" that only shows the named files,
1697	// to make it look like this is a standard package or
1698	// command directory. So that local imports resolve
1699	// consistently, the files must all be in the same directory.
1700	var dirent []os.FileInfo
1701	var dir string
1702	for _, file := range gofiles {
1703		fi, err := os.Stat(file)
1704		if err != nil {
1705			base.Fatalf("%s", err)
1706		}
1707		if fi.IsDir() {
1708			base.Fatalf("%s is a directory, should be a Go file", file)
1709		}
1710		dir1, _ := filepath.Split(file)
1711		if dir1 == "" {
1712			dir1 = "./"
1713		}
1714		if dir == "" {
1715			dir = dir1
1716		} else if dir != dir1 {
1717			base.Fatalf("named files must all be in one directory; have %s and %s", dir, dir1)
1718		}
1719		dirent = append(dirent, fi)
1720	}
1721	ctxt.ReadDir = func(string) ([]os.FileInfo, error) { return dirent, nil }
1722
1723	var err error
1724	if dir == "" {
1725		dir = base.Cwd
1726	}
1727	dir, err = filepath.Abs(dir)
1728	if err != nil {
1729		base.Fatalf("%s", err)
1730	}
1731
1732	bp, err := ctxt.ImportDir(dir, 0)
1733	pkg := new(Package)
1734	pkg.Internal.Local = true
1735	pkg.Internal.CmdlineFiles = true
1736	stk.Push("main")
1737	pkg.load(&stk, bp, err)
1738	stk.Pop()
1739	pkg.Internal.LocalPrefix = dirToImportPath(dir)
1740	pkg.ImportPath = "command-line-arguments"
1741	pkg.Target = ""
1742
1743	if pkg.Name == "main" {
1744		_, elem := filepath.Split(gofiles[0])
1745		exe := elem[:len(elem)-len(".go")] + cfg.ExeSuffix
1746		if cfg.BuildO == "" {
1747			cfg.BuildO = exe
1748		}
1749		if cfg.GOBIN != "" {
1750			pkg.Target = filepath.Join(cfg.GOBIN, exe)
1751		}
1752	}
1753
1754	return pkg
1755}
1756
1757// GetTestPackagesFor returns package structs ptest, the package p plus
1758// its test files, and pxtest, the external tests of package p.
1759// pxtest may be nil. If there are no test files, forceTest decides
1760// whether this returns a new package struct or just returns p.
1761func GetTestPackagesFor(p *Package, forceTest bool) (ptest, pxtest *Package, err error) {
1762	var imports, ximports []*Package
1763	var stk ImportStack
1764	stk.Push(p.ImportPath + " (test)")
1765	rawTestImports := str.StringList(p.TestImports)
1766	for i, path := range p.TestImports {
1767		p1 := LoadImport(path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], ResolveImport)
1768		if p1.Error != nil {
1769			return nil, nil, p1.Error
1770		}
1771		if len(p1.DepsErrors) > 0 {
1772			err := p1.DepsErrors[0]
1773			err.Pos = "" // show full import stack
1774			return nil, nil, err
1775		}
1776		if str.Contains(p1.Deps, p.ImportPath) || p1.ImportPath == p.ImportPath {
1777			// Same error that loadPackage returns (via reusePackage) in pkg.go.
1778			// Can't change that code, because that code is only for loading the
1779			// non-test copy of a package.
1780			err := &PackageError{
1781				ImportStack:   testImportStack(stk[0], p1, p.ImportPath),
1782				Err:           "import cycle not allowed in test",
1783				IsImportCycle: true,
1784			}
1785			return nil, nil, err
1786		}
1787		p.TestImports[i] = p1.ImportPath
1788		imports = append(imports, p1)
1789	}
1790	stk.Pop()
1791	stk.Push(p.ImportPath + "_test")
1792	pxtestNeedsPtest := false
1793	rawXTestImports := str.StringList(p.XTestImports)
1794	for i, path := range p.XTestImports {
1795		p1 := LoadImport(path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], ResolveImport)
1796		if p1.Error != nil {
1797			return nil, nil, p1.Error
1798		}
1799		if len(p1.DepsErrors) > 0 {
1800			err := p1.DepsErrors[0]
1801			err.Pos = "" // show full import stack
1802			return nil, nil, err
1803		}
1804		if p1.ImportPath == p.ImportPath {
1805			pxtestNeedsPtest = true
1806		} else {
1807			ximports = append(ximports, p1)
1808		}
1809		p.XTestImports[i] = p1.ImportPath
1810	}
1811	stk.Pop()
1812
1813	// Test package.
1814	if len(p.TestGoFiles) > 0 || forceTest {
1815		ptest = new(Package)
1816		*ptest = *p
1817		ptest.GoFiles = nil
1818		ptest.GoFiles = append(ptest.GoFiles, p.GoFiles...)
1819		ptest.GoFiles = append(ptest.GoFiles, p.TestGoFiles...)
1820		ptest.Target = ""
1821		// Note: The preparation of the vet config requires that common
1822		// indexes in ptest.Imports, ptest.Internal.Imports, and ptest.Internal.RawImports
1823		// all line up (but RawImports can be shorter than the others).
1824		// That is, for 0 ≤ i < len(RawImports),
1825		// RawImports[i] is the import string in the program text,
1826		// Imports[i] is the expanded import string (vendoring applied or relative path expanded away),
1827		// and Internal.Imports[i] is the corresponding *Package.
1828		// Any implicitly added imports appear in Imports and Internal.Imports
1829		// but not RawImports (because they were not in the source code).
1830		// We insert TestImports, imports, and rawTestImports at the start of
1831		// these lists to preserve the alignment.
1832		ptest.Imports = str.StringList(p.TestImports, p.Imports)
1833		ptest.Internal.Imports = append(imports, p.Internal.Imports...)
1834		ptest.Internal.RawImports = str.StringList(rawTestImports, p.Internal.RawImports)
1835		ptest.Internal.ForceLibrary = true
1836		ptest.Internal.Build = new(build.Package)
1837		*ptest.Internal.Build = *p.Internal.Build
1838		m := map[string][]token.Position{}
1839		for k, v := range p.Internal.Build.ImportPos {
1840			m[k] = append(m[k], v...)
1841		}
1842		for k, v := range p.Internal.Build.TestImportPos {
1843			m[k] = append(m[k], v...)
1844		}
1845		ptest.Internal.Build.ImportPos = m
1846	} else {
1847		ptest = p
1848	}
1849
1850	// External test package.
1851	if len(p.XTestGoFiles) > 0 {
1852		pxtest = &Package{
1853			PackagePublic: PackagePublic{
1854				Name:       p.Name + "_test",
1855				ImportPath: p.ImportPath + "_test",
1856				Root:       p.Root,
1857				Dir:        p.Dir,
1858				GoFiles:    p.XTestGoFiles,
1859				Imports:    p.XTestImports,
1860			},
1861			Internal: PackageInternal{
1862				LocalPrefix: p.Internal.LocalPrefix,
1863				Build: &build.Package{
1864					ImportPos: p.Internal.Build.XTestImportPos,
1865				},
1866				Imports:    ximports,
1867				RawImports: rawXTestImports,
1868
1869				Asmflags:   p.Internal.Asmflags,
1870				Gcflags:    p.Internal.Gcflags,
1871				Ldflags:    p.Internal.Ldflags,
1872				Gccgoflags: p.Internal.Gccgoflags,
1873			},
1874		}
1875		if pxtestNeedsPtest {
1876			pxtest.Internal.Imports = append(pxtest.Internal.Imports, ptest)
1877		}
1878	}
1879
1880	return ptest, pxtest, nil
1881}
1882
1883func testImportStack(top string, p *Package, target string) []string {
1884	stk := []string{top, p.ImportPath}
1885Search:
1886	for p.ImportPath != target {
1887		for _, p1 := range p.Internal.Imports {
1888			if p1.ImportPath == target || str.Contains(p1.Deps, target) {
1889				stk = append(stk, p1.ImportPath)
1890				p = p1
1891				continue Search
1892			}
1893		}
1894		// Can't happen, but in case it does...
1895		stk = append(stk, "<lost path to cycle>")
1896		break
1897	}
1898	return stk
1899}
1900