]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: refactor usage of `RootMode`
authorIan Alexander <jitsu@google.com>
Wed, 20 Aug 2025 23:34:40 +0000 (19:34 -0400)
committerIan Alexander <jitsu@google.com>
Tue, 7 Oct 2025 16:56:16 +0000 (09:56 -0700)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
13 files changed:
src/cmd/go/internal/load/godebug.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/modcmd/graph.go
src/cmd/go/internal/modcmd/tidy.go
src/cmd/go/internal/modcmd/vendor.go
src/cmd/go/internal/modcmd/verify.go
src/cmd/go/internal/modcmd/why.go
src/cmd/go/internal/modload/import_test.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/run/run.go
src/cmd/go/internal/tool/tool.go
src/cmd/go/internal/toolchain/select.go
src/cmd/go/internal/work/build.go

index 8ea8ffab1aea1fede31061e27272ba2de223992c..ff184384567afe1ff9ce21c59a713a6065c6b27f 100644 (file)
@@ -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,
index 48b2e70d74b75964c2eb580564af136c58357902..27a1fbfff836fa9f425d58c05b9add62535a7d91 100644 (file)
@@ -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")
        }
 
index 4abae33129ab6b936fa11dbd317384ac83cbc028..5f47260e1882925cc085948db0a826bd89271b64 100644 (file)
@@ -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 {
index dde70c6d74181e4e8c12d29a95fb8e17b439db25..0314dcef250d81a7a9bf1d13a08d2e7070bba4b5 100644 (file)
@@ -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 {
index bd3d8d602e2382edc3c26229ac685489f189c3aa..dfea571c0e031762ded0b3096082c5f97d257501 100644 (file)
@@ -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(),
index ecd25d3a40e610b0aea33fd04a46ac3ae2b99e5e..157c920c06732175f5f01de1e9361576515598f6 100644 (file)
@@ -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{}
index 6c4bf8abab17ee1d0a26d6a2d5f1315c6223859b..62a5387ed8841c91629a454668b88889dc3ddac1 100644 (file)
@@ -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{
index eb4f5d64d3a3c7dca78c66d9dfa901530c5dfaa9..f6b8bb90992735188a13be51eed593f3f3d0524f 100644 (file)
@@ -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)
index 264c02ef6db10052fa982ea249a00e34881bec66..58b16a6599dbe349fe4508eec8c7a36a27fed7a0 100644 (file)
@@ -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 {
index 05ea5eaa15eea462bd5333fefb8369e3818d0c0d..d922dcdd66a551d34fb8ac6f0fce48b237434417 100644 (file)
@@ -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 {
index 120ef5339bede00d0a20f7479e449abcc4d785e2..ef25d17b54d8924ad4d53713557e7c6c809f1b23 100644 (file)
@@ -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)
index 8f55076c6282c20efe337512fa61364ee9820626..4f46b19c12167b1758040aee924bdc17be8c943d 100644 (file)
@@ -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()
 
index 21bbab1bf47463e6daf908256ff5777bd991e5c7..adc98f93138007e3f8c8436951302d8739f7f255 100644 (file)
@@ -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()