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 <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
This commit is contained in:
Ian Alexander 2025-11-15 23:08:02 -05:00
parent 2f7fd5714f
commit d3e11b3f90
2 changed files with 19 additions and 11 deletions

View file

@ -503,14 +503,14 @@ func (s *State) AddWorkspaceGoSumFile(file string) {
// Reset resets globals in the modfetch package, so previous loads don't affect // Reset resets globals in the modfetch package, so previous loads don't affect
// contents of go.sum files. // contents of go.sum files.
func Reset() { func Reset() {
SetState(State{}) SetState(NewState())
} }
// SetState sets the global state of the modfetch package to the newState, and returns the previous // 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. // 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 // 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. // 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 { if newState.lookupCache == nil {
newState.lookupCache = new(par.Cache[lookupCacheKey, Repo]) newState.lookupCache = new(par.Cache[lookupCacheKey, Repo])
} }
@ -521,7 +521,7 @@ func SetState(newState State) (oldState State) {
goSum.mu.Lock() goSum.mu.Lock()
defer goSum.mu.Unlock() defer goSum.mu.Unlock()
oldState = State{ oldState = &State{
goSumFile: ModuleFetchState.goSumFile, goSumFile: ModuleFetchState.goSumFile,
workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles, workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles,
lookupCache: ModuleFetchState.lookupCache, lookupCache: ModuleFetchState.lookupCache,

View file

@ -80,7 +80,7 @@ func EnterWorkspace(loaderstate *State, ctx context.Context) (exit func(), err e
} }
// Reset the state to a clean state. // Reset the state to a clean state.
oldstate := loaderstate.setState(State{}) oldstate := loaderstate.setState(NewState())
loaderstate.ForceUseModules = true loaderstate.ForceUseModules = true
// Load in workspace mode. // 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, // Reset clears all the initialized, cached state about the use of modules,
// so that we can start over. // so that we can start over.
func (s *State) Reset() { func (s *State) Reset() {
s.setState(State{}) s.setState(NewState())
} }
func (s *State) setState(new State) State { func (s *State) setState(new *State) (old *State) {
oldState := State{ old = &State{
initialized: s.initialized, initialized: s.initialized,
ForceUseModules: s.ForceUseModules, ForceUseModules: s.ForceUseModules,
RootMode: s.RootMode, RootMode: s.RootMode,
@ -397,6 +397,8 @@ func (s *State) setState(new State) State {
modulesEnabled: cfg.ModulesEnabled, modulesEnabled: cfg.ModulesEnabled,
MainModules: s.MainModules, MainModules: s.MainModules,
requirements: s.requirements, requirements: s.requirements,
workFilePath: s.workFilePath,
modfetchState: s.modfetchState,
} }
s.initialized = new.initialized s.initialized = new.initialized
s.ForceUseModules = new.ForceUseModules 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 modfetch package's global state is used to compute
// the go.sum file, so save and restore it along with the // the go.sum file, so save and restore it along with the
// modload state. // modload state.
oldState.modfetchState = modfetch.SetState(new.modfetchState) s.modfetchState = new.modfetchState
return oldState old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination
return old
} }
type State struct { type State struct {
@ -448,10 +452,14 @@ type State struct {
// Set to the path to the go.work file, or "" if workspace mode is // Set to the path to the go.work file, or "" if workspace mode is
// disabled // disabled
workFilePath string 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 // Init determines whether module mode is enabled, locates the root of the
// current module (if any), sets environment variables for Git subprocesses, and // current module (if any), sets environment variables for Git subprocesses, and