Skip to content

Commit 6f79265

Browse files
committed
cmd/go: refactor usage of modRoots
This commit refactors usage of the global variable `modRoots` 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 { modRoots -> LoaderState.modRoots }' rf 'add State.RootMode \ // These are primarily used to initialize the MainModules, and should\ // be eventually superseded by them but are still used in cases where\ // the module roots are required but MainModules has not been\ // initialized yet. Set to the modRoots of the main modules.\ // modRoots != nil implies len(modRoots) > 0' rf 'rm modRoots' Change-Id: Ie9e1f3d468cfceee25efefaf945b10492318b079 Reviewed-on: https://go-review.googlesource.com/c/go/+/698059 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 11d5484 commit 6f79265

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,7 @@ var (
5353

5454
// Variables set in Init.
5555
var (
56-
57-
// These are primarily used to initialize the MainModules, and should be
58-
// eventually superseded by them but are still used in cases where the module
59-
// roots are required but MainModules hasn't been initialized yet. Set to
60-
// the modRoots of the main modules.
61-
// modRoots != nil implies len(modRoots) > 0
62-
modRoots []string
63-
gopath string
56+
gopath string
6457
)
6558

6659
// EnterModule resets MainModules and requirements to refer to just this one module.
@@ -70,7 +63,7 @@ func EnterModule(ctx context.Context, enterModroot string) {
7063
workFilePath = "" // Force module mode
7164
modfetch.Reset()
7265

73-
modRoots = []string{enterModroot}
66+
LoaderState.modRoots = []string{enterModroot}
7467
LoadModFile(ctx)
7568
}
7669

@@ -401,15 +394,15 @@ func setState(s State) State {
401394
initialized: LoaderState.initialized,
402395
ForceUseModules: LoaderState.ForceUseModules,
403396
RootMode: LoaderState.RootMode,
404-
modRoots: modRoots,
397+
modRoots: LoaderState.modRoots,
405398
modulesEnabled: cfg.ModulesEnabled,
406399
mainModules: MainModules,
407400
requirements: requirements,
408401
}
409402
LoaderState.initialized = s.initialized
410403
LoaderState.ForceUseModules = s.ForceUseModules
411404
LoaderState.RootMode = s.RootMode
412-
modRoots = s.modRoots
405+
LoaderState.modRoots = s.modRoots
413406
cfg.ModulesEnabled = s.modulesEnabled
414407
MainModules = s.mainModules
415408
requirements = s.requirements
@@ -429,7 +422,13 @@ type State struct {
429422
ForceUseModules bool
430423

431424
// RootMode determines whether a module root is needed.
432-
RootMode Root
425+
RootMode Root
426+
427+
// These are primarily used to initialize the MainModules, and should
428+
// be eventually superseded by them but are still used in cases where
429+
// the module roots are required but MainModules has not been
430+
// initialized yet. Set to the modRoots of the main modules.
431+
// modRoots != nil implies len(modRoots) > 0
433432
modRoots []string
434433
modulesEnabled bool
435434
mainModules *MainModuleSet
@@ -491,14 +490,14 @@ func Init() {
491490
if os.Getenv("GCM_INTERACTIVE") == "" {
492491
os.Setenv("GCM_INTERACTIVE", "never")
493492
}
494-
if modRoots != nil {
493+
if LoaderState.modRoots != nil {
495494
// modRoot set before Init was called ("go mod init" does this).
496495
// No need to search for go.mod.
497496
} else if LoaderState.RootMode == NoRoot {
498497
if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
499498
base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
500499
}
501-
modRoots = nil
500+
LoaderState.modRoots = nil
502501
} else if workFilePath != "" {
503502
// We're in workspace mode, which implies module mode.
504503
if cfg.ModFile != "" {
@@ -531,7 +530,7 @@ func Init() {
531530
return
532531
}
533532
} else {
534-
modRoots = []string{modRoot}
533+
LoaderState.modRoots = []string{modRoot}
535534
}
536535
}
537536
if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
@@ -566,7 +565,7 @@ func Init() {
566565
// be called until the command is installed and flags are parsed. Instead of
567566
// calling Init and Enabled, the main package can call this function.
568567
func WillBeEnabled() bool {
569-
if modRoots != nil || cfg.ModulesEnabled {
568+
if LoaderState.modRoots != nil || cfg.ModulesEnabled {
570569
// Already enabled.
571570
return true
572571
}
@@ -619,7 +618,7 @@ func FindGoMod(wd string) string {
619618
// (usually through MustModRoot).
620619
func Enabled() bool {
621620
Init()
622-
return modRoots != nil || cfg.ModulesEnabled
621+
return LoaderState.modRoots != nil || cfg.ModulesEnabled
623622
}
624623

625624
func VendorDir() string {
@@ -651,7 +650,7 @@ func inWorkspaceMode() bool {
651650
// does not require a main module.
652651
func HasModRoot() bool {
653652
Init()
654-
return modRoots != nil
653+
return LoaderState.modRoots != nil
655654
}
656655

657656
// MustHaveModRoot checks that a main module or main modules are present,
@@ -880,16 +879,16 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
880879
var workFile *modfile.WorkFile
881880
if inWorkspaceMode() {
882881
var err error
883-
workFile, modRoots, err = LoadWorkFile(workFilePath)
882+
workFile, LoaderState.modRoots, err = LoadWorkFile(workFilePath)
884883
if err != nil {
885884
return nil, err
886885
}
887-
for _, modRoot := range modRoots {
886+
for _, modRoot := range LoaderState.modRoots {
888887
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
889888
modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
890889
}
891890
modfetch.GoSumFile = workFilePath + ".sum"
892-
} else if len(modRoots) == 0 {
891+
} else if len(LoaderState.modRoots) == 0 {
893892
// We're in module mode, but not inside a module.
894893
//
895894
// Commands like 'go build', 'go run', 'go list' have no go.mod file to
@@ -908,9 +907,9 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
908907
//
909908
// See golang.org/issue/32027.
910909
} else {
911-
modfetch.GoSumFile = strings.TrimSuffix(modFilePath(modRoots[0]), ".mod") + ".sum"
910+
modfetch.GoSumFile = strings.TrimSuffix(modFilePath(LoaderState.modRoots[0]), ".mod") + ".sum"
912911
}
913-
if len(modRoots) == 0 {
912+
if len(LoaderState.modRoots) == 0 {
914913
// TODO(#49228): Instead of creating a fake module with an empty modroot,
915914
// make MainModules.Len() == 0 mean that we're in module mode but not inside
916915
// any module.
@@ -956,7 +955,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
956955
var mainModules []module.Version
957956
var indices []*modFileIndex
958957
var errs []error
959-
for _, modroot := range modRoots {
958+
for _, modroot := range LoaderState.modRoots {
960959
gomod := modFilePath(modroot)
961960
var fixed bool
962961
data, f, err := ReadModFile(gomod, fixVersion(ctx, &fixed))
@@ -1017,7 +1016,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
10171016
return nil, errors.Join(errs...)
10181017
}
10191018

1020-
MainModules = makeMainModules(mainModules, modRoots, modFiles, indices, workFile)
1019+
MainModules = makeMainModules(mainModules, LoaderState.modRoots, modFiles, indices, workFile)
10211020
setDefaultBuildMod() // possibly enable automatic vendoring
10221021
rs := requirementsFromModFiles(ctx, workFile, modFiles, opts)
10231022

@@ -1120,7 +1119,7 @@ func CheckReservedModulePath(path string) error {
11201119
// packages at multiple versions from the same module).
11211120
func CreateModFile(ctx context.Context, modPath string) {
11221121
modRoot := base.Cwd()
1123-
modRoots = []string{modRoot}
1122+
LoaderState.modRoots = []string{modRoot}
11241123
Init()
11251124
modFilePath := modFilePath(modRoot)
11261125
if _, err := fsys.Stat(modFilePath); err == nil {
@@ -1509,7 +1508,7 @@ func setDefaultBuildMod() {
15091508
cfg.BuildMod = "readonly"
15101509
return
15111510
}
1512-
if modRoots == nil {
1511+
if LoaderState.modRoots == nil {
15131512
if allowMissingModuleImports {
15141513
cfg.BuildMod = "mod"
15151514
} else {
@@ -1518,7 +1517,7 @@ func setDefaultBuildMod() {
15181517
return
15191518
}
15201519

1521-
if len(modRoots) >= 1 {
1520+
if len(LoaderState.modRoots) >= 1 {
15221521
var goVersion string
15231522
var versionSource string
15241523
if inWorkspaceMode() {
@@ -1537,10 +1536,10 @@ func setDefaultBuildMod() {
15371536
if workFilePath != "" {
15381537
vendorDir = filepath.Join(filepath.Dir(workFilePath), "vendor")
15391538
} else {
1540-
if len(modRoots) != 1 {
1541-
panic(fmt.Errorf("outside workspace mode, but have %v modRoots", modRoots))
1539+
if len(LoaderState.modRoots) != 1 {
1540+
panic(fmt.Errorf("outside workspace mode, but have %v modRoots", LoaderState.modRoots))
15421541
}
1543-
vendorDir = filepath.Join(modRoots[0], "vendor")
1542+
vendorDir = filepath.Join(LoaderState.modRoots[0], "vendor")
15441543
}
15451544
if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
15461545
if goVersion != "" {

src/cmd/go/internal/modload/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
271271
case m.IsLocal():
272272
// Evaluate list of file system directories on first iteration.
273273
if m.Dirs == nil {
274-
matchModRoots := modRoots
274+
matchModRoots := LoaderState.modRoots
275275
if opts.MainModule != (module.Version{}) {
276276
matchModRoots = []string{MainModules.ModRoot(opts.MainModule)}
277277
}

src/cmd/go/internal/modload/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ func QueryPattern(ctx context.Context, pattern, query string, current func(strin
716716

717717
var mainModuleMatches []module.Version
718718
for _, mainModule := range MainModules.Versions() {
719-
m := match(mainModule, modRoots, true)
719+
m := match(mainModule, LoaderState.modRoots, true)
720720
if len(m.Pkgs) > 0 {
721721
if query != "upgrade" && query != "patch" {
722722
return nil, nil, &QueryMatchesPackagesInMainModuleError{

0 commit comments

Comments
 (0)