Skip to content

Commit 7847004

Browse files
committed
cmd/go: use local state object in pkg workcmd
This commit modifies package `workcmd` to construct a new modload.State object using the new constructor instead of the current global `modload.LoaderState` variable. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/workcmd rf ' inject modload.LoaderState runUse add sync.go:/func runSync\(/-0 var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runSync://+0 moduleLoaderState := modload.NewState() add runEditwork://+0 moduleLoaderState := modload.NewState() add runInit://+0 moduleLoaderState := modload.NewState() add runUse://+0 moduleLoaderState := modload.NewState() add runVendor://+0 moduleLoaderState := modload.NewState() rm sync.go:/var moduleLoaderState \*modload.State/ ' Change-Id: Iadc4ffd19d15f80a694285c86adfc01f0d26bac7 Reviewed-on: https://go-review.googlesource.com/c/go/+/711129 Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Michael Matloob <matloob@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 69673e9 commit 7847004

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

src/cmd/go/internal/workcmd/edit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func init() {
132132
}
133133

134134
func runEditwork(ctx context.Context, cmd *base.Command, args []string) {
135+
moduleLoaderState := modload.NewState()
135136
if *editJSON && *editPrint {
136137
base.Fatalf("go: cannot use both -json and -print")
137138
}
@@ -143,8 +144,8 @@ func runEditwork(ctx context.Context, cmd *base.Command, args []string) {
143144
if len(args) == 1 {
144145
gowork = args[0]
145146
} else {
146-
modload.InitWorkfile(modload.LoaderState)
147-
gowork = modload.WorkFilePath(modload.LoaderState)
147+
modload.InitWorkfile(moduleLoaderState)
148+
gowork = modload.WorkFilePath(moduleLoaderState)
148149
}
149150
if gowork == "" {
150151
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")

src/cmd/go/internal/workcmd/init.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ func init() {
4444
}
4545

4646
func runInit(ctx context.Context, cmd *base.Command, args []string) {
47-
modload.InitWorkfile(modload.LoaderState)
47+
moduleLoaderState := modload.NewState()
48+
modload.InitWorkfile(moduleLoaderState)
4849

49-
modload.LoaderState.ForceUseModules = true
50+
moduleLoaderState.ForceUseModules = true
5051

51-
gowork := modload.WorkFilePath(modload.LoaderState)
52+
gowork := modload.WorkFilePath(moduleLoaderState)
5253
if gowork == "" {
5354
gowork = filepath.Join(base.Cwd(), "go.work")
5455
}
@@ -61,6 +62,6 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) {
6162
wf := new(modfile.WorkFile)
6263
wf.Syntax = new(modfile.FileSyntax)
6364
wf.AddGoStmt(goV)
64-
workUse(ctx, gowork, wf, args)
65+
workUse(moduleLoaderState, ctx, gowork, wf, args)
6566
modload.WriteWorkFile(gowork, wf)
6667
}

src/cmd/go/internal/workcmd/sync.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,20 @@ func init() {
4848
}
4949

5050
func runSync(ctx context.Context, cmd *base.Command, args []string) {
51-
modload.LoaderState.ForceUseModules = true
52-
modload.InitWorkfile(modload.LoaderState)
53-
if modload.WorkFilePath(modload.LoaderState) == "" {
51+
moduleLoaderState := modload.NewState()
52+
moduleLoaderState.ForceUseModules = true
53+
modload.InitWorkfile(moduleLoaderState)
54+
if modload.WorkFilePath(moduleLoaderState) == "" {
5455
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
5556
}
5657

57-
_, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
58+
_, err := modload.LoadModGraph(moduleLoaderState, ctx, "")
5859
if err != nil {
59-
toolchain.SwitchOrFatal(modload.LoaderState, ctx, err)
60+
toolchain.SwitchOrFatal(moduleLoaderState, ctx, err)
6061
}
6162
mustSelectFor := map[module.Version][]module.Version{}
6263

63-
mms := modload.LoaderState.MainModules
64+
mms := moduleLoaderState.MainModules
6465

6566
opts := modload.PackageOpts{
6667
Tags: imports.AnyTags(),
@@ -73,7 +74,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
7374
}
7475
for _, m := range mms.Versions() {
7576
opts.MainModule = m
76-
_, pkgs := modload.LoadPackages(modload.LoaderState, ctx, opts, "all")
77+
_, pkgs := modload.LoadPackages(moduleLoaderState, ctx, opts, "all")
7778
opts.MainModule = module.Version{} // reset
7879

7980
var (
@@ -91,7 +92,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
9192
mustSelectFor[m] = mustSelect
9293
}
9394

94-
workFilePath := modload.WorkFilePath(modload.LoaderState) // save go.work path because EnterModule clobbers it.
95+
workFilePath := modload.WorkFilePath(moduleLoaderState) // save go.work path because EnterModule clobbers it.
9596

9697
var goV string
9798
for _, m := range mms.Versions() {
@@ -104,7 +105,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
104105

105106
// Use EnterModule to reset the global state in modload to be in
106107
// single-module mode using the modroot of m.
107-
modload.EnterModule(modload.LoaderState, ctx, mms.ModRoot(m))
108+
modload.EnterModule(moduleLoaderState, ctx, mms.ModRoot(m))
108109

109110
// Edit the build list in the same way that 'go get' would if we
110111
// requested the relevant module versions explicitly.
@@ -114,12 +115,12 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
114115
// so we don't write some go.mods with the "before" toolchain
115116
// and others with the "after" toolchain. If nothing else, that
116117
// discrepancy could show up in auto-recorded toolchain lines.
117-
changed, err := modload.EditBuildList(modload.LoaderState, ctx, nil, mustSelectFor[m])
118+
changed, err := modload.EditBuildList(moduleLoaderState, ctx, nil, mustSelectFor[m])
118119
if err != nil {
119120
continue
120121
}
121122
if changed {
122-
modload.LoadPackages(modload.LoaderState, ctx, modload.PackageOpts{
123+
modload.LoadPackages(moduleLoaderState, ctx, modload.PackageOpts{
123124
Tags: imports.AnyTags(),
124125
Tidy: true,
125126
VendorModulesInGOROOTSrc: true,
@@ -129,9 +130,9 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
129130
SilenceMissingStdImports: true,
130131
SilencePackageErrors: true,
131132
}, "all")
132-
modload.WriteGoMod(modload.LoaderState, ctx, modload.WriteOpts{})
133+
modload.WriteGoMod(moduleLoaderState, ctx, modload.WriteOpts{})
133134
}
134-
goV = gover.Max(goV, modload.LoaderState.MainModules.GoVersion(modload.LoaderState))
135+
goV = gover.Max(goV, moduleLoaderState.MainModules.GoVersion(moduleLoaderState))
135136
}
136137

137138
wf, err := modload.ReadWorkFile(workFilePath)

src/cmd/go/internal/workcmd/use.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,22 @@ func init() {
6161
}
6262

6363
func runUse(ctx context.Context, cmd *base.Command, args []string) {
64-
modload.LoaderState.ForceUseModules = true
65-
modload.InitWorkfile(modload.LoaderState)
66-
gowork := modload.WorkFilePath(modload.LoaderState)
64+
moduleLoaderState := modload.NewState()
65+
moduleLoaderState.ForceUseModules = true
66+
modload.InitWorkfile(moduleLoaderState)
67+
gowork := modload.WorkFilePath(moduleLoaderState)
6768
if gowork == "" {
6869
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
6970
}
7071
wf, err := modload.ReadWorkFile(gowork)
7172
if err != nil {
7273
base.Fatal(err)
7374
}
74-
workUse(ctx, gowork, wf, args)
75+
workUse(moduleLoaderState, ctx, gowork, wf, args)
7576
modload.WriteWorkFile(gowork, wf)
7677
}
7778

78-
func workUse(ctx context.Context, gowork string, wf *modfile.WorkFile, args []string) {
79+
func workUse(loaderstate *modload.State, ctx context.Context, gowork string, wf *modfile.WorkFile, args []string) {
7980
workDir := filepath.Dir(gowork) // absolute, since gowork itself is absolute
8081

8182
haveDirs := make(map[string][]string) // absolute → original(s)
@@ -94,7 +95,7 @@ func workUse(ctx context.Context, gowork string, wf *modfile.WorkFile, args []st
9495
// all entries for the absolute path should be removed.
9596
keepDirs := make(map[string]string)
9697

97-
sw := toolchain.NewSwitcher(modload.LoaderState)
98+
sw := toolchain.NewSwitcher(loaderstate)
9899

99100
// lookDir updates the entry in keepDirs for the directory dir,
100101
// which is either absolute or relative to the current working directory

src/cmd/go/internal/workcmd/vendor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ func init() {
4646
}
4747

4848
func runVendor(ctx context.Context, cmd *base.Command, args []string) {
49-
modload.InitWorkfile(modload.LoaderState)
50-
if modload.WorkFilePath(modload.LoaderState) == "" {
49+
moduleLoaderState := modload.NewState()
50+
modload.InitWorkfile(moduleLoaderState)
51+
if modload.WorkFilePath(moduleLoaderState) == "" {
5152
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
5253
}
5354

54-
modcmd.RunVendor(modload.LoaderState, ctx, vendorE, vendorO, args)
55+
modcmd.RunVendor(moduleLoaderState, ctx, vendorE, vendorO, args)
5556
}

0 commit comments

Comments
 (0)