From 2e52060084ff170097347457525f0debde91aea9 Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Wed, 20 Aug 2025 19:34:40 -0400 Subject: [PATCH] cmd/go: refactor usage of `RootMode` This commit refactors usage of the global variable `RootMode` to the global LoaderState variable 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 'mv State.rootMode State.RootMode' for dir in load modcmd run tool toolchain work ; do cd ../${dir} rf 'ex { import "cmd/go/internal/modload"; modload.RootMode -> modload.LoaderState.RootMode }' done cd ../modload rf 'ex { RootMode -> LoaderState.RootMode }' rf 'add State.ForceUseModules \ // RootMode determines whether a module root is needed.' rf 'rm RootMode' Change-Id: Ib5e513ee570dfc3b01cc974fe32944e5e391fd82 Reviewed-on: https://go-review.googlesource.com/c/go/+/698058 LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob Reviewed-by: Michael Matloob --- src/cmd/go/internal/load/godebug.go | 2 +- src/cmd/go/internal/load/pkg.go | 2 +- src/cmd/go/internal/modcmd/graph.go | 2 +- src/cmd/go/internal/modcmd/tidy.go | 2 +- src/cmd/go/internal/modcmd/vendor.go | 2 +- src/cmd/go/internal/modcmd/verify.go | 2 +- src/cmd/go/internal/modcmd/why.go | 2 +- src/cmd/go/internal/modload/import_test.go | 6 ++-- src/cmd/go/internal/modload/init.go | 33 +++++++++++----------- src/cmd/go/internal/run/run.go | 2 +- src/cmd/go/internal/tool/tool.go | 2 +- src/cmd/go/internal/toolchain/select.go | 4 +-- src/cmd/go/internal/work/build.go | 2 +- 13 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/cmd/go/internal/load/godebug.go b/src/cmd/go/internal/load/godebug.go index 8ea8ffab1a..ff18438456 100644 --- a/src/cmd/go/internal/load/godebug.go +++ b/src/cmd/go/internal/load/godebug.go @@ -50,7 +50,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu return "" } goVersion := modload.MainModules.GoVersion() - if modload.RootMode == modload.NoRoot && p.Module != nil { + if modload.LoaderState.RootMode == modload.NoRoot && p.Module != nil { // This is go install pkg@version or go run pkg@version. // Use the Go version from the package. // If there isn't one, then assume Go 1.20, diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 48b2e70d74..27a1fbfff8 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -3351,7 +3351,7 @@ func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args if !modload.LoaderState.ForceUseModules { panic("modload.ForceUseModules must be true") } - if modload.RootMode != modload.NoRoot { + if modload.LoaderState.RootMode != modload.NoRoot { panic("modload.RootMode must be NoRoot") } diff --git a/src/cmd/go/internal/modcmd/graph.go b/src/cmd/go/internal/modcmd/graph.go index 4abae33129..5f47260e18 100644 --- a/src/cmd/go/internal/modcmd/graph.go +++ b/src/cmd/go/internal/modcmd/graph.go @@ -58,7 +58,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) { base.Fatalf("go: 'go mod graph' accepts no arguments") } modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NeedRoot + modload.LoaderState.RootMode = modload.NeedRoot goVersion := graphGo.String() if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 { diff --git a/src/cmd/go/internal/modcmd/tidy.go b/src/cmd/go/internal/modcmd/tidy.go index dde70c6d74..0314dcef25 100644 --- a/src/cmd/go/internal/modcmd/tidy.go +++ b/src/cmd/go/internal/modcmd/tidy.go @@ -120,7 +120,7 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) { // that are in 'all' but outside of the main module, we must explicitly // request that their test dependencies be included. modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NeedRoot + modload.LoaderState.RootMode = modload.NeedRoot goVersion := tidyGo.String() if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 { diff --git a/src/cmd/go/internal/modcmd/vendor.go b/src/cmd/go/internal/modcmd/vendor.go index bd3d8d602e..dfea571c0e 100644 --- a/src/cmd/go/internal/modcmd/vendor.go +++ b/src/cmd/go/internal/modcmd/vendor.go @@ -78,7 +78,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) base.Fatalf("go: 'go mod vendor' accepts no arguments") } modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NeedRoot + modload.LoaderState.RootMode = modload.NeedRoot loadOpts := modload.PackageOpts{ Tags: imports.AnyTags(), diff --git a/src/cmd/go/internal/modcmd/verify.go b/src/cmd/go/internal/modcmd/verify.go index ecd25d3a40..157c920c06 100644 --- a/src/cmd/go/internal/modcmd/verify.go +++ b/src/cmd/go/internal/modcmd/verify.go @@ -51,7 +51,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) { base.Fatalf("go: verify takes no arguments") } modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NeedRoot + modload.LoaderState.RootMode = modload.NeedRoot // Only verify up to GOMAXPROCS zips at once. type token struct{} diff --git a/src/cmd/go/internal/modcmd/why.go b/src/cmd/go/internal/modcmd/why.go index 6c4bf8abab..62a5387ed8 100644 --- a/src/cmd/go/internal/modcmd/why.go +++ b/src/cmd/go/internal/modcmd/why.go @@ -65,7 +65,7 @@ func init() { func runWhy(ctx context.Context, cmd *base.Command, args []string) { modload.InitWorkfile() modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NeedRoot + modload.LoaderState.RootMode = modload.NeedRoot modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules loadOpts := modload.PackageOpts{ diff --git a/src/cmd/go/internal/modload/import_test.go b/src/cmd/go/internal/modload/import_test.go index eb4f5d64d3..f6b8bb9099 100644 --- a/src/cmd/go/internal/modload/import_test.go +++ b/src/cmd/go/internal/modload/import_test.go @@ -60,13 +60,13 @@ func TestQueryImport(t *testing.T) { testenv.MustHaveExecPath(t, "git") oldAllowMissingModuleImports := allowMissingModuleImports - oldRootMode := RootMode + oldRootMode := LoaderState.RootMode defer func() { allowMissingModuleImports = oldAllowMissingModuleImports - RootMode = oldRootMode + LoaderState.RootMode = oldRootMode }() allowMissingModuleImports = true - RootMode = NoRoot + LoaderState.RootMode = NoRoot ctx := context.Background() rs := LoadModFile(ctx) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 264c02ef6d..58b16a6599 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -38,9 +38,6 @@ import ( // // TODO(#40775): See if these can be plumbed as explicit parameters. var ( - // RootMode determines whether a module root is needed. - RootMode Root - allowMissingModuleImports bool // ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions @@ -370,7 +367,7 @@ func InitWorkfile() { // It is exported mainly for Go toolchain switching, which must process // the go.work very early at startup. func FindGoWork(wd string) string { - if RootMode == NoRoot { + if LoaderState.RootMode == NoRoot { return "" } @@ -403,7 +400,7 @@ func setState(s State) State { oldState := State{ initialized: LoaderState.initialized, ForceUseModules: LoaderState.ForceUseModules, - rootMode: RootMode, + RootMode: LoaderState.RootMode, modRoots: modRoots, modulesEnabled: cfg.ModulesEnabled, mainModules: MainModules, @@ -411,7 +408,7 @@ func setState(s State) State { } LoaderState.initialized = s.initialized LoaderState.ForceUseModules = s.ForceUseModules - RootMode = s.rootMode + LoaderState.RootMode = s.RootMode modRoots = s.modRoots cfg.ModulesEnabled = s.modulesEnabled MainModules = s.mainModules @@ -430,13 +427,15 @@ type State struct { // ForceUseModules may be set to force modules to be enabled when // GO111MODULE=auto or to report an error when GO111MODULE=off. ForceUseModules bool - rootMode Root - modRoots []string - modulesEnabled bool - mainModules *MainModuleSet - requirements *Requirements - workFilePath string - modfetchState modfetch.State + + // RootMode determines whether a module root is needed. + RootMode Root + modRoots []string + modulesEnabled bool + mainModules *MainModuleSet + requirements *Requirements + workFilePath string + modfetchState modfetch.State } func NewState() *State { return &State{} } @@ -495,7 +494,7 @@ func Init() { if modRoots != nil { // modRoot set before Init was called ("go mod init" does this). // No need to search for go.mod. - } else if RootMode == NoRoot { + } else if LoaderState.RootMode == NoRoot { if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") { base.Fatalf("go: -modfile cannot be used with commands that ignore the current module") } @@ -510,7 +509,7 @@ func Init() { if cfg.ModFile != "" { base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.") } - if RootMode == NeedRoot { + if LoaderState.RootMode == NeedRoot { base.Fatal(ErrNoModRoot) } if !mustUseModules { @@ -525,7 +524,7 @@ func Init() { // It's a bit of a peculiar thing to disallow but quite mysterious // when it happens. See golang.org/issue/26708. fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir()) - if RootMode == NeedRoot { + if LoaderState.RootMode == NeedRoot { base.Fatal(ErrNoModRoot) } if !mustUseModules { @@ -547,7 +546,7 @@ func Init() { gopath = list[0] if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil { fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath) - if RootMode == NeedRoot { + if LoaderState.RootMode == NeedRoot { base.Fatal(ErrNoModRoot) } if !mustUseModules { diff --git a/src/cmd/go/internal/run/run.go b/src/cmd/go/internal/run/run.go index 05ea5eaa15..d922dcdd66 100644 --- a/src/cmd/go/internal/run/run.go +++ b/src/cmd/go/internal/run/run.go @@ -77,7 +77,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) { // before loading packages, since it affects package locations, e.g., // for -race and -msan. modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NoRoot + modload.LoaderState.RootMode = modload.NoRoot modload.AllowMissingModuleImports() modload.Init() } else { diff --git a/src/cmd/go/internal/tool/tool.go b/src/cmd/go/internal/tool/tool.go index 120ef5339b..ef25d17b54 100644 --- a/src/cmd/go/internal/tool/tool.go +++ b/src/cmd/go/internal/tool/tool.go @@ -308,7 +308,7 @@ func buildAndRunBuiltinTool(ctx context.Context, toolName, tool string, args []s // Ignore go.mod and go.work: we don't need them, and we want to be able // to run the tool even if there's an issue with the module or workspace the // user happens to be in. - modload.RootMode = modload.NoRoot + modload.LoaderState.RootMode = modload.NoRoot runFunc := func(b *work.Builder, ctx context.Context, a *work.Action) error { cmdline := str.StringList(builtTool(a), a.Args) diff --git a/src/cmd/go/internal/toolchain/select.go b/src/cmd/go/internal/toolchain/select.go index 8f55076c62..4f46b19c12 100644 --- a/src/cmd/go/internal/toolchain/select.go +++ b/src/cmd/go/internal/toolchain/select.go @@ -354,7 +354,7 @@ func Exec(gotoolchain string) { // Set up modules without an explicit go.mod, to download distribution. modload.Reset() modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NoRoot + modload.LoaderState.RootMode = modload.NoRoot modload.Init() // Download and unpack toolchain module into module cache. @@ -693,7 +693,7 @@ func maybeSwitchForGoInstallVersion(minVers string) { // Set up modules without an explicit go.mod, to download go.mod. modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NoRoot + modload.LoaderState.RootMode = modload.NoRoot modload.Init() defer modload.Reset() diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 21bbab1bf4..adc98f9313 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -860,7 +860,7 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag // See golang.org/issue/40276 for details and rationale. func installOutsideModule(ctx context.Context, args []string) { modload.LoaderState.ForceUseModules = true - modload.RootMode = modload.NoRoot + modload.LoaderState.RootMode = modload.NoRoot modload.AllowMissingModuleImports() modload.Init() BuildInit() -- 2.52.0