@@ -109,18 +109,19 @@ type ModuleJSON struct {
109109}
110110
111111func runDownload (ctx context.Context , cmd * base.Command , args []string ) {
112- modload .InitWorkfile (modload .LoaderState )
112+ moduleLoaderState := modload .NewState ()
113+ modload .InitWorkfile (moduleLoaderState )
113114
114115 // Check whether modules are enabled and whether we're in a module.
115- modload . LoaderState .ForceUseModules = true
116+ moduleLoaderState .ForceUseModules = true
116117 modload .ExplicitWriteGoMod = true
117118 haveExplicitArgs := len (args ) > 0
118119
119- if modload .HasModRoot (modload . LoaderState ) || modload .WorkFilePath (modload . LoaderState ) != "" {
120- modload .LoadModFile (modload . LoaderState , ctx ) // to fill MainModules
120+ if modload .HasModRoot (moduleLoaderState ) || modload .WorkFilePath (moduleLoaderState ) != "" {
121+ modload .LoadModFile (moduleLoaderState , ctx ) // to fill MainModules
121122
122123 if haveExplicitArgs {
123- for _ , mainModule := range modload . LoaderState .MainModules .Versions () {
124+ for _ , mainModule := range moduleLoaderState .MainModules .Versions () {
124125 targetAtUpgrade := mainModule .Path + "@upgrade"
125126 targetAtPatch := mainModule .Path + "@patch"
126127 for _ , arg := range args {
@@ -130,14 +131,14 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
130131 }
131132 }
132133 }
133- } else if modload .WorkFilePath (modload . LoaderState ) != "" {
134+ } else if modload .WorkFilePath (moduleLoaderState ) != "" {
134135 // TODO(#44435): Think about what the correct query is to download the
135136 // right set of modules. Also see code review comment at
136137 // https://go-review.googlesource.com/c/go/+/359794/comments/ce946a80_6cf53992.
137138 args = []string {"all" }
138139 } else {
139- mainModule := modload . LoaderState .MainModules .Versions ()[0 ]
140- modFile := modload . LoaderState .MainModules .ModFile (mainModule )
140+ mainModule := moduleLoaderState .MainModules .Versions ()[0 ]
141+ modFile := moduleLoaderState .MainModules .ModFile (mainModule )
141142 if modFile .Go == nil || gover .Compare (modFile .Go .Version , gover .ExplicitIndirectVersion ) < 0 {
142143 if len (modFile .Require ) > 0 {
143144 args = []string {"all" }
@@ -153,12 +154,12 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
153154 // However, we also need to load the full module graph, to ensure that
154155 // we have downloaded enough of the module graph to run 'go list all',
155156 // 'go mod graph', and similar commands.
156- _ , err := modload .LoadModGraph (modload . LoaderState , ctx , "" )
157+ _ , err := modload .LoadModGraph (moduleLoaderState , ctx , "" )
157158 if err != nil {
158159 // TODO(#64008): call base.Fatalf instead of toolchain.SwitchOrFatal
159160 // here, since we can only reach this point with an outdated toolchain
160161 // if the go.mod file is inconsistent.
161- toolchain .SwitchOrFatal (modload . LoaderState , ctx , err )
162+ toolchain .SwitchOrFatal (moduleLoaderState , ctx , err )
162163 }
163164
164165 for _ , m := range modFile .Require {
@@ -169,22 +170,22 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
169170 }
170171
171172 if len (args ) == 0 {
172- if modload .HasModRoot (modload . LoaderState ) {
173+ if modload .HasModRoot (moduleLoaderState ) {
173174 os .Stderr .WriteString ("go: no module dependencies to download\n " )
174175 } else {
175176 base .Errorf ("go: no modules specified (see 'go help mod download')" )
176177 }
177178 base .Exit ()
178179 }
179180
180- if * downloadReuse != "" && modload .HasModRoot (modload . LoaderState ) {
181+ if * downloadReuse != "" && modload .HasModRoot (moduleLoaderState ) {
181182 base .Fatalf ("go mod download -reuse cannot be used inside a module" )
182183 }
183184
184185 var mods []* ModuleJSON
185186 type token struct {}
186187 sem := make (chan token , runtime .GOMAXPROCS (0 ))
187- infos , infosErr := modload .ListModules (modload . LoaderState , ctx , args , 0 , * downloadReuse )
188+ infos , infosErr := modload .ListModules (moduleLoaderState , ctx , args , 0 , * downloadReuse )
188189
189190 // There is a bit of a chicken-and-egg problem here: ideally we need to know
190191 // which Go version to switch to download the requested modules, but if we
@@ -211,7 +212,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
211212 // toolchain version) or only one module (as is used by the Go Module Proxy).
212213
213214 if infosErr != nil {
214- sw := toolchain .NewSwitcher (modload . LoaderState )
215+ sw := toolchain .NewSwitcher (moduleLoaderState )
215216 sw .Error (infosErr )
216217 if sw .NeedSwitch () {
217218 sw .Switch (ctx )
@@ -220,7 +221,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
220221 // when we can.
221222 }
222223
223- if ! haveExplicitArgs && modload .WorkFilePath (modload . LoaderState ) == "" {
224+ if ! haveExplicitArgs && modload .WorkFilePath (moduleLoaderState ) == "" {
224225 // 'go mod download' is sometimes run without arguments to pre-populate the
225226 // module cache. In modules that aren't at go 1.17 or higher, it may fetch
226227 // modules that aren't needed to build packages in the main module. This is
@@ -231,7 +232,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
231232 // TODO(#64008): In the future, report an error if go.mod or go.sum need to
232233 // be updated after loading the build list. This may require setting
233234 // the mode to "mod" or "readonly" depending on haveExplicitArgs.
234- if err := modload .WriteGoMod (modload . LoaderState , ctx , modload.WriteOpts {}); err != nil {
235+ if err := modload .WriteGoMod (moduleLoaderState , ctx , modload.WriteOpts {}); err != nil {
235236 base .Fatal (err )
236237 }
237238 }
@@ -291,8 +292,8 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
291292 // with no arguments we download the module pattern "all",
292293 // which may include dependencies that are normally pruned out
293294 // of the individual modules in the workspace.
294- if haveExplicitArgs || modload .WorkFilePath (modload . LoaderState ) != "" {
295- sw := toolchain .NewSwitcher (modload . LoaderState )
295+ if haveExplicitArgs || modload .WorkFilePath (moduleLoaderState ) != "" {
296+ sw := toolchain .NewSwitcher (moduleLoaderState )
296297 // Add errors to the Switcher in deterministic order so that they will be
297298 // logged deterministically.
298299 for _ , m := range mods {
@@ -347,8 +348,8 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
347348 //
348349 // Don't save sums for 'go mod download' without arguments unless we're in
349350 // workspace mode; see comment above.
350- if haveExplicitArgs || modload .WorkFilePath (modload . LoaderState ) != "" {
351- if err := modload .WriteGoMod (modload . LoaderState , ctx , modload.WriteOpts {}); err != nil {
351+ if haveExplicitArgs || modload .WorkFilePath (moduleLoaderState ) != "" {
352+ if err := modload .WriteGoMod (moduleLoaderState , ctx , modload.WriteOpts {}); err != nil {
352353 base .Error (err )
353354 }
354355 }
0 commit comments