Skip to content

Commit fd75989

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Trust: Jay Conrod <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent d42b32e commit fd75989

File tree

13 files changed

+199
-142
lines changed

13 files changed

+199
-142
lines changed

src/cmd/go/internal/list/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ var (
325325
var nl = []byte{'\n'}
326326

327327
func runList(ctx context.Context, cmd *base.Command, args []string) {
328-
modload.LoadTests = *listTest
328+
load.ModResolveTests = *listTest
329329
work.BuildInit()
330330
out := newTrackingWriter(os.Stdout)
331331
defer out.w.Flush()

src/cmd/go/internal/load/pkg.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ func loadPackageData(path, parentPath, parentDir, parentRoot string, parentIsStd
742742
// For vendored imports, it is the expanded form.
743743
//
744744
// Note that when modules are enabled, local import paths are normally
745-
// canonicalized by modload.ImportPaths before now. However, if there's an
745+
// canonicalized by modload.LoadPackages before now. However, if there's an
746746
// error resolving a local path, it will be returned untransformed
747747
// so that 'go list -e' reports something useful.
748748
importKey := importSpec{
@@ -885,7 +885,7 @@ var preloadWorkerCount = runtime.GOMAXPROCS(0)
885885
// because of global mutable state that cannot safely be read and written
886886
// concurrently. In particular, packageDataCache may be cleared by "go get"
887887
// in GOPATH mode, and modload.loaded (accessed via modload.Lookup) may be
888-
// modified by modload.ImportPaths.
888+
// modified by modload.LoadPackages.
889889
type preload struct {
890890
cancel chan struct{}
891891
sema chan struct{}
@@ -2106,6 +2106,18 @@ func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack,
21062106
return p
21072107
}
21082108

2109+
// ModResolveTests indicates whether calls to the module loader should also
2110+
// resolve test dependencies of the requested packages.
2111+
//
2112+
// If ModResolveTests is true, then the module loader needs to resolve test
2113+
// dependencies at the same time as packages; otherwise, the test dependencies
2114+
// of those packages could be missing, and resolving those missing dependencies
2115+
// could change the selected versions of modules that provide other packages.
2116+
//
2117+
// TODO(#40775): Change this from a global variable to an explicit function
2118+
// argument where needed.
2119+
var ModResolveTests bool
2120+
21092121
// Packages returns the packages named by the
21102122
// command line arguments 'args'. If a named package
21112123
// 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 {
21472159
}
21482160
}
21492161

2150-
matches := ImportPaths(ctx, patterns)
2162+
var matches []*search.Match
2163+
if modload.Init(); cfg.ModulesEnabled {
2164+
loadOpts := modload.PackageOpts{
2165+
ResolveMissingImports: true,
2166+
LoadTests: ModResolveTests,
2167+
AllowErrors: true,
2168+
}
2169+
matches, _ = modload.LoadPackages(ctx, loadOpts, patterns...)
2170+
} else {
2171+
matches = search.ImportPaths(patterns)
2172+
}
2173+
21512174
var (
21522175
pkgs []*Package
21532176
stk ImportStack
@@ -2217,13 +2240,6 @@ func setToolFlags(pkgs ...*Package) {
22172240
}
22182241
}
22192242

2220-
func ImportPaths(ctx context.Context, args []string) []*search.Match {
2221-
if modload.Init(); cfg.ModulesEnabled {
2222-
return modload.ImportPaths(ctx, args)
2223-
}
2224-
return search.ImportPaths(args)
2225-
}
2226-
22272243
// PackagesForBuild is like Packages but exits
22282244
// if any of the packages or their dependencies have errors
22292245
// (cannot be built).

src/cmd/go/internal/modcmd/tidy.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package modcmd
99
import (
1010
"cmd/go/internal/base"
1111
"cmd/go/internal/cfg"
12+
"cmd/go/internal/imports"
1213
"cmd/go/internal/modload"
1314
"context"
1415
)
@@ -49,11 +50,15 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
4950
// those packages. In order to make 'go test' reproducible for the packages
5051
// that are in 'all' but outside of the main module, we must explicitly
5152
// request that their test dependencies be included.
52-
modload.LoadTests = true
5353
modload.ForceUseModules = true
5454
modload.RootMode = modload.NeedRoot
5555

56-
modload.LoadALL(ctx)
56+
modload.LoadPackages(ctx, modload.PackageOpts{
57+
Tags: imports.AnyTags(),
58+
ResolveMissingImports: true,
59+
LoadTests: true,
60+
AllowErrors: false, // TODO(#26603): Make this a flag.
61+
}, "all")
5762
modload.TidyBuildList()
5863
modload.TrimGoSum()
5964
modload.WriteGoMod()

src/cmd/go/internal/modcmd/vendor.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
4949
}
5050
modload.ForceUseModules = true
5151
modload.RootMode = modload.NeedRoot
52-
pkgs := modload.LoadVendor(ctx)
52+
53+
loadOpts := modload.PackageOpts{
54+
Tags: imports.AnyTags(),
55+
ResolveMissingImports: true,
56+
UseVendorAll: true,
57+
}
58+
_, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
5359

5460
vdir := filepath.Join(modload.ModRoot(), "vendor")
5561
if err := os.RemoveAll(vdir); err != nil {

src/cmd/go/internal/modcmd/why.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"cmd/go/internal/base"
13+
"cmd/go/internal/imports"
1314
"cmd/go/internal/modload"
1415

1516
"golang.org/x/mod/module"
@@ -63,12 +64,14 @@ func init() {
6364
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
6465
modload.ForceUseModules = true
6566
modload.RootMode = modload.NeedRoot
66-
loadALL := modload.LoadALL
67-
if *whyVendor {
68-
loadALL = modload.LoadVendor
69-
} else {
70-
modload.LoadTests = true
67+
68+
loadOpts := modload.PackageOpts{
69+
Tags: imports.AnyTags(),
70+
LoadTests: !*whyVendor,
71+
AllowErrors: true,
72+
UseVendorAll: *whyVendor,
7173
}
74+
7275
if *whyM {
7376
listU := false
7477
listVersions := false
@@ -80,7 +83,8 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
8083
}
8184
mods := modload.ListModules(ctx, args, listU, listVersions, listRetractions)
8285
byModule := make(map[module.Version][]string)
83-
for _, path := range loadALL(ctx) {
86+
_, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
87+
for _, path := range pkgs {
8488
m := modload.PackageModule(path)
8589
if m.Path != "" {
8690
byModule[m] = append(byModule[m], path)
@@ -109,8 +113,11 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
109113
sep = "\n"
110114
}
111115
} else {
112-
matches := modload.ImportPaths(ctx, args) // resolve to packages
113-
loadALL(ctx) // rebuild graph, from main module (not from named packages)
116+
// Resolve to packages.
117+
matches, _ := modload.LoadPackages(ctx, loadOpts, args...)
118+
119+
modload.LoadPackages(ctx, loadOpts, "all") // rebuild graph, from main module (not from named packages)
120+
114121
sep := ""
115122
for _, m := range matches {
116123
for _, path := range m.Pkgs {

src/cmd/go/internal/modget/get.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
284284
if cfg.Insecure {
285285
fmt.Fprintf(os.Stderr, "go get: -insecure flag is deprecated; see 'go help get' for details\n")
286286
}
287-
modload.LoadTests = *getT
287+
load.ModResolveTests = *getT
288288

289289
// Do not allow any updating of go.mod until we've applied
290290
// all the requested changes and checked that the result matches
@@ -314,7 +314,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
314314

315315
// Add missing modules to the build list.
316316
// We call SetBuildList here and elsewhere, since newUpgrader,
317-
// ImportPathsQuiet, and other functions read the global build list.
317+
// LoadPackages, and other functions read the global build list.
318318
for _, q := range queries {
319319
if _, ok := selectedVersion[q.m.Path]; !ok && q.m.Version != "none" {
320320
buildList = append(buildList, q.m)
@@ -400,9 +400,16 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
400400

401401
if len(pkgPatterns) > 0 {
402402
// Don't load packages if pkgPatterns is empty. Both
403-
// modload.ImportPathsQuiet and ModulePackages convert an empty list
403+
// modload.LoadPackages and ModulePackages convert an empty list
404404
// of patterns to []string{"."}, which is not what we want.
405-
matches = modload.ImportPathsQuiet(ctx, pkgPatterns, imports.AnyTags())
405+
loadOpts := modload.PackageOpts{
406+
Tags: imports.AnyTags(),
407+
ResolveMissingImports: true, // dubious; see https://golang.org/issue/32567
408+
LoadTests: *getT,
409+
AllowErrors: true, // Errors may be fixed by subsequent upgrades or downgrades.
410+
SilenceUnmatchedWarnings: true, // We will warn after iterating below.
411+
}
412+
matches, _ = modload.LoadPackages(ctx, loadOpts, pkgPatterns...)
406413
seenPkgs = make(map[string]bool)
407414
for i, match := range matches {
408415
arg := pkgGets[i]

src/cmd/go/internal/modload/build.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func findStandardImportPath(path string) string {
5050
// PackageModuleInfo returns information about the module that provides
5151
// a given package. If modules are not enabled or if the package is in the
5252
// standard library or if the package was not successfully loaded with
53-
// ImportPaths or a similar loading function, nil is returned.
53+
// LoadPackages or ImportFromFiles, nil is returned.
5454
func PackageModuleInfo(pkgpath string) *modinfo.ModulePublic {
5555
if isStandardImportPath(pkgpath) || !Enabled() {
5656
return nil
@@ -250,8 +250,7 @@ func moduleInfo(ctx context.Context, m module.Version, fromBuildList, listRetrac
250250

251251
// PackageBuildInfo returns a string containing module version information
252252
// for modules providing packages named by path and deps. path and deps must
253-
// name packages that were resolved successfully with ImportPaths or one of
254-
// the Load functions.
253+
// name packages that were resolved successfully with LoadPackages.
255254
func PackageBuildInfo(path string, deps []string) string {
256255
if isStandardImportPath(path) || !Enabled() {
257256
return ""
@@ -321,9 +320,8 @@ func mustFindModule(target, path string) module.Version {
321320
}
322321

323322
// findModule searches for the module that contains the package at path.
324-
// If the package was loaded with ImportPaths or one of the other loading
325-
// functions, its containing module and true are returned. Otherwise,
326-
// module.Version{} and false are returend.
323+
// If the package was loaded, its containing module and true are returned.
324+
// Otherwise, module.Version{} and false are returend.
327325
func findModule(path string) (module.Version, bool) {
328326
if pkg, ok := loaded.pkgCache.Get(path).(*loadPkg); ok {
329327
return pkg.mod, pkg.mod != module.Version{}

src/cmd/go/internal/modload/buildlist.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
)
1818

1919
// buildList is the list of modules to use for building packages.
20-
// It is initialized by calling ImportPaths, ImportFromFiles,
21-
// LoadALL, or LoadBuildList, each of which uses loaded.load.
20+
// It is initialized by calling LoadPackages or ImportFromFiles,
21+
// each of which uses loaded.load.
2222
//
2323
// Ideally, exactly ONE of those functions would be called,
2424
// and exactly once. Most of the time, that's true.
@@ -31,8 +31,8 @@ var buildList []module.Version
3131
// module pattern, starting with the Target module and in a deterministic
3232
// (stable) order, without loading any packages.
3333
//
34-
// Modules are loaded automatically (and lazily) in ImportPaths:
35-
// LoadAllModules need only be called if ImportPaths is not,
34+
// Modules are loaded automatically (and lazily) in LoadPackages:
35+
// LoadAllModules need only be called if LoadPackages is not,
3636
// typically in commands that care about modules but no particular package.
3737
//
3838
// The caller must not modify the returned list.
@@ -44,7 +44,7 @@ func LoadAllModules(ctx context.Context) []module.Version {
4444
}
4545

4646
// LoadedModules returns the list of module requirements loaded or set by a
47-
// previous call (typically LoadAllModules or ImportPaths), starting with the
47+
// previous call (typically LoadAllModules or LoadPackages), starting with the
4848
// Target module and in a deterministic (stable) order.
4949
//
5050
// The caller must not modify the returned list.
@@ -71,8 +71,8 @@ func ReloadBuildList() []module.Version {
7171
}
7272

7373
// TidyBuildList trims the build list to the minimal requirements needed to
74-
// retain the same versions of all packages from the preceding Load* or
75-
// ImportPaths* call.
74+
// retain the same versions of all packages from the preceding call to
75+
// LoadPackages.
7676
func TidyBuildList() {
7777
used := map[module.Version]bool{Target: true}
7878
for _, pkg := range loaded.pkgs {

src/cmd/go/internal/modload/init.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ const (
8383

8484
// ModFile returns the parsed go.mod file.
8585
//
86-
// Note that after calling ImportPaths or LoadBuildList,
86+
// Note that after calling LoadPackages or LoadAllModules,
8787
// the require statements in the modfile.File are no longer
8888
// the source of truth and will be ignored: edits made directly
8989
// will be lost at the next call to WriteGoMod.
9090
// To make permanent changes to the require statements
91-
// in go.mod, edit it before calling ImportPaths or LoadBuildList.
91+
// in go.mod, edit it before loading.
9292
func ModFile() *modfile.File {
9393
Init()
9494
if modFile == nil {
@@ -943,9 +943,9 @@ func WriteGoMod() {
943943

944944
// keepSums returns a set of module sums to preserve in go.sum. The set
945945
// includes entries for all modules used to load packages (according to
946-
// the last load function like ImportPaths, LoadALL, etc.). It also contains
947-
// entries for go.mod files needed for MVS (the version of these entries
948-
// ends with "/go.mod").
946+
// the last load function such as LoadPackages or ImportFromFiles).
947+
// It also contains entries for go.mod files needed for MVS (the version
948+
// of these entries ends with "/go.mod").
949949
//
950950
// If addDirect is true, the set also includes sums for modules directly
951951
// required by go.mod, as represented by the index, with replacements applied.
@@ -977,8 +977,7 @@ func keepSums(addDirect bool) map[module.Version]bool {
977977
}
978978
walk(Target)
979979

980-
// Add entries for modules that provided packages loaded with ImportPaths,
981-
// LoadALL, or similar functions.
980+
// Add entries for modules from which packages were loaded.
982981
if loaded != nil {
983982
for _, pkg := range loaded.pkgs {
984983
m := pkg.mod

0 commit comments

Comments
 (0)