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 <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Alexander 2025-10-02 17:00:18 -04:00
parent 9769a61e64
commit d9e6f95450
2 changed files with 7 additions and 7 deletions

View file

@ -57,14 +57,14 @@ var (
) )
// EnterModule resets MainModules and requirements to refer to just this one module. // EnterModule resets MainModules and requirements to refer to just this one module.
func EnterModule(ctx context.Context, enterModroot string) { func EnterModule(loaderstate *State, ctx context.Context, enterModroot string) {
LoaderState.MainModules = nil // reset MainModules loaderstate.MainModules = nil // reset MainModules
LoaderState.requirements = nil loaderstate.requirements = nil
LoaderState.workFilePath = "" // Force module mode loaderstate.workFilePath = "" // Force module mode
modfetch.Reset() modfetch.Reset()
LoaderState.modRoots = []string{enterModroot} loaderstate.modRoots = []string{enterModroot}
LoadModFile(LoaderState, ctx) LoadModFile(loaderstate, ctx)
} }
// EnterWorkspace enters workspace mode from module mode, applying the updated requirements to the main // EnterWorkspace enters workspace mode from module mode, applying the updated requirements to the main

View file

@ -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 // Use EnterModule to reset the global state in modload to be in
// single-module mode using the modroot of m. // 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 // Edit the build list in the same way that 'go get' would if we
// requested the relevant module versions explicitly. // requested the relevant module versions explicitly.