Skip to content

Commit 2e52060

Browse files
committed
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 <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent f86ddb5 commit 2e52060

File tree

13 files changed

+31
-32
lines changed

13 files changed

+31
-32
lines changed

src/cmd/go/internal/load/godebug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu
5050
return ""
5151
}
5252
goVersion := modload.MainModules.GoVersion()
53-
if modload.RootMode == modload.NoRoot && p.Module != nil {
53+
if modload.LoaderState.RootMode == modload.NoRoot && p.Module != nil {
5454
// This is go install pkg@version or go run pkg@version.
5555
// Use the Go version from the package.
5656
// If there isn't one, then assume Go 1.20,

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3351,7 +3351,7 @@ func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args
33513351
if !modload.LoaderState.ForceUseModules {
33523352
panic("modload.ForceUseModules must be true")
33533353
}
3354-
if modload.RootMode != modload.NoRoot {
3354+
if modload.LoaderState.RootMode != modload.NoRoot {
33553355
panic("modload.RootMode must be NoRoot")
33563356
}
33573357

src/cmd/go/internal/modcmd/graph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) {
5858
base.Fatalf("go: 'go mod graph' accepts no arguments")
5959
}
6060
modload.LoaderState.ForceUseModules = true
61-
modload.RootMode = modload.NeedRoot
61+
modload.LoaderState.RootMode = modload.NeedRoot
6262

6363
goVersion := graphGo.String()
6464
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
120120
// that are in 'all' but outside of the main module, we must explicitly
121121
// request that their test dependencies be included.
122122
modload.LoaderState.ForceUseModules = true
123-
modload.RootMode = modload.NeedRoot
123+
modload.LoaderState.RootMode = modload.NeedRoot
124124

125125
goVersion := tidyGo.String()
126126
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
7878
base.Fatalf("go: 'go mod vendor' accepts no arguments")
7979
}
8080
modload.LoaderState.ForceUseModules = true
81-
modload.RootMode = modload.NeedRoot
81+
modload.LoaderState.RootMode = modload.NeedRoot
8282

8383
loadOpts := modload.PackageOpts{
8484
Tags: imports.AnyTags(),

src/cmd/go/internal/modcmd/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
5151
base.Fatalf("go: verify takes no arguments")
5252
}
5353
modload.LoaderState.ForceUseModules = true
54-
modload.RootMode = modload.NeedRoot
54+
modload.LoaderState.RootMode = modload.NeedRoot
5555

5656
// Only verify up to GOMAXPROCS zips at once.
5757
type token struct{}

src/cmd/go/internal/modcmd/why.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func init() {
6565
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
6666
modload.InitWorkfile()
6767
modload.LoaderState.ForceUseModules = true
68-
modload.RootMode = modload.NeedRoot
68+
modload.LoaderState.RootMode = modload.NeedRoot
6969
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules
7070

7171
loadOpts := modload.PackageOpts{

src/cmd/go/internal/modload/import_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ func TestQueryImport(t *testing.T) {
6060
testenv.MustHaveExecPath(t, "git")
6161

6262
oldAllowMissingModuleImports := allowMissingModuleImports
63-
oldRootMode := RootMode
63+
oldRootMode := LoaderState.RootMode
6464
defer func() {
6565
allowMissingModuleImports = oldAllowMissingModuleImports
66-
RootMode = oldRootMode
66+
LoaderState.RootMode = oldRootMode
6767
}()
6868
allowMissingModuleImports = true
69-
RootMode = NoRoot
69+
LoaderState.RootMode = NoRoot
7070

7171
ctx := context.Background()
7272
rs := LoadModFile(ctx)

src/cmd/go/internal/modload/init.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ import (
3838
//
3939
// TODO(#40775): See if these can be plumbed as explicit parameters.
4040
var (
41-
// RootMode determines whether a module root is needed.
42-
RootMode Root
43-
4441
allowMissingModuleImports bool
4542

4643
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
@@ -370,7 +367,7 @@ func InitWorkfile() {
370367
// It is exported mainly for Go toolchain switching, which must process
371368
// the go.work very early at startup.
372369
func FindGoWork(wd string) string {
373-
if RootMode == NoRoot {
370+
if LoaderState.RootMode == NoRoot {
374371
return ""
375372
}
376373

@@ -403,15 +400,15 @@ func setState(s State) State {
403400
oldState := State{
404401
initialized: LoaderState.initialized,
405402
ForceUseModules: LoaderState.ForceUseModules,
406-
rootMode: RootMode,
403+
RootMode: LoaderState.RootMode,
407404
modRoots: modRoots,
408405
modulesEnabled: cfg.ModulesEnabled,
409406
mainModules: MainModules,
410407
requirements: requirements,
411408
}
412409
LoaderState.initialized = s.initialized
413410
LoaderState.ForceUseModules = s.ForceUseModules
414-
RootMode = s.rootMode
411+
LoaderState.RootMode = s.RootMode
415412
modRoots = s.modRoots
416413
cfg.ModulesEnabled = s.modulesEnabled
417414
MainModules = s.mainModules
@@ -430,13 +427,15 @@ type State struct {
430427
// ForceUseModules may be set to force modules to be enabled when
431428
// GO111MODULE=auto or to report an error when GO111MODULE=off.
432429
ForceUseModules bool
433-
rootMode Root
434-
modRoots []string
435-
modulesEnabled bool
436-
mainModules *MainModuleSet
437-
requirements *Requirements
438-
workFilePath string
439-
modfetchState modfetch.State
430+
431+
// RootMode determines whether a module root is needed.
432+
RootMode Root
433+
modRoots []string
434+
modulesEnabled bool
435+
mainModules *MainModuleSet
436+
requirements *Requirements
437+
workFilePath string
438+
modfetchState modfetch.State
440439
}
441440

442441
func NewState() *State { return &State{} }
@@ -495,7 +494,7 @@ func Init() {
495494
if modRoots != nil {
496495
// modRoot set before Init was called ("go mod init" does this).
497496
// No need to search for go.mod.
498-
} else if RootMode == NoRoot {
497+
} else if LoaderState.RootMode == NoRoot {
499498
if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
500499
base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
501500
}
@@ -510,7 +509,7 @@ func Init() {
510509
if cfg.ModFile != "" {
511510
base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
512511
}
513-
if RootMode == NeedRoot {
512+
if LoaderState.RootMode == NeedRoot {
514513
base.Fatal(ErrNoModRoot)
515514
}
516515
if !mustUseModules {
@@ -525,7 +524,7 @@ func Init() {
525524
// It's a bit of a peculiar thing to disallow but quite mysterious
526525
// when it happens. See golang.org/issue/26708.
527526
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
528-
if RootMode == NeedRoot {
527+
if LoaderState.RootMode == NeedRoot {
529528
base.Fatal(ErrNoModRoot)
530529
}
531530
if !mustUseModules {
@@ -547,7 +546,7 @@ func Init() {
547546
gopath = list[0]
548547
if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
549548
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
550-
if RootMode == NeedRoot {
549+
if LoaderState.RootMode == NeedRoot {
551550
base.Fatal(ErrNoModRoot)
552551
}
553552
if !mustUseModules {

src/cmd/go/internal/run/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
7777
// before loading packages, since it affects package locations, e.g.,
7878
// for -race and -msan.
7979
modload.LoaderState.ForceUseModules = true
80-
modload.RootMode = modload.NoRoot
80+
modload.LoaderState.RootMode = modload.NoRoot
8181
modload.AllowMissingModuleImports()
8282
modload.Init()
8383
} else {

0 commit comments

Comments
 (0)