aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/load/pkg.go
diff options
context:
space:
mode:
authorJeremy Faller <jeremy@golang.org>2020-09-30 17:57:14 -0400
committerJeremy Faller <jeremy@golang.org>2020-09-30 18:00:58 -0400
commit91e4d2d57bc341dd82c98247117114c851380aef (patch)
tree15a2d023cdc63543cf8a6e99f8a561c0a0459000 /src/cmd/go/internal/load/pkg.go
parentc863e14a6c15e174ac0979ddd7f9530d6a4ec9cc (diff)
parent846dce9d05f19a1f53465e62a304dea21b99f910 (diff)
downloadgo-91e4d2d57bc341dd82c98247117114c851380aef.tar.gz
go-91e4d2d57bc341dd82c98247117114c851380aef.zip
[dev.link] Merge branch 'master' into dev.link
2 conflicts, that make sense. src/cmd/internal/obj/objfile.go src/cmd/link/internal/loader/loader.go Change-Id: Ib224e2d248cb568fa1e888af79dd908b2f5e05ff
Diffstat (limited to 'src/cmd/go/internal/load/pkg.go')
-rw-r--r--src/cmd/go/internal/load/pkg.go85
1 files changed, 45 insertions, 40 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 71fd9b5538..5cc77915e7 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -28,27 +28,13 @@ import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/modinfo"
+ "cmd/go/internal/modload"
"cmd/go/internal/par"
"cmd/go/internal/search"
"cmd/go/internal/str"
"cmd/go/internal/trace"
)
-var (
- // module initialization hook; never nil, no-op if module use is disabled
- ModInit func()
-
- // module hooks; nil if module use is disabled
- ModBinDir func() string // return effective bin directory
- ModLookup func(parentPath string, parentIsStd bool, path string) (dir, realPath string, err error) // lookup effective meaning of import
- ModPackageModuleInfo func(path string) *modinfo.ModulePublic // return module info for Package struct
- ModImportPaths func(ctx context.Context, args []string) []*search.Match // expand import paths
- ModPackageBuildInfo func(main string, deps []string) string // return module info to embed in binary
- ModInfoProg func(info string, isgccgo bool) []byte // wrap module info in .go code for binary
- ModImportFromFiles func(context.Context, []string) // update go.mod to add modules for imports in these files
- ModDirImportPath func(string) string // return effective import path for directory
-)
-
var IgnoreImports bool // control whether we ignore imports in packages
// A Package describes a single package found in a directory.
@@ -434,13 +420,16 @@ type ImportPathError interface {
ImportPath() string
}
+var (
+ _ ImportPathError = (*importError)(nil)
+ _ ImportPathError = (*modload.ImportMissingError)(nil)
+)
+
type importError struct {
importPath string
err error // created with fmt.Errorf
}
-var _ ImportPathError = (*importError)(nil)
-
func ImportErrorf(path, format string, args ...interface{}) ImportPathError {
err := &importError{importPath: path, err: fmt.Errorf(format, args...)}
if errStr := err.Error(); !strings.Contains(errStr, path) {
@@ -753,7 +742,7 @@ func loadPackageData(path, parentPath, parentDir, parentRoot string, parentIsStd
// For vendored imports, it is the expanded form.
//
// Note that when modules are enabled, local import paths are normally
- // canonicalized by modload.ImportPaths before now. However, if there's an
+ // canonicalized by modload.LoadPackages before now. However, if there's an
// error resolving a local path, it will be returned untransformed
// so that 'go list -e' reports something useful.
importKey := importSpec{
@@ -770,7 +759,7 @@ func loadPackageData(path, parentPath, parentDir, parentRoot string, parentIsStd
r.dir = filepath.Join(parentDir, path)
r.path = dirToImportPath(r.dir)
} else if cfg.ModulesEnabled {
- r.dir, r.path, r.err = ModLookup(parentPath, parentIsStd, path)
+ r.dir, r.path, r.err = modload.Lookup(parentPath, parentIsStd, path)
} else if mode&ResolveImport != 0 {
// We do our own path resolution, because we want to
// find out the key to use in packageCache without the
@@ -801,7 +790,7 @@ func loadPackageData(path, parentPath, parentDir, parentRoot string, parentIsStd
}
data.p, data.err = cfg.BuildContext.ImportDir(r.dir, buildMode)
if data.p.Root == "" && cfg.ModulesEnabled {
- if info := ModPackageModuleInfo(path); info != nil {
+ if info := modload.PackageModuleInfo(path); info != nil {
data.p.Root = info.Dir
}
}
@@ -827,7 +816,7 @@ func loadPackageData(path, parentPath, parentDir, parentRoot string, parentIsStd
if cfg.GOBIN != "" {
data.p.BinDir = cfg.GOBIN
} else if cfg.ModulesEnabled {
- data.p.BinDir = ModBinDir()
+ data.p.BinDir = modload.BinDir()
}
}
@@ -895,8 +884,8 @@ var preloadWorkerCount = runtime.GOMAXPROCS(0)
// to ensure preload goroutines are no longer active. This is necessary
// because of global mutable state that cannot safely be read and written
// concurrently. In particular, packageDataCache may be cleared by "go get"
-// in GOPATH mode, and modload.loaded (accessed via ModLookup) may be
-// modified by modload.ImportPaths (ModImportPaths).
+// in GOPATH mode, and modload.loaded (accessed via modload.Lookup) may be
+// modified by modload.LoadPackages.
type preload struct {
cancel chan struct{}
sema chan struct{}
@@ -1006,7 +995,7 @@ func ResolveImportPath(parent *Package, path string) (found string) {
func resolveImportPath(path, parentPath, parentDir, parentRoot string, parentIsStd bool) (found string) {
if cfg.ModulesEnabled {
- if _, p, e := ModLookup(parentPath, parentIsStd, path); e == nil {
+ if _, p, e := modload.Lookup(parentPath, parentIsStd, path); e == nil {
return p
}
return path
@@ -1369,7 +1358,7 @@ func disallowInternal(srcDir string, importer *Package, importerPath string, p *
// directory containing them.
// If the directory is outside the main module, this will resolve to ".",
// which is not a prefix of any valid module.
- importerPath = ModDirImportPath(importer.Dir)
+ importerPath = modload.DirImportPath(importer.Dir)
}
parentOfInternal := p.ImportPath[:i]
if str.HasPathPrefix(importerPath, parentOfInternal) {
@@ -1652,7 +1641,7 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor
elem = full
}
if p.Internal.Build.BinDir == "" && cfg.ModulesEnabled {
- p.Internal.Build.BinDir = ModBinDir()
+ p.Internal.Build.BinDir = modload.BinDir()
}
if p.Internal.Build.BinDir != "" {
// Install to GOBIN or bin of GOPATH entry.
@@ -1861,9 +1850,9 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor
if p.Internal.CmdlineFiles {
mainPath = "command-line-arguments"
}
- p.Module = ModPackageModuleInfo(mainPath)
+ p.Module = modload.PackageModuleInfo(mainPath)
if p.Name == "main" && len(p.DepsErrors) == 0 {
- p.Internal.BuildInfo = ModPackageBuildInfo(mainPath, p.Deps)
+ p.Internal.BuildInfo = modload.PackageBuildInfo(mainPath, p.Deps)
}
}
}
@@ -1961,7 +1950,7 @@ func externalLinkingForced(p *Package) bool {
if cfg.BuildContext.GOARCH != "arm64" {
return true
}
- case "darwin":
+ case "darwin", "ios":
if cfg.BuildContext.GOARCH == "arm64" {
return true
}
@@ -2117,6 +2106,18 @@ func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack,
return p
}
+// ModResolveTests indicates whether calls to the module loader should also
+// resolve test dependencies of the requested packages.
+//
+// If ModResolveTests is true, then the module loader needs to resolve test
+// dependencies at the same time as packages; otherwise, the test dependencies
+// of those packages could be missing, and resolving those missing dependencies
+// could change the selected versions of modules that provide other packages.
+//
+// TODO(#40775): Change this from a global variable to an explicit function
+// argument where needed.
+var ModResolveTests bool
+
// Packages returns the packages named by the
// command line arguments 'args'. If a named package
// cannot be loaded at all (for example, if the directory does not exist),
@@ -2158,7 +2159,18 @@ func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
}
}
- matches := ImportPaths(ctx, patterns)
+ var matches []*search.Match
+ if modload.Init(); cfg.ModulesEnabled {
+ loadOpts := modload.PackageOpts{
+ ResolveMissingImports: true,
+ LoadTests: ModResolveTests,
+ SilenceErrors: true,
+ }
+ matches, _ = modload.LoadPackages(ctx, loadOpts, patterns...)
+ } else {
+ matches = search.ImportPaths(patterns)
+ }
+
var (
pkgs []*Package
stk ImportStack
@@ -2228,13 +2240,6 @@ func setToolFlags(pkgs ...*Package) {
}
}
-func ImportPaths(ctx context.Context, args []string) []*search.Match {
- if ModInit(); cfg.ModulesEnabled {
- return ModImportPaths(ctx, args)
- }
- return search.ImportPaths(args)
-}
-
// PackagesForBuild is like Packages but exits
// if any of the packages or their dependencies have errors
// (cannot be built).
@@ -2282,7 +2287,7 @@ func PackagesForBuild(ctx context.Context, args []string) []*Package {
// (typically named on the command line). The target is named p.a for
// package p or named after the first Go file for package main.
func GoFilesPackage(ctx context.Context, gofiles []string) *Package {
- ModInit()
+ modload.Init()
for _, f := range gofiles {
if !strings.HasSuffix(f, ".go") {
@@ -2329,7 +2334,7 @@ func GoFilesPackage(ctx context.Context, gofiles []string) *Package {
ctxt.ReadDir = func(string) ([]os.FileInfo, error) { return dirent, nil }
if cfg.ModulesEnabled {
- ModImportFromFiles(ctx, gofiles)
+ modload.ImportFromFiles(ctx, gofiles)
}
var err error
@@ -2357,7 +2362,7 @@ func GoFilesPackage(ctx context.Context, gofiles []string) *Package {
if cfg.GOBIN != "" {
pkg.Target = filepath.Join(cfg.GOBIN, exe)
} else if cfg.ModulesEnabled {
- pkg.Target = filepath.Join(ModBinDir(), exe)
+ pkg.Target = filepath.Join(modload.BinDir(), exe)
}
}