Skip to content

Commit fe345ff

Browse files
committed
cmd/go: use local state object in modget.runGet
This commit modifies `modget.runGet` to construct a new modload.State object using the new constructor instead of the current global `modload.LoaderState` variable. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/modget rf ' add get.go:/func runGet\(/-0 var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runGet://+0 moduleLoaderState := modload.NewState() rm get.go:/var moduleLoaderState \*modload.State/ ' Change-Id: I977c3f57c00b60d383ffd2c2c0d7bc90813c7988 Reviewed-on: https://go-review.googlesource.com/c/go/+/711126 Reviewed-by: Michael Matloob <matloob@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@google.com>
1 parent d312e27 commit fe345ff

File tree

1 file changed

+27
-26
lines changed
  • src/cmd/go/internal/modget

1 file changed

+27
-26
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func init() {
273273
}
274274

275275
func runGet(ctx context.Context, cmd *base.Command, args []string) {
276+
moduleLoaderState := modload.NewState()
276277
switch getU.version {
277278
case "", "upgrade", "patch":
278279
// ok
@@ -298,7 +299,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
298299
base.Fatalf("go: -insecure flag is no longer supported; use GOINSECURE instead")
299300
}
300301

301-
modload.LoaderState.ForceUseModules = true
302+
moduleLoaderState.ForceUseModules = true
302303

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

308309
// Allow looking up modules for import paths when outside of a module.
309310
// 'go get' is expected to do this, unlike other commands.
310-
modload.AllowMissingModuleImports(modload.LoaderState)
311+
modload.AllowMissingModuleImports(moduleLoaderState)
311312

312313
// 'go get' no longer builds or installs packages, so there's nothing to do
313314
// if there's no go.mod file.
314315
// TODO(#40775): make modload.Init return ErrNoModRoot instead of exiting.
315316
// We could handle that here by printing a different message.
316-
modload.Init(modload.LoaderState)
317-
if !modload.HasModRoot(modload.LoaderState) {
317+
modload.Init(moduleLoaderState)
318+
if !modload.HasModRoot(moduleLoaderState) {
318319
base.Fatalf("go: go.mod file not found in current directory or any parent directory.\n" +
319320
"\t'go get' is no longer supported outside a module.\n" +
320321
"\tTo build and install a command, use 'go install' with a version,\n" +
@@ -323,7 +324,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
323324
"\tor run 'go help get' or 'go help install'.")
324325
}
325326

326-
dropToolchain, queries := parseArgs(modload.LoaderState, ctx, args)
327+
dropToolchain, queries := parseArgs(moduleLoaderState, ctx, args)
327328
opts := modload.WriteOpts{
328329
DropToolchain: dropToolchain,
329330
}
@@ -333,17 +334,17 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
333334
}
334335
}
335336

336-
r := newResolver(modload.LoaderState, ctx, queries)
337-
r.performLocalQueries(modload.LoaderState, ctx)
338-
r.performPathQueries(modload.LoaderState, ctx)
339-
r.performToolQueries(modload.LoaderState, ctx)
340-
r.performWorkQueries(modload.LoaderState, ctx)
337+
r := newResolver(moduleLoaderState, ctx, queries)
338+
r.performLocalQueries(moduleLoaderState, ctx)
339+
r.performPathQueries(moduleLoaderState, ctx)
340+
r.performToolQueries(moduleLoaderState, ctx)
341+
r.performWorkQueries(moduleLoaderState, ctx)
341342

342343
for {
343-
r.performWildcardQueries(modload.LoaderState, ctx)
344-
r.performPatternAllQueries(modload.LoaderState, ctx)
344+
r.performWildcardQueries(moduleLoaderState, ctx)
345+
r.performPatternAllQueries(moduleLoaderState, ctx)
345346

346-
if changed := r.resolveQueries(modload.LoaderState, ctx, queries); changed {
347+
if changed := r.resolveQueries(moduleLoaderState, ctx, queries); changed {
347348
// 'go get' arguments can be (and often are) package patterns rather than
348349
// (just) modules. A package can be provided by any module with a prefix
349350
// of its import path, and a wildcard can even match packages in modules
@@ -379,20 +380,20 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
379380
//
380381
// - ambiguous import errors.
381382
// TODO(#27899): Try to resolve ambiguous import errors automatically.
382-
upgrades := r.findAndUpgradeImports(modload.LoaderState, ctx, queries)
383-
if changed := r.applyUpgrades(modload.LoaderState, ctx, upgrades); changed {
383+
upgrades := r.findAndUpgradeImports(moduleLoaderState, ctx, queries)
384+
if changed := r.applyUpgrades(moduleLoaderState, ctx, upgrades); changed {
384385
continue
385386
}
386387

387-
r.findMissingWildcards(modload.LoaderState, ctx)
388-
if changed := r.resolveQueries(modload.LoaderState, ctx, r.wildcardQueries); changed {
388+
r.findMissingWildcards(moduleLoaderState, ctx)
389+
if changed := r.resolveQueries(moduleLoaderState, ctx, r.wildcardQueries); changed {
389390
continue
390391
}
391392

392393
break
393394
}
394395

395-
r.checkWildcardVersions(modload.LoaderState, ctx)
396+
r.checkWildcardVersions(moduleLoaderState, ctx)
396397

397398
var pkgPatterns []string
398399
for _, q := range queries {
@@ -403,30 +404,30 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
403404

404405
// If a workspace applies, checkPackageProblems will switch to the workspace
405406
// using modload.EnterWorkspace when doing the final load, and then switch back.
406-
r.checkPackageProblems(modload.LoaderState, ctx, pkgPatterns)
407+
r.checkPackageProblems(moduleLoaderState, ctx, pkgPatterns)
407408

408409
if *getTool {
409-
updateTools(modload.LoaderState, ctx, queries, &opts)
410+
updateTools(moduleLoaderState, ctx, queries, &opts)
410411
}
411412

412413
// Everything succeeded. Update go.mod.
413-
oldReqs := reqsFromGoMod(modload.ModFile(modload.LoaderState))
414+
oldReqs := reqsFromGoMod(modload.ModFile(moduleLoaderState))
414415

415-
if err := modload.WriteGoMod(modload.LoaderState, ctx, opts); err != nil {
416+
if err := modload.WriteGoMod(moduleLoaderState, ctx, opts); err != nil {
416417
// A TooNewError can happen for 'go get go@newversion'
417418
// when all the required modules are old enough
418419
// but the command line is not.
419420
// TODO(bcmills): modload.EditBuildList should catch this instead,
420421
// and then this can be changed to base.Fatal(err).
421-
toolchain.SwitchOrFatal(modload.LoaderState, ctx, err)
422+
toolchain.SwitchOrFatal(moduleLoaderState, ctx, err)
422423
}
423424

424-
newReqs := reqsFromGoMod(modload.ModFile(modload.LoaderState))
425+
newReqs := reqsFromGoMod(modload.ModFile(moduleLoaderState))
425426
r.reportChanges(oldReqs, newReqs)
426427

427-
if gowork := modload.FindGoWork(modload.LoaderState, base.Cwd()); gowork != "" {
428+
if gowork := modload.FindGoWork(moduleLoaderState, base.Cwd()); gowork != "" {
428429
wf, err := modload.ReadWorkFile(gowork)
429-
if err == nil && modload.UpdateWorkGoVersion(wf, modload.LoaderState.MainModules.GoVersion(modload.LoaderState)) {
430+
if err == nil && modload.UpdateWorkGoVersion(wf, moduleLoaderState.MainModules.GoVersion(moduleLoaderState)) {
430431
modload.WriteWorkFile(gowork, wf)
431432
}
432433
}

0 commit comments

Comments
 (0)