cmd/go: export type State

Export the type `State` and add global variable `LoaderState` in
preparation for refactoring usage of other global variables in the
modload package.

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

[git-generate]
cd src/cmd/go/internal/modload
rf 'mv state State'
rf 'add State func NewState() *State { return &State{} }'
rf 'add init.go:/NewState/+0 var LoaderState = NewState()'

Change-Id: I0ec6199ba3e05927bec12f11a60383d1b51b111a
Reviewed-on: https://go-review.googlesource.com/c/go/+/698055
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-08-20 19:12:20 -04:00
parent c2fb15164b
commit 24416458c2

View file

@ -96,7 +96,7 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
} }
// Reset the state to a clean state. // Reset the state to a clean state.
oldstate := setState(state{}) oldstate := setState(State{})
ForceUseModules = true ForceUseModules = true
// Load in workspace mode. // Load in workspace mode.
@ -401,11 +401,11 @@ func WorkFilePath() 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 Reset() { func Reset() {
setState(state{}) setState(State{})
} }
func setState(s state) state { func setState(s State) State {
oldState := state{ oldState := State{
initialized: initialized, initialized: initialized,
forceUseModules: ForceUseModules, forceUseModules: ForceUseModules,
rootMode: RootMode, rootMode: RootMode,
@ -429,7 +429,7 @@ func setState(s state) state {
return oldState return oldState
} }
type state struct { type State struct {
initialized bool initialized bool
forceUseModules bool forceUseModules bool
rootMode Root rootMode Root
@ -441,6 +441,10 @@ type state struct {
modfetchState modfetch.State modfetchState modfetch.State
} }
func NewState() *State { return &State{} }
var LoaderState = NewState()
// 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
// configures the cfg, codehost, load, modfetch, and search packages for use // configures the cfg, codehost, load, modfetch, and search packages for use