From d9e6f95450134f55d35f63c8da7605019cfda1c3 Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Thu, 2 Oct 2025 17:00:18 -0400 Subject: [PATCH] cmd/go: inject State parameter into `workcmd.runSync` This command modifies the call tree starting at `workcmd.runSync` to inject a `State` parameter to every function that is currently using the global `modload.LoaderState` variable. By explicilty passing a `State` parameter, we can begin to eliminate the usage of the global `modload.LoaderState`. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/workcmd rf 'inject modload.LoaderState runSync' cd .. ./rf-cleanup.zsh Change-Id: Ib8a7b332b89762a7463ace53243cae6aa0ffcc2d Reviewed-on: https://go-review.googlesource.com/c/go/+/709987 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob --- src/cmd/go/internal/modload/init.go | 12 ++++++------ src/cmd/go/internal/workcmd/sync.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index ba88713b36e..12f52381b04 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -57,14 +57,14 @@ var ( ) // EnterModule resets MainModules and requirements to refer to just this one module. -func EnterModule(ctx context.Context, enterModroot string) { - LoaderState.MainModules = nil // reset MainModules - LoaderState.requirements = nil - LoaderState.workFilePath = "" // Force module mode +func EnterModule(loaderstate *State, ctx context.Context, enterModroot string) { + loaderstate.MainModules = nil // reset MainModules + loaderstate.requirements = nil + loaderstate.workFilePath = "" // Force module mode modfetch.Reset() - LoaderState.modRoots = []string{enterModroot} - LoadModFile(LoaderState, ctx) + loaderstate.modRoots = []string{enterModroot} + LoadModFile(loaderstate, ctx) } // EnterWorkspace enters workspace mode from module mode, applying the updated requirements to the main diff --git a/src/cmd/go/internal/workcmd/sync.go b/src/cmd/go/internal/workcmd/sync.go index 7f45909de5c..233d12986da 100644 --- a/src/cmd/go/internal/workcmd/sync.go +++ b/src/cmd/go/internal/workcmd/sync.go @@ -104,7 +104,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) { // Use EnterModule to reset the global state in modload to be in // single-module mode using the modroot of m. - modload.EnterModule(ctx, mms.ModRoot(m)) + modload.EnterModule(modload.LoaderState, ctx, mms.ModRoot(m)) // Edit the build list in the same way that 'go get' would if we // requested the relevant module versions explicitly.