From d3e11b3f9011a08600a14fdbb94a9ca9da970344 Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Sat, 15 Nov 2025 23:08:02 -0500 Subject: [PATCH] cmd/go/internal/modload: make State.modfetchState a pointer This change aligns modfetch.State with modload.State by using pointer parameters and receivers. [git-generate] cd src/cmd/go/internal/modload sed -i ' s/oldState/old/ s/old := State{/old = \&State{/ s/func (s \*State) setState(new State) State {/func (s *State) setState(new *State) (old *State) {/ s/setState(State{})/setState(NewState())/ ' init.go cd ../modfetch sed -i ' s/oldState = State{/oldState = \&State{/ s/func SetState(newState State) (oldState State) {/func SetState(newState *State) (oldState *State) {/ s/SetState(State{})/SetState(NewState())/ ' fetch.go cd ../modload sed -i ' s/old.modfetchState = modfetch.SetState(new.modfetchState)/_ = modfetch.SetState(\&new.modfetchState)/ ' init.go rf ' # # Prepare to swap the existing modfetchState field for a pointer type # mv State.modfetchState State.modfetchState_ add State:/modfetchState_/+0 modfetchState *modfetch.State # # Update State.setState to set & restore additional values # add State.setState:/s\.requirements,/+0 workFilePath: s.workFilePath, add State.setState:/s\.workFilePath,/+0 modfetchState: s.modfetchState, # # Swap the existing modfetchState field for a pointer type # add init.go:/.* = modfetch.SetState\(.*\)/-0 s.modfetchState = new.modfetchState add init.go:/.* = modfetch.SetState\(.*\)/-0 old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination rm init.go:/_ = modfetch.SetState\(.*\)/ rm State.modfetchState_ ' sed -i ' s/return &State{}/s := new(State)\ns.modfetchState = modfetch.NewState()\nreturn s/ ' init.go go fmt Change-Id: I0602ecf976fd3ee93844e77989291d729ad71595 Reviewed-on: https://go-review.googlesource.com/c/go/+/720900 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI Reviewed-by: Mark Freeman --- src/cmd/go/internal/modfetch/fetch.go | 6 +++--- src/cmd/go/internal/modload/init.go | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) 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