From 24416458c21a48d83f34d3c2242d892e002bcd6c Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Wed, 20 Aug 2025 19:12:20 -0400 Subject: [PATCH] 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 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/modload/init.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 498ff7433ea..5192fe0fc8c 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -96,7 +96,7 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) { } // Reset the state to a clean state. - oldstate := setState(state{}) + oldstate := setState(State{}) ForceUseModules = true // Load in workspace mode. @@ -401,11 +401,11 @@ func WorkFilePath() string { // Reset clears all the initialized, cached state about the use of modules, // so that we can start over. func Reset() { - setState(state{}) + setState(State{}) } -func setState(s state) state { - oldState := state{ +func setState(s State) State { + oldState := State{ initialized: initialized, forceUseModules: ForceUseModules, rootMode: RootMode, @@ -429,7 +429,7 @@ func setState(s state) state { return oldState } -type state struct { +type State struct { initialized bool forceUseModules bool rootMode Root @@ -441,6 +441,10 @@ type state struct { modfetchState modfetch.State } +func NewState() *State { return &State{} } + +var LoaderState = NewState() + // Init determines whether module mode is enabled, locates the root of the // current module (if any), sets environment variables for Git subprocesses, and // configures the cfg, codehost, load, modfetch, and search packages for use