60
60
func EnterModule (ctx context.Context , enterModroot string ) {
61
61
LoaderState .MainModules = nil // reset MainModules
62
62
LoaderState .requirements = nil
63
- workFilePath = "" // Force module mode
63
+ LoaderState . workFilePath = "" // Force module mode
64
64
modfetch .Reset ()
65
65
66
66
LoaderState .modRoots = []string {enterModroot }
@@ -97,12 +97,6 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
97
97
}, nil
98
98
}
99
99
100
- // Variable set in InitWorkfile
101
- var (
102
- // Set to the path to the go.work file, or "" if workspace mode is disabled.
103
- workFilePath string
104
- )
105
-
106
100
type MainModuleSet struct {
107
101
// versions are the module.Version values of each of the main modules.
108
102
// For each of them, the Path fields are ordinary module paths and the Version
@@ -349,7 +343,7 @@ func InitWorkfile() {
349
343
if err := fsys .Init (); err != nil {
350
344
base .Fatal (err )
351
345
}
352
- workFilePath = FindGoWork (base .Cwd ())
346
+ LoaderState . workFilePath = FindGoWork (base .Cwd ())
353
347
}
354
348
355
349
// FindGoWork returns the name of the go.work file for this command,
@@ -378,7 +372,7 @@ func FindGoWork(wd string) string {
378
372
// WorkFilePath returns the absolute path of the go.work file, or "" if not in
379
373
// workspace mode. WorkFilePath must be called after InitWorkfile.
380
374
func WorkFilePath () string {
381
- return workFilePath
375
+ return LoaderState . workFilePath
382
376
}
383
377
384
378
// Reset clears all the initialized, cached state about the use of modules,
@@ -404,7 +398,7 @@ func setState(s State) State {
404
398
cfg .ModulesEnabled = s .modulesEnabled
405
399
LoaderState .MainModules = s .MainModules
406
400
LoaderState .requirements = s .requirements
407
- workFilePath = s .workFilePath
401
+ LoaderState . workFilePath = s .workFilePath
408
402
// The modfetch package's global state is used to compute
409
403
// the go.sum file, so save and restore it along with the
410
404
// modload state.
@@ -441,7 +435,10 @@ type State struct {
441
435
// commitRequirements functions. All other functions that need or
442
436
// produce a *Requirements should accept and/or return an explicit
443
437
// parameter.
444
- requirements * Requirements
438
+ requirements * Requirements
439
+
440
+ // Set to the path to the go.work file, or "" if workspace mode is
441
+ // disabled
445
442
workFilePath string
446
443
modfetchState modfetch.State
447
444
}
@@ -507,7 +504,7 @@ func Init() {
507
504
base .Fatalf ("go: -modfile cannot be used with commands that ignore the current module" )
508
505
}
509
506
LoaderState .modRoots = nil
510
- } else if workFilePath != "" {
507
+ } else if LoaderState . workFilePath != "" {
511
508
// We're in workspace mode, which implies module mode.
512
509
if cfg .ModFile != "" {
513
510
base .Fatalf ("go: -modfile cannot be used in workspace mode" )
@@ -651,7 +648,7 @@ func inWorkspaceMode() bool {
651
648
if ! Enabled () {
652
649
return false
653
650
}
654
- return workFilePath != ""
651
+ return LoaderState . workFilePath != ""
655
652
}
656
653
657
654
// HasModRoot reports whether a main module or main modules are present.
@@ -888,15 +885,15 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
888
885
var workFile * modfile.WorkFile
889
886
if inWorkspaceMode () {
890
887
var err error
891
- workFile , LoaderState .modRoots , err = LoadWorkFile (workFilePath )
888
+ workFile , LoaderState .modRoots , err = LoadWorkFile (LoaderState . workFilePath )
892
889
if err != nil {
893
890
return nil , err
894
891
}
895
892
for _ , modRoot := range LoaderState .modRoots {
896
893
sumFile := strings .TrimSuffix (modFilePath (modRoot ), ".mod" ) + ".sum"
897
894
modfetch .WorkspaceGoSumFiles = append (modfetch .WorkspaceGoSumFiles , sumFile )
898
895
}
899
- modfetch .GoSumFile = workFilePath + ".sum"
896
+ modfetch .GoSumFile = LoaderState . workFilePath + ".sum"
900
897
} else if len (LoaderState .modRoots ) == 0 {
901
898
// We're in module mode, but not inside a module.
902
899
//
@@ -1542,8 +1539,8 @@ func setDefaultBuildMod() {
1542
1539
}
1543
1540
}
1544
1541
vendorDir := ""
1545
- if workFilePath != "" {
1546
- vendorDir = filepath .Join (filepath .Dir (workFilePath ), "vendor" )
1542
+ if LoaderState . workFilePath != "" {
1543
+ vendorDir = filepath .Join (filepath .Dir (LoaderState . workFilePath ), "vendor" )
1547
1544
} else {
1548
1545
if len (LoaderState .modRoots ) != 1 {
1549
1546
panic (fmt .Errorf ("outside workspace mode, but have %v modRoots" , LoaderState .modRoots ))
0 commit comments