diff --git a/src/cmd/go/internal/modfetch/fetch.go b/src/cmd/go/internal/modfetch/fetch.go index 87ea829180f..8a17a374d0f 100644 --- a/src/cmd/go/internal/modfetch/fetch.go +++ b/src/cmd/go/internal/modfetch/fetch.go @@ -503,14 +503,14 @@ func (s *State) AddWorkspaceGoSumFile(file string) { // Reset resets globals in the modfetch package, so previous loads don't affect // contents of go.sum files. func Reset() { - SetState(State{}) + SetState(NewState()) } // SetState sets the global state of the modfetch package to the newState, and returns the previous // global state. newState should have been returned by SetState, or be an empty State. // There should be no concurrent calls to any of the exported functions of this package with // a call to SetState because it will modify the global state in a non-thread-safe way. -func SetState(newState State) (oldState State) { +func SetState(newState *State) (oldState *State) { if newState.lookupCache == nil { newState.lookupCache = new(par.Cache[lookupCacheKey, Repo]) } @@ -521,7 +521,7 @@ func SetState(newState State) (oldState State) { goSum.mu.Lock() defer goSum.mu.Unlock() - oldState = State{ + oldState = &State{ goSumFile: ModuleFetchState.goSumFile, workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles, lookupCache: ModuleFetchState.lookupCache, diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index ad7b08e062a..4680ea427e9 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -80,7 +80,7 @@ func EnterWorkspace(loaderstate *State, ctx context.Context) (exit func(), err e } // Reset the state to a clean state. - oldstate := loaderstate.setState(State{}) + oldstate := loaderstate.setState(NewState()) loaderstate.ForceUseModules = true // Load in workspace mode. @@ -385,11 +385,11 @@ func WorkFilePath(loaderstate *State) string { // Reset clears all the initialized, cached state about the use of modules, // so that we can start over. func (s *State) Reset() { - s.setState(State{}) + s.setState(NewState()) } -func (s *State) setState(new State) State { - oldState := State{ +func (s *State) setState(new *State) (old *State) { + old = &State{ initialized: s.initialized, ForceUseModules: s.ForceUseModules, RootMode: s.RootMode, @@ -397,6 +397,8 @@ func (s *State) setState(new State) State { modulesEnabled: cfg.ModulesEnabled, MainModules: s.MainModules, requirements: s.requirements, + workFilePath: s.workFilePath, + modfetchState: s.modfetchState, } s.initialized = new.initialized s.ForceUseModules = new.ForceUseModules @@ -409,8 +411,10 @@ func (s *State) setState(new State) State { // 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(new.modfetchState) - return oldState + s.modfetchState = new.modfetchState + old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination + + return old } type State struct { @@ -448,10 +452,14 @@ type State struct { // Set to the path to the go.work file, or "" if workspace mode is // disabled workFilePath string - modfetchState modfetch.State + modfetchState *modfetch.State } -func NewState() *State { return &State{} } +func NewState() *State { + s := new(State) + s.modfetchState = modfetch.NewState() + return s +} // Init determines whether module mode is enabled, locates the root of the // current module (if any), sets environment variables for Git subprocesses, and