@@ -82,39 +82,59 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
82
82
return rs , []* modinfo.ModulePublic {moduleInfo (ctx , rs , Target , mode )}, nil
83
83
}
84
84
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
98
86
for _ , arg := range args {
99
87
if strings .Contains (arg , `\` ) {
100
88
base .Fatalf ("go: module paths never use backslash" )
101
89
}
102
90
if search .IsRelativePath (arg ) {
103
91
base .Fatalf ("go: cannot use relative path %s to specify module" , arg )
104
92
}
105
- if ! HasModRoot () {
106
- if arg == "all" || strings .Contains (arg , "..." ) {
93
+ if arg == "all" || strings .Contains (arg , "..." ) {
94
+ needFullGraph = true
95
+ if ! HasModRoot () {
107
96
base .Fatalf ("go: cannot match %q: %v" , arg , ErrNoModRoot )
108
97
}
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 () {
110
116
base .Fatalf ("go: cannot match %q without -versions or an explicit version: %v" , arg , ErrNoModRoot )
111
117
}
112
118
}
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 {
113
128
if i := strings .Index (arg , "@" ); i >= 0 {
114
129
path := arg [:i ]
115
130
vers := arg [i + 1 :]
116
131
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
+ }
118
138
if current == "none" && mgErr != nil {
119
139
if vers == "upgrade" || vers == "patch" {
120
140
// 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
156
176
} else if strings .Contains (arg , "..." ) {
157
177
match = search .MatchPattern (arg )
158
178
} 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
+ }
160
191
if v == "none" && mgErr != nil {
161
192
// mgErr is already set, so just skip this module.
162
193
continue
0 commit comments