Skip to content

Commit 564b350

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: rename LoadBuildList and BuildList
With lazy loading, the “build list” can be refined as packages are loaded. Rename functions that return the build list to more precisely describe the set of modules returned by the call. Also eliminate a redundant call to LoadBuildList (right before ListModules, which itself begins with the same call). For #36460 Change-Id: I0fc4f9dd7602e0df5e166e329ee5d516d810ca53 Reviewed-on: https://go-review.googlesource.com/c/go/+/249878 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 521393e commit 564b350

File tree

8 files changed

+36
-33
lines changed

8 files changed

+36
-33
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
437437
}
438438
}
439439

440-
modload.LoadBuildList(ctx)
441-
442440
mods := modload.ListModules(ctx, args, *listU, *listVersions, *listRetracted)
443441
if !*listE {
444442
for _, m := range mods {

src/cmd/go/internal/modcmd/graph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) {
4848
base.Fatalf("go: cannot find main module; see 'go help modules'")
4949
}
5050
}
51-
modload.LoadBuildList(ctx)
51+
modload.LoadAllModules(ctx)
5252

5353
reqs := modload.MinReqs()
5454
format := func(m module.Version) string {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
7777
}
7878

7979
var buf bytes.Buffer
80-
for _, m := range modload.BuildList()[1:] {
80+
for _, m := range modload.LoadedModules()[1:] {
8181
if pkgs := modpkgs[m]; len(pkgs) > 0 || isExplicit[m] {
8282
line := moduleLine(m, modload.Replacement(m))
8383
buf.WriteString(line)

src/cmd/go/internal/modcmd/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
6060
sem := make(chan token, runtime.GOMAXPROCS(0))
6161

6262
// Use a slice of result channels, so that the output is deterministic.
63-
mods := modload.LoadBuildList(ctx)[1:]
63+
mods := modload.LoadAllModules(ctx)[1:]
6464
errsChans := make([]<-chan []error, len(mods))
6565

6666
for i, mod := range mods {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
278278
}
279279
modload.LoadTests = *getT
280280

281-
buildList := modload.LoadBuildList(ctx)
281+
buildList := modload.LoadAllModules(ctx)
282282
buildList = buildList[:len(buildList):len(buildList)] // copy on append
283283
versionByPath := make(map[string]string)
284284
for _, m := range buildList {
@@ -599,7 +599,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
599599
base.ExitIfErrors()
600600

601601
// Stop if no changes have been made to the build list.
602-
buildList = modload.BuildList()
602+
buildList = modload.LoadedModules()
603603
eq := len(buildList) == len(prevBuildList)
604604
for i := 0; eq && i < len(buildList); i++ {
605605
eq = buildList[i] == prevBuildList[i]
@@ -617,7 +617,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
617617

618618
// Handle downgrades.
619619
var down []module.Version
620-
for _, m := range modload.BuildList() {
620+
for _, m := range modload.LoadedModules() {
621621
q := byPath[m.Path]
622622
if q != nil && semver.Compare(m.Version, q.m.Version) > 0 {
623623
down = append(down, module.Version{Path: m.Path, Version: q.m.Version})
@@ -641,7 +641,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
641641
var lostUpgrades []*query
642642
if len(down) > 0 {
643643
versionByPath = make(map[string]string)
644-
for _, m := range modload.BuildList() {
644+
for _, m := range modload.LoadedModules() {
645645
versionByPath[m.Path] = m.Version
646646
}
647647
for _, q := range byPath {
@@ -892,7 +892,7 @@ func reportRetractions(ctx context.Context) {
892892
// Use modload.ListModules, since that provides information in the same format
893893
// as 'go list -m'. Don't query for "all", since that's not allowed outside a
894894
// module.
895-
buildList := modload.BuildList()
895+
buildList := modload.LoadedModules()
896896
args := make([]string, 0, len(buildList))
897897
for _, m := range buildList {
898898
if m.Version == "" {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func ModuleInfo(ctx context.Context, path string) *modinfo.ModulePublic {
7676
return moduleInfo(ctx, m, fromBuildList, listRetracted)
7777
}
7878

79-
for _, m := range BuildList() {
79+
for _, m := range LoadedModules() {
8080
if m.Path == path {
8181
fromBuildList := true
8282
return moduleInfo(ctx, m, fromBuildList, listRetracted)

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,28 @@ import (
2727
//
2828
var buildList []module.Version
2929

30-
// LoadBuildList loads and returns the build list from go.mod.
31-
// The loading of the build list happens automatically in ImportPaths:
32-
// LoadBuildList need only be called if ImportPaths is not
33-
// (typically in commands that care about the module but
34-
// no particular package).
35-
func LoadBuildList(ctx context.Context) []module.Version {
30+
// LoadAllModules loads and returns the list of modules matching the "all"
31+
// module pattern, starting with the Target module and in a deterministic
32+
// (stable) order, without loading any packages.
33+
//
34+
// Modules are loaded automatically (and lazily) in ImportPaths:
35+
// LoadAllModules need only be called if ImportPaths is not,
36+
// typically in commands that care about modules but no particular package.
37+
//
38+
// The caller must not modify the returned list.
39+
func LoadAllModules(ctx context.Context) []module.Version {
3640
InitMod(ctx)
3741
ReloadBuildList()
3842
WriteGoMod()
3943
return buildList
4044
}
4145

42-
// ReloadBuildList resets the state of loaded packages, then loads and returns
43-
// the build list set in SetBuildList.
44-
func ReloadBuildList() []module.Version {
45-
loaded = loadFromRoots(loaderParams{
46-
tags: imports.Tags(),
47-
listRoots: func() []string { return nil },
48-
allClosesOverTests: index.allPatternClosesOverTests(), // but doesn't matter because the root list is empty.
49-
})
50-
return buildList
51-
}
52-
53-
// BuildList returns the module build list,
54-
// typically constructed by a previous call to
55-
// LoadBuildList or ImportPaths.
46+
// LoadedModules returns the list of module requirements loaded or set by a
47+
// previous call (typically LoadAllModules or ImportPaths), starting with the
48+
// Target module and in a deterministic (stable) order.
49+
//
5650
// The caller must not modify the returned list.
57-
func BuildList() []module.Version {
51+
func LoadedModules() []module.Version {
5852
return buildList
5953
}
6054

@@ -65,6 +59,17 @@ func SetBuildList(list []module.Version) {
6559
buildList = append([]module.Version{}, list...)
6660
}
6761

62+
// ReloadBuildList resets the state of loaded packages, then loads and returns
63+
// the build list set in SetBuildList.
64+
func ReloadBuildList() []module.Version {
65+
loaded = loadFromRoots(loaderParams{
66+
tags: imports.Tags(),
67+
listRoots: func() []string { return nil },
68+
allClosesOverTests: index.allPatternClosesOverTests(), // but doesn't matter because the root list is empty.
69+
})
70+
return buildList
71+
}
72+
6873
// TidyBuildList trims the build list to the minimal requirements needed to
6974
// retain the same versions of all packages from the preceding Load* or
7075
// ImportPaths* call.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func ListModules(ctx context.Context, args []string, listU, listVersions, listRe
5858
}
5959

6060
func listModules(ctx context.Context, args []string, listVersions, listRetracted bool) []*modinfo.ModulePublic {
61-
LoadBuildList(ctx)
61+
LoadAllModules(ctx)
6262
if len(args) == 0 {
6363
return []*modinfo.ModulePublic{moduleInfo(ctx, buildList[0], true, listRetracted)}
6464
}

0 commit comments

Comments
 (0)