Skip to content

Commit 78b4303

Browse files
committed
cmd/go: refactor usage of workFilePath
This commit refactors usage of the global variable `workFilePath` 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 { workFilePath -> LoaderState.workFilePath }' rf 'add State.requirements \ // Set to the path to the go.work file, or "" if workspace mode is\ // disabled' rf 'rm workFilePath' Change-Id: I53cdbc3cc619914421513db74a74a04ab10b3e33 Reviewed-on: https://go-review.googlesource.com/c/go/+/698062 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent bb1ca7a commit 78b4303

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/cmd/go/internal/modload/buildlist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func newRequirements(pruning modPruning, rootModules []module.Version, direct ma
108108
mustHaveGoRoot(rootModules)
109109

110110
if pruning != workspace {
111-
if workFilePath != "" {
111+
if LoaderState.workFilePath != "" {
112112
panic("in workspace mode, but pruning is not workspace in newRequirements")
113113
}
114114
}
115115

116116
if pruning != workspace {
117-
if workFilePath != "" {
117+
if LoaderState.workFilePath != "" {
118118
panic("in workspace mode, but pruning is not workspace in newRequirements")
119119
}
120120
for i, m := range rootModules {

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var (
6060
func EnterModule(ctx context.Context, enterModroot string) {
6161
LoaderState.MainModules = nil // reset MainModules
6262
LoaderState.requirements = nil
63-
workFilePath = "" // Force module mode
63+
LoaderState.workFilePath = "" // Force module mode
6464
modfetch.Reset()
6565

6666
LoaderState.modRoots = []string{enterModroot}
@@ -97,12 +97,6 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
9797
}, nil
9898
}
9999

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-
106100
type MainModuleSet struct {
107101
// versions are the module.Version values of each of the main modules.
108102
// For each of them, the Path fields are ordinary module paths and the Version
@@ -349,7 +343,7 @@ func InitWorkfile() {
349343
if err := fsys.Init(); err != nil {
350344
base.Fatal(err)
351345
}
352-
workFilePath = FindGoWork(base.Cwd())
346+
LoaderState.workFilePath = FindGoWork(base.Cwd())
353347
}
354348

355349
// FindGoWork returns the name of the go.work file for this command,
@@ -378,7 +372,7 @@ func FindGoWork(wd string) string {
378372
// WorkFilePath returns the absolute path of the go.work file, or "" if not in
379373
// workspace mode. WorkFilePath must be called after InitWorkfile.
380374
func WorkFilePath() string {
381-
return workFilePath
375+
return LoaderState.workFilePath
382376
}
383377

384378
// Reset clears all the initialized, cached state about the use of modules,
@@ -404,7 +398,7 @@ func setState(s State) State {
404398
cfg.ModulesEnabled = s.modulesEnabled
405399
LoaderState.MainModules = s.MainModules
406400
LoaderState.requirements = s.requirements
407-
workFilePath = s.workFilePath
401+
LoaderState.workFilePath = s.workFilePath
408402
// The modfetch package's global state is used to compute
409403
// the go.sum file, so save and restore it along with the
410404
// modload state.
@@ -441,7 +435,10 @@ type State struct {
441435
// commitRequirements functions. All other functions that need or
442436
// produce a *Requirements should accept and/or return an explicit
443437
// 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
445442
workFilePath string
446443
modfetchState modfetch.State
447444
}
@@ -507,7 +504,7 @@ func Init() {
507504
base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
508505
}
509506
LoaderState.modRoots = nil
510-
} else if workFilePath != "" {
507+
} else if LoaderState.workFilePath != "" {
511508
// We're in workspace mode, which implies module mode.
512509
if cfg.ModFile != "" {
513510
base.Fatalf("go: -modfile cannot be used in workspace mode")
@@ -651,7 +648,7 @@ func inWorkspaceMode() bool {
651648
if !Enabled() {
652649
return false
653650
}
654-
return workFilePath != ""
651+
return LoaderState.workFilePath != ""
655652
}
656653

657654
// HasModRoot reports whether a main module or main modules are present.
@@ -888,15 +885,15 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
888885
var workFile *modfile.WorkFile
889886
if inWorkspaceMode() {
890887
var err error
891-
workFile, LoaderState.modRoots, err = LoadWorkFile(workFilePath)
888+
workFile, LoaderState.modRoots, err = LoadWorkFile(LoaderState.workFilePath)
892889
if err != nil {
893890
return nil, err
894891
}
895892
for _, modRoot := range LoaderState.modRoots {
896893
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
897894
modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
898895
}
899-
modfetch.GoSumFile = workFilePath + ".sum"
896+
modfetch.GoSumFile = LoaderState.workFilePath + ".sum"
900897
} else if len(LoaderState.modRoots) == 0 {
901898
// We're in module mode, but not inside a module.
902899
//
@@ -1542,8 +1539,8 @@ func setDefaultBuildMod() {
15421539
}
15431540
}
15441541
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")
15471544
} else {
15481545
if len(LoaderState.modRoots) != 1 {
15491546
panic(fmt.Errorf("outside workspace mode, but have %v modRoots", LoaderState.modRoots))

src/cmd/go/internal/modload/modfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func replacementFrom(mod module.Version) (r module.Version, modroot string, from
356356
return module.Version{}, "", ""
357357
}
358358
if _, r, ok := replacement(mod, LoaderState.MainModules.WorkFileReplaceMap()); ok {
359-
return r, "", workFilePath
359+
return r, "", LoaderState.workFilePath
360360
}
361361
for _, v := range LoaderState.MainModules.Versions() {
362362
if index := LoaderState.MainModules.Index(v); index != nil {

0 commit comments

Comments
 (0)