cmd/go: make setState work with any State instance

This commit updates the setState function to work with any state
object in preparation for converting it to a method.

This commit is part of the overall effort to eliminate global
modloader state.

Change-Id: I6d485156e7c4c83ff608f941b68e6d928418bd8f
Reviewed-on: https://go-review.googlesource.com/c/go/+/712700
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-10-16 18:06:27 -04:00
parent a420aa221e
commit 386d81151d

View file

@ -81,7 +81,7 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
}
// Reset the state to a clean state.
oldstate := setState(State{})
oldstate := setState(LoaderState, State{})
LoaderState.ForceUseModules = true
// Load in workspace mode.
@ -93,7 +93,7 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
LoaderState.requirements = requirementsFromModFiles(LoaderState, ctx, LoaderState.MainModules.workFile, slices.Collect(maps.Values(LoaderState.MainModules.modFiles)), nil)
return func() {
setState(oldstate)
setState(LoaderState, oldstate)
}, nil
}
@ -378,31 +378,31 @@ func WorkFilePath(loaderstate *State) string {
// Reset clears all the initialized, cached state about the use of modules,
// so that we can start over.
func Reset() {
setState(State{})
setState(LoaderState, State{})
}
func setState(s State) State {
func setState(s *State, new State) State {
oldState := State{
initialized: LoaderState.initialized,
ForceUseModules: LoaderState.ForceUseModules,
RootMode: LoaderState.RootMode,
modRoots: LoaderState.modRoots,
initialized: s.initialized,
ForceUseModules: s.ForceUseModules,
RootMode: s.RootMode,
modRoots: s.modRoots,
modulesEnabled: cfg.ModulesEnabled,
MainModules: LoaderState.MainModules,
requirements: LoaderState.requirements,
MainModules: s.MainModules,
requirements: s.requirements,
}
LoaderState.initialized = s.initialized
LoaderState.ForceUseModules = s.ForceUseModules
LoaderState.RootMode = s.RootMode
LoaderState.modRoots = s.modRoots
cfg.ModulesEnabled = s.modulesEnabled
LoaderState.MainModules = s.MainModules
LoaderState.requirements = s.requirements
LoaderState.workFilePath = s.workFilePath
s.initialized = new.initialized
s.ForceUseModules = new.ForceUseModules
s.RootMode = new.RootMode
s.modRoots = new.modRoots
cfg.ModulesEnabled = new.modulesEnabled
s.MainModules = new.MainModules
s.requirements = new.requirements
s.workFilePath = new.workFilePath
// The modfetch package's global state is used to compute
// the go.sum file, so save and restore it along with the
// modload state.
oldState.modfetchState = modfetch.SetState(s.modfetchState)
oldState.modfetchState = modfetch.SetState(new.modfetchState)
return oldState
}