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