]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make setState work with any State instance
authorIan Alexander <jitsu@google.com>
Thu, 16 Oct 2025 22:06:27 +0000 (18:06 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 19:52:33 +0000 (12:52 -0700)
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>

src/cmd/go/internal/modload/init.go

index 383ea20e3e673a2bffaa1f3bfac47c75ac64503d..34af9796e59ad19bff8ffd0d3dc1992f5bcec3cf 100644 (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,
-       }
-       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
+               MainModules:     s.MainModules,
+               requirements:    s.requirements,
+       }
+       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
 }