53
53
54
54
// Variables set in Init.
55
55
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
64
57
)
65
58
66
59
// EnterModule resets MainModules and requirements to refer to just this one module.
@@ -70,7 +63,7 @@ func EnterModule(ctx context.Context, enterModroot string) {
70
63
workFilePath = "" // Force module mode
71
64
modfetch .Reset ()
72
65
73
- modRoots = []string {enterModroot }
66
+ LoaderState . modRoots = []string {enterModroot }
74
67
LoadModFile (ctx )
75
68
}
76
69
@@ -401,15 +394,15 @@ func setState(s State) State {
401
394
initialized : LoaderState .initialized ,
402
395
ForceUseModules : LoaderState .ForceUseModules ,
403
396
RootMode : LoaderState .RootMode ,
404
- modRoots : modRoots ,
397
+ modRoots : LoaderState . modRoots ,
405
398
modulesEnabled : cfg .ModulesEnabled ,
406
399
mainModules : MainModules ,
407
400
requirements : requirements ,
408
401
}
409
402
LoaderState .initialized = s .initialized
410
403
LoaderState .ForceUseModules = s .ForceUseModules
411
404
LoaderState .RootMode = s .RootMode
412
- modRoots = s .modRoots
405
+ LoaderState . modRoots = s .modRoots
413
406
cfg .ModulesEnabled = s .modulesEnabled
414
407
MainModules = s .mainModules
415
408
requirements = s .requirements
@@ -429,7 +422,13 @@ type State struct {
429
422
ForceUseModules bool
430
423
431
424
// 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
433
432
modRoots []string
434
433
modulesEnabled bool
435
434
mainModules * MainModuleSet
@@ -491,14 +490,14 @@ func Init() {
491
490
if os .Getenv ("GCM_INTERACTIVE" ) == "" {
492
491
os .Setenv ("GCM_INTERACTIVE" , "never" )
493
492
}
494
- if modRoots != nil {
493
+ if LoaderState . modRoots != nil {
495
494
// modRoot set before Init was called ("go mod init" does this).
496
495
// No need to search for go.mod.
497
496
} else if LoaderState .RootMode == NoRoot {
498
497
if cfg .ModFile != "" && ! base .InGOFLAGS ("-modfile" ) {
499
498
base .Fatalf ("go: -modfile cannot be used with commands that ignore the current module" )
500
499
}
501
- modRoots = nil
500
+ LoaderState . modRoots = nil
502
501
} else if workFilePath != "" {
503
502
// We're in workspace mode, which implies module mode.
504
503
if cfg .ModFile != "" {
@@ -531,7 +530,7 @@ func Init() {
531
530
return
532
531
}
533
532
} else {
534
- modRoots = []string {modRoot }
533
+ LoaderState . modRoots = []string {modRoot }
535
534
}
536
535
}
537
536
if cfg .ModFile != "" && ! strings .HasSuffix (cfg .ModFile , ".mod" ) {
@@ -566,7 +565,7 @@ func Init() {
566
565
// be called until the command is installed and flags are parsed. Instead of
567
566
// calling Init and Enabled, the main package can call this function.
568
567
func WillBeEnabled () bool {
569
- if modRoots != nil || cfg .ModulesEnabled {
568
+ if LoaderState . modRoots != nil || cfg .ModulesEnabled {
570
569
// Already enabled.
571
570
return true
572
571
}
@@ -619,7 +618,7 @@ func FindGoMod(wd string) string {
619
618
// (usually through MustModRoot).
620
619
func Enabled () bool {
621
620
Init ()
622
- return modRoots != nil || cfg .ModulesEnabled
621
+ return LoaderState . modRoots != nil || cfg .ModulesEnabled
623
622
}
624
623
625
624
func VendorDir () string {
@@ -651,7 +650,7 @@ func inWorkspaceMode() bool {
651
650
// does not require a main module.
652
651
func HasModRoot () bool {
653
652
Init ()
654
- return modRoots != nil
653
+ return LoaderState . modRoots != nil
655
654
}
656
655
657
656
// MustHaveModRoot checks that a main module or main modules are present,
@@ -880,16 +879,16 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
880
879
var workFile * modfile.WorkFile
881
880
if inWorkspaceMode () {
882
881
var err error
883
- workFile , modRoots , err = LoadWorkFile (workFilePath )
882
+ workFile , LoaderState . modRoots , err = LoadWorkFile (workFilePath )
884
883
if err != nil {
885
884
return nil , err
886
885
}
887
- for _ , modRoot := range modRoots {
886
+ for _ , modRoot := range LoaderState . modRoots {
888
887
sumFile := strings .TrimSuffix (modFilePath (modRoot ), ".mod" ) + ".sum"
889
888
modfetch .WorkspaceGoSumFiles = append (modfetch .WorkspaceGoSumFiles , sumFile )
890
889
}
891
890
modfetch .GoSumFile = workFilePath + ".sum"
892
- } else if len (modRoots ) == 0 {
891
+ } else if len (LoaderState . modRoots ) == 0 {
893
892
// We're in module mode, but not inside a module.
894
893
//
895
894
// 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)
908
907
//
909
908
// See golang.org/issue/32027.
910
909
} else {
911
- modfetch .GoSumFile = strings .TrimSuffix (modFilePath (modRoots [0 ]), ".mod" ) + ".sum"
910
+ modfetch .GoSumFile = strings .TrimSuffix (modFilePath (LoaderState . modRoots [0 ]), ".mod" ) + ".sum"
912
911
}
913
- if len (modRoots ) == 0 {
912
+ if len (LoaderState . modRoots ) == 0 {
914
913
// TODO(#49228): Instead of creating a fake module with an empty modroot,
915
914
// make MainModules.Len() == 0 mean that we're in module mode but not inside
916
915
// any module.
@@ -956,7 +955,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
956
955
var mainModules []module.Version
957
956
var indices []* modFileIndex
958
957
var errs []error
959
- for _ , modroot := range modRoots {
958
+ for _ , modroot := range LoaderState . modRoots {
960
959
gomod := modFilePath (modroot )
961
960
var fixed bool
962
961
data , f , err := ReadModFile (gomod , fixVersion (ctx , & fixed ))
@@ -1017,7 +1016,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
1017
1016
return nil , errors .Join (errs ... )
1018
1017
}
1019
1018
1020
- MainModules = makeMainModules (mainModules , modRoots , modFiles , indices , workFile )
1019
+ MainModules = makeMainModules (mainModules , LoaderState . modRoots , modFiles , indices , workFile )
1021
1020
setDefaultBuildMod () // possibly enable automatic vendoring
1022
1021
rs := requirementsFromModFiles (ctx , workFile , modFiles , opts )
1023
1022
@@ -1120,7 +1119,7 @@ func CheckReservedModulePath(path string) error {
1120
1119
// packages at multiple versions from the same module).
1121
1120
func CreateModFile (ctx context.Context , modPath string ) {
1122
1121
modRoot := base .Cwd ()
1123
- modRoots = []string {modRoot }
1122
+ LoaderState . modRoots = []string {modRoot }
1124
1123
Init ()
1125
1124
modFilePath := modFilePath (modRoot )
1126
1125
if _ , err := fsys .Stat (modFilePath ); err == nil {
@@ -1509,7 +1508,7 @@ func setDefaultBuildMod() {
1509
1508
cfg .BuildMod = "readonly"
1510
1509
return
1511
1510
}
1512
- if modRoots == nil {
1511
+ if LoaderState . modRoots == nil {
1513
1512
if allowMissingModuleImports {
1514
1513
cfg .BuildMod = "mod"
1515
1514
} else {
@@ -1518,7 +1517,7 @@ func setDefaultBuildMod() {
1518
1517
return
1519
1518
}
1520
1519
1521
- if len (modRoots ) >= 1 {
1520
+ if len (LoaderState . modRoots ) >= 1 {
1522
1521
var goVersion string
1523
1522
var versionSource string
1524
1523
if inWorkspaceMode () {
@@ -1537,10 +1536,10 @@ func setDefaultBuildMod() {
1537
1536
if workFilePath != "" {
1538
1537
vendorDir = filepath .Join (filepath .Dir (workFilePath ), "vendor" )
1539
1538
} 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 ))
1542
1541
}
1543
- vendorDir = filepath .Join (modRoots [0 ], "vendor" )
1542
+ vendorDir = filepath .Join (LoaderState . modRoots [0 ], "vendor" )
1544
1543
}
1545
1544
if fi , err := fsys .Stat (vendorDir ); err == nil && fi .IsDir () {
1546
1545
if goVersion != "" {
0 commit comments