Skip to content

Commit 0e315ad

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: avoid loading the full module graph when listing specific modules
For #36460 For #41297 Updates #29666 Change-Id: I5f324c0ef9a164f8043d2188101d141bb5fa7454 Reviewed-on: https://go-review.googlesource.com/c/go/+/309191 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent c05d50f commit 0e315ad

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

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

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,59 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
8282
return rs, []*modinfo.ModulePublic{moduleInfo(ctx, rs, Target, mode)}, nil
8383
}
8484

85-
var mg *ModuleGraph
86-
if go117LazyTODO {
87-
// Pull the args-loop below into another (new) loop.
88-
// If the main module is lazy, try it once with mg == nil, and then load mg
89-
// and try again.
90-
} else {
91-
// TODO(#41297): Don't bother loading or expanding the graph if all
92-
// arguments are explicit version queries (including if no arguments are
93-
// present at all).
94-
rs, mg, mgErr = expandGraph(ctx, rs)
95-
}
96-
97-
matchedModule := map[module.Version]bool{}
85+
needFullGraph := false
9886
for _, arg := range args {
9987
if strings.Contains(arg, `\`) {
10088
base.Fatalf("go: module paths never use backslash")
10189
}
10290
if search.IsRelativePath(arg) {
10391
base.Fatalf("go: cannot use relative path %s to specify module", arg)
10492
}
105-
if !HasModRoot() {
106-
if arg == "all" || strings.Contains(arg, "...") {
93+
if arg == "all" || strings.Contains(arg, "...") {
94+
needFullGraph = true
95+
if !HasModRoot() {
10796
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
10897
}
109-
if mode&ListVersions == 0 && !strings.Contains(arg, "@") {
98+
continue
99+
}
100+
if i := strings.Index(arg, "@"); i >= 0 {
101+
path := arg[:i]
102+
vers := arg[i+1:]
103+
if vers == "upgrade" || vers == "patch" {
104+
if _, ok := rs.rootSelected(path); !ok || rs.depth == eager {
105+
needFullGraph = true
106+
if !HasModRoot() {
107+
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
108+
}
109+
}
110+
}
111+
continue
112+
}
113+
if _, ok := rs.rootSelected(arg); !ok || rs.depth == eager {
114+
needFullGraph = true
115+
if mode&ListVersions == 0 && !HasModRoot() {
110116
base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot)
111117
}
112118
}
119+
}
120+
121+
var mg *ModuleGraph
122+
if needFullGraph {
123+
rs, mg, mgErr = expandGraph(ctx, rs)
124+
}
125+
126+
matchedModule := map[module.Version]bool{}
127+
for _, arg := range args {
113128
if i := strings.Index(arg, "@"); i >= 0 {
114129
path := arg[:i]
115130
vers := arg[i+1:]
116131

117-
current := mg.Selected(path)
132+
var current string
133+
if mg == nil {
134+
current, _ = rs.rootSelected(path)
135+
} else {
136+
current = mg.Selected(path)
137+
}
118138
if current == "none" && mgErr != nil {
119139
if vers == "upgrade" || vers == "patch" {
120140
// The module graph is incomplete, so we don't know what version we're
@@ -156,7 +176,18 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
156176
} else if strings.Contains(arg, "...") {
157177
match = search.MatchPattern(arg)
158178
} else {
159-
v := mg.Selected(arg)
179+
var v string
180+
if mg == nil {
181+
var ok bool
182+
v, ok = rs.rootSelected(arg)
183+
if !ok {
184+
// We checked rootSelected(arg) in the earlier args loop, so if there
185+
// is no such root we should have loaded a non-nil mg.
186+
panic(fmt.Sprintf("internal error: root requirement expected but not found for %v", arg))
187+
}
188+
} else {
189+
v = mg.Selected(arg)
190+
}
160191
if v == "none" && mgErr != nil {
161192
// mgErr is already set, so just skip this module.
162193
continue

src/cmd/go/testdata/script/mod_list_sums.txt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@
44
go mod init m
55
go mod edit -require=rsc.io/[email protected]
66

7-
# 'go list' currently loads the whole build list, even when listing only
8-
# non-dependencies.
9-
#
10-
# TODO(#41297): Thes should not be errors.
7+
go list -m -mod=readonly rsc.io/quote@latest
8+
stdout '^rsc\.io/quote v1\.5\.2$'
9+
! stderr .
1110

12-
! go list -m -mod=readonly rsc.io/quote@latest
13-
stderr '^go list -m: rsc\.io/quote@v1\.5\.1: missing go\.sum entry; to add it:\n\tgo mod download rsc\.io/quote$'
14-
15-
! go list -m -mod=readonly -versions rsc.io/quote
16-
stderr '^go list -m: rsc\.io/quote@v1\.5\.1: missing go\.sum entry; to add it:\n\tgo mod download rsc\.io/quote$'
11+
go list -m -mod=readonly -versions rsc.io/quote
12+
stdout 'rsc\.io/quote v1\.0\.0 .* v1\.5\.3-pre1$'
13+
! stderr .
1714

1815
# Incidentally fetching the required version of a module records its checksum,
1916
# just because it happens to be in the build list, and recording the checksum
2017
# triggers an error under -mod=readonly.
2118
#
2219
# TODO(#41297): This should not be an error.
2320
! go list -m -mod=readonly rsc.io/quote@<v1.5.2
24-
stderr '^go list -m: rsc\.io/quote@v1\.5\.1: missing go\.sum entry; to add it:\n\tgo mod download rsc\.io/quote$'
25-
! stderr '^go: updates to go.sum needed, disabled by -mod=readonly$'
21+
stderr '^go: updates to go.sum needed, disabled by -mod=readonly$'
22+
! stderr 'missing go.sum entry'
2623

2724
# Attempting to list the versions of a module that is not a root dependency
2825
# causes the build list to be resolved (so that the selected version can *also*

0 commit comments

Comments
 (0)