cmd/go: refactor usage of initialized

This commit refactors usage of the global variable `initialized` to
the global LoaderState field of the same name.

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

[git-generate]
cd src/cmd/go/internal/modload
rf 'ex { initialized -> LoaderState.initialized }'
rf 'rm initialized'

Change-Id: I97e35bab00f4c22661670b01b69425fc25efe6df
Reviewed-on: https://go-review.googlesource.com/c/go/+/698056
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
This commit is contained in:
Ian Alexander 2025-08-20 19:16:36 -04:00
parent 1d62e92567
commit 7bfeb43509

View file

@ -60,7 +60,6 @@ var (
// Variables set in Init. // Variables set in Init.
var ( var (
initialized bool
// These are primarily used to initialize the MainModules, and should be // These are primarily used to initialize the MainModules, and should be
// eventually superseded by them but are still used in cases where the module // eventually superseded by them but are still used in cases where the module
@ -406,7 +405,7 @@ func Reset() {
func setState(s State) State { func setState(s State) State {
oldState := State{ oldState := State{
initialized: initialized, initialized: LoaderState.initialized,
forceUseModules: ForceUseModules, forceUseModules: ForceUseModules,
rootMode: RootMode, rootMode: RootMode,
modRoots: modRoots, modRoots: modRoots,
@ -414,7 +413,7 @@ func setState(s State) State {
mainModules: MainModules, mainModules: MainModules,
requirements: requirements, requirements: requirements,
} }
initialized = s.initialized LoaderState.initialized = s.initialized
ForceUseModules = s.forceUseModules ForceUseModules = s.forceUseModules
RootMode = s.rootMode RootMode = s.rootMode
modRoots = s.modRoots modRoots = s.modRoots
@ -450,10 +449,10 @@ var LoaderState = NewState()
// configures the cfg, codehost, load, modfetch, and search packages for use // configures the cfg, codehost, load, modfetch, and search packages for use
// with modules. // with modules.
func Init() { func Init() {
if initialized { if LoaderState.initialized {
return return
} }
initialized = true LoaderState.initialized = true
fips140.Init() fips140.Init()
@ -573,7 +572,7 @@ func WillBeEnabled() bool {
// Already enabled. // Already enabled.
return true return true
} }
if initialized { if LoaderState.initialized {
// Initialized, not enabled. // Initialized, not enabled.
return false return false
} }
@ -640,7 +639,7 @@ func VendorDir() string {
} }
func inWorkspaceMode() bool { func inWorkspaceMode() bool {
if !initialized { if !LoaderState.initialized {
panic("inWorkspaceMode called before modload.Init called") panic("inWorkspaceMode called before modload.Init called")
} }
if !Enabled() { if !Enabled() {
@ -1253,7 +1252,7 @@ func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer {
// This function affects the default cfg.BuildMod when outside of a module, // This function affects the default cfg.BuildMod when outside of a module,
// so it can only be called prior to Init. // so it can only be called prior to Init.
func AllowMissingModuleImports() { func AllowMissingModuleImports() {
if initialized { if LoaderState.initialized {
panic("AllowMissingModuleImports after Init") panic("AllowMissingModuleImports after Init")
} }
allowMissingModuleImports = true allowMissingModuleImports = true