aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/load/pkg.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2020-09-17 16:24:29 -0400
committerBryan C. Mills <bcmills@google.com>2020-09-22 16:52:00 +0000
commitfd75989f46c80d2446dd9dcefaffbebdb7f7ea87 (patch)
treeff0ce203b50f1fc75c2be3a3bdee760e0cdb81b6 /src/cmd/go/internal/load/pkg.go
parentd42b32e321fa5c5d2c93b2ad22d48e804c9f45d2 (diff)
downloadgo-fd75989f46c80d2446dd9dcefaffbebdb7f7ea87.tar.gz
go-fd75989f46c80d2446dd9dcefaffbebdb7f7ea87.zip
cmd/go/internal/modget: consolidate Load entrypoints
This change replaces ImportPaths, ImportPathsQuiet, LoadALL, and LoadVendor with a single LoadPackages function, with a LoadOpts struct that more clearly documents the variations in behavior. It also eliminates the cmd/go/internal/load.ImportPaths function, which was undocumented and had only one call site (within its own package). The modload.LoadTests global variable is subsumed by a field in the new LoadOpts struct, and is no longer needed for callers that invoke LoadPackages directly. It has been (temporarily) replaced with a similar global variable, load.ModResolveTests, which can itself be converted to an explicit, local argument. For #37438 For #36460 Updates #40775 Fixes #26977 Change-Id: I4fb6086c01b04de829d98875db19cf0118d40f8c Reviewed-on: https://go-review.googlesource.com/c/go/+/255938 Trust: Bryan C. Mills <bcmills@google.com> Trust: Jay Conrod <jayconrod@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/load/pkg.go')
-rw-r--r--src/cmd/go/internal/load/pkg.go36
1 files changed, 26 insertions, 10 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 077fdf25d3..df9d9964e6 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -742,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{
@@ -885,7 +885,7 @@ var preloadWorkerCount = runtime.GOMAXPROCS(0)
// 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 modload.Lookup) may be
-// modified by modload.ImportPaths.
+// modified by modload.LoadPackages.
type preload struct {
cancel chan struct{}
sema chan struct{}
@@ -2106,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),
@@ -2147,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,
+ AllowErrors: true,
+ }
+ matches, _ = modload.LoadPackages(ctx, loadOpts, patterns...)
+ } else {
+ matches = search.ImportPaths(patterns)
+ }
+
var (
pkgs []*Package
stk ImportStack
@@ -2217,13 +2240,6 @@ func setToolFlags(pkgs ...*Package) {
}
}
-func ImportPaths(ctx context.Context, args []string) []*search.Match {
- if modload.Init(); cfg.ModulesEnabled {
- return modload.ImportPaths(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).