]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: refactor usage of `workFilePath`
authorIan Alexander <jitsu@google.com>
Thu, 21 Aug 2025 01:21:41 +0000 (21:21 -0400)
committerIan Alexander <jitsu@google.com>
Tue, 7 Oct 2025 23:10:28 +0000 (16:10 -0700)
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 <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

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

index b73cb43d0dc3cf2a7c0971fce08972a5b2d542a6..086626042c95b7555f5b42580caf1f6d895dc68c 100644 (file)
@@ -108,13 +108,13 @@ func newRequirements(pruning modPruning, rootModules []module.Version, direct ma
        mustHaveGoRoot(rootModules)
 
        if pruning != workspace {
-               if workFilePath != "" {
+               if LoaderState.workFilePath != "" {
                        panic("in workspace mode, but pruning is not workspace in newRequirements")
                }
        }
 
        if pruning != workspace {
-               if workFilePath != "" {
+               if LoaderState.workFilePath != "" {
                        panic("in workspace mode, but pruning is not workspace in newRequirements")
                }
                for i, m := range rootModules {
index 1c8aa379d3cf782621ffd09dc242c324a3a473b2..31fe6327735736dd0e13a5cbc2704f76cc23d49e 100644 (file)
@@ -60,7 +60,7 @@ var (
 func EnterModule(ctx context.Context, enterModroot string) {
        LoaderState.MainModules = nil // reset MainModules
        LoaderState.requirements = nil
-       workFilePath = "" // Force module mode
+       LoaderState.workFilePath = "" // Force module mode
        modfetch.Reset()
 
        LoaderState.modRoots = []string{enterModroot}
@@ -97,12 +97,6 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
        }, nil
 }
 
-// Variable set in InitWorkfile
-var (
-       // Set to the path to the go.work file, or "" if workspace mode is disabled.
-       workFilePath string
-)
-
 type MainModuleSet struct {
        // versions are the module.Version values of each of the main modules.
        // For each of them, the Path fields are ordinary module paths and the Version
@@ -349,7 +343,7 @@ func InitWorkfile() {
        if err := fsys.Init(); err != nil {
                base.Fatal(err)
        }
-       workFilePath = FindGoWork(base.Cwd())
+       LoaderState.workFilePath = FindGoWork(base.Cwd())
 }
 
 // FindGoWork returns the name of the go.work file for this command,
@@ -378,7 +372,7 @@ func FindGoWork(wd string) string {
 // WorkFilePath returns the absolute path of the go.work file, or "" if not in
 // workspace mode. WorkFilePath must be called after InitWorkfile.
 func WorkFilePath() string {
-       return workFilePath
+       return LoaderState.workFilePath
 }
 
 // Reset clears all the initialized, cached state about the use of modules,
@@ -404,7 +398,7 @@ func setState(s State) State {
        cfg.ModulesEnabled = s.modulesEnabled
        LoaderState.MainModules = s.MainModules
        LoaderState.requirements = s.requirements
-       workFilePath = s.workFilePath
+       LoaderState.workFilePath = s.workFilePath
        // The modfetch package's global state is used to compute
        // the go.sum file, so save and restore it along with the
        // modload state.
@@ -441,7 +435,10 @@ type State struct {
        // commitRequirements functions.  All other functions that need or
        // produce a *Requirements should accept and/or return an explicit
        // parameter.
-       requirements  *Requirements
+       requirements *Requirements
+
+       // Set to the path to the go.work file, or "" if workspace mode is
+       // disabled
        workFilePath  string
        modfetchState modfetch.State
 }
@@ -507,7 +504,7 @@ func Init() {
                        base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
                }
                LoaderState.modRoots = nil
-       } else if workFilePath != "" {
+       } else if LoaderState.workFilePath != "" {
                // We're in workspace mode, which implies module mode.
                if cfg.ModFile != "" {
                        base.Fatalf("go: -modfile cannot be used in workspace mode")
@@ -651,7 +648,7 @@ func inWorkspaceMode() bool {
        if !Enabled() {
                return false
        }
-       return workFilePath != ""
+       return LoaderState.workFilePath != ""
 }
 
 // HasModRoot reports whether a main module or main modules are present.
@@ -888,7 +885,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
        var workFile *modfile.WorkFile
        if inWorkspaceMode() {
                var err error
-               workFile, LoaderState.modRoots, err = LoadWorkFile(workFilePath)
+               workFile, LoaderState.modRoots, err = LoadWorkFile(LoaderState.workFilePath)
                if err != nil {
                        return nil, err
                }
@@ -896,7 +893,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
                        sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
                        modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
                }
-               modfetch.GoSumFile = workFilePath + ".sum"
+               modfetch.GoSumFile = LoaderState.workFilePath + ".sum"
        } else if len(LoaderState.modRoots) == 0 {
                // We're in module mode, but not inside a module.
                //
@@ -1542,8 +1539,8 @@ func setDefaultBuildMod() {
                        }
                }
                vendorDir := ""
-               if workFilePath != "" {
-                       vendorDir = filepath.Join(filepath.Dir(workFilePath), "vendor")
+               if LoaderState.workFilePath != "" {
+                       vendorDir = filepath.Join(filepath.Dir(LoaderState.workFilePath), "vendor")
                } else {
                        if len(LoaderState.modRoots) != 1 {
                                panic(fmt.Errorf("outside workspace mode, but have %v modRoots", LoaderState.modRoots))
index 6b432ed9637ec5cbde89dc9c884af08359c35e13..fa2348d97baf25e26e2aaf5f9d5952847f0b6b65 100644 (file)
@@ -356,7 +356,7 @@ func replacementFrom(mod module.Version) (r module.Version, modroot string, from
                return module.Version{}, "", ""
        }
        if _, r, ok := replacement(mod, LoaderState.MainModules.WorkFileReplaceMap()); ok {
-               return r, "", workFilePath
+               return r, "", LoaderState.workFilePath
        }
        for _, v := range LoaderState.MainModules.Versions() {
                if index := LoaderState.MainModules.Index(v); index != nil {