From 7bfeb43509acbe75ce8e1a14c60bffe597e46813 Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Wed, 20 Aug 2025 19:16:36 -0400 Subject: [PATCH] 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 Reviewed-by: Michael Matloob Reviewed-by: Michael Matloob --- src/cmd/go/internal/modload/init.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 5192fe0fc8..d7d532ec94 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -60,7 +60,6 @@ var ( // Variables set in Init. var ( - initialized bool // These are primarily used to initialize the MainModules, and should be // eventually superseded by them but are still used in cases where the module @@ -406,7 +405,7 @@ func Reset() { func setState(s State) State { oldState := State{ - initialized: initialized, + initialized: LoaderState.initialized, forceUseModules: ForceUseModules, rootMode: RootMode, modRoots: modRoots, @@ -414,7 +413,7 @@ func setState(s State) State { mainModules: MainModules, requirements: requirements, } - initialized = s.initialized + LoaderState.initialized = s.initialized ForceUseModules = s.forceUseModules RootMode = s.rootMode modRoots = s.modRoots @@ -450,10 +449,10 @@ var LoaderState = NewState() // configures the cfg, codehost, load, modfetch, and search packages for use // with modules. func Init() { - if initialized { + if LoaderState.initialized { return } - initialized = true + LoaderState.initialized = true fips140.Init() @@ -573,7 +572,7 @@ func WillBeEnabled() bool { // Already enabled. return true } - if initialized { + if LoaderState.initialized { // Initialized, not enabled. return false } @@ -640,7 +639,7 @@ func VendorDir() string { } func inWorkspaceMode() bool { - if !initialized { + if !LoaderState.initialized { panic("inWorkspaceMode called before modload.Init called") } 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, // so it can only be called prior to Init. func AllowMissingModuleImports() { - if initialized { + if LoaderState.initialized { panic("AllowMissingModuleImports after Init") } allowMissingModuleImports = true -- 2.52.0