Skip to content

Commit d312e27

Browse files
committed
cmd/go: use local state object in pkg modcmd
This commit modifies `modcmd.runDownload` 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/modcmd rf ' add download.go:/func runDownload\(/-0 var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runDownload://+0 moduleLoaderState := modload.NewState() add runEdit://+0 moduleLoaderState := modload.NewState() add runGraph://+0 moduleLoaderState := modload.NewState() add runInit://+0 moduleLoaderState := modload.NewState() add runTidy://+0 moduleLoaderState := modload.NewState() add runVendor://+0 moduleLoaderState := modload.NewState() add runVerify://+0 moduleLoaderState := modload.NewState() add runWhy://+0 moduleLoaderState := modload.NewState() rm download.go:/var moduleLoaderState \*modload.State/ ' Change-Id: Id69deba173032a4d6da228eae28e38bd87176db5 Reviewed-on: https://go-review.googlesource.com/c/go/+/711125 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>
1 parent ea9cf26 commit d312e27

File tree

8 files changed

+56
-48
lines changed

8 files changed

+56
-48
lines changed

src/cmd/go/internal/modcmd/download.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,19 @@ type ModuleJSON struct {
109109
}
110110

111111
func runDownload(ctx context.Context, cmd *base.Command, args []string) {
112-
modload.InitWorkfile(modload.LoaderState)
112+
moduleLoaderState := modload.NewState()
113+
modload.InitWorkfile(moduleLoaderState)
113114

114115
// Check whether modules are enabled and whether we're in a module.
115-
modload.LoaderState.ForceUseModules = true
116+
moduleLoaderState.ForceUseModules = true
116117
modload.ExplicitWriteGoMod = true
117118
haveExplicitArgs := len(args) > 0
118119

119-
if modload.HasModRoot(modload.LoaderState) || modload.WorkFilePath(modload.LoaderState) != "" {
120-
modload.LoadModFile(modload.LoaderState, ctx) // to fill MainModules
120+
if modload.HasModRoot(moduleLoaderState) || modload.WorkFilePath(moduleLoaderState) != "" {
121+
modload.LoadModFile(moduleLoaderState, ctx) // to fill MainModules
121122

122123
if haveExplicitArgs {
123-
for _, mainModule := range modload.LoaderState.MainModules.Versions() {
124+
for _, mainModule := range moduleLoaderState.MainModules.Versions() {
124125
targetAtUpgrade := mainModule.Path + "@upgrade"
125126
targetAtPatch := mainModule.Path + "@patch"
126127
for _, arg := range args {
@@ -130,14 +131,14 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
130131
}
131132
}
132133
}
133-
} else if modload.WorkFilePath(modload.LoaderState) != "" {
134+
} else if modload.WorkFilePath(moduleLoaderState) != "" {
134135
// TODO(#44435): Think about what the correct query is to download the
135136
// right set of modules. Also see code review comment at
136137
// https://go-review.googlesource.com/c/go/+/359794/comments/ce946a80_6cf53992.
137138
args = []string{"all"}
138139
} else {
139-
mainModule := modload.LoaderState.MainModules.Versions()[0]
140-
modFile := modload.LoaderState.MainModules.ModFile(mainModule)
140+
mainModule := moduleLoaderState.MainModules.Versions()[0]
141+
modFile := moduleLoaderState.MainModules.ModFile(mainModule)
141142
if modFile.Go == nil || gover.Compare(modFile.Go.Version, gover.ExplicitIndirectVersion) < 0 {
142143
if len(modFile.Require) > 0 {
143144
args = []string{"all"}
@@ -153,12 +154,12 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
153154
// However, we also need to load the full module graph, to ensure that
154155
// we have downloaded enough of the module graph to run 'go list all',
155156
// 'go mod graph', and similar commands.
156-
_, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
157+
_, err := modload.LoadModGraph(moduleLoaderState, ctx, "")
157158
if err != nil {
158159
// TODO(#64008): call base.Fatalf instead of toolchain.SwitchOrFatal
159160
// here, since we can only reach this point with an outdated toolchain
160161
// if the go.mod file is inconsistent.
161-
toolchain.SwitchOrFatal(modload.LoaderState, ctx, err)
162+
toolchain.SwitchOrFatal(moduleLoaderState, ctx, err)
162163
}
163164

164165
for _, m := range modFile.Require {
@@ -169,22 +170,22 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
169170
}
170171

171172
if len(args) == 0 {
172-
if modload.HasModRoot(modload.LoaderState) {
173+
if modload.HasModRoot(moduleLoaderState) {
173174
os.Stderr.WriteString("go: no module dependencies to download\n")
174175
} else {
175176
base.Errorf("go: no modules specified (see 'go help mod download')")
176177
}
177178
base.Exit()
178179
}
179180

180-
if *downloadReuse != "" && modload.HasModRoot(modload.LoaderState) {
181+
if *downloadReuse != "" && modload.HasModRoot(moduleLoaderState) {
181182
base.Fatalf("go mod download -reuse cannot be used inside a module")
182183
}
183184

184185
var mods []*ModuleJSON
185186
type token struct{}
186187
sem := make(chan token, runtime.GOMAXPROCS(0))
187-
infos, infosErr := modload.ListModules(modload.LoaderState, ctx, args, 0, *downloadReuse)
188+
infos, infosErr := modload.ListModules(moduleLoaderState, ctx, args, 0, *downloadReuse)
188189

189190
// There is a bit of a chicken-and-egg problem here: ideally we need to know
190191
// which Go version to switch to download the requested modules, but if we
@@ -211,7 +212,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
211212
// toolchain version) or only one module (as is used by the Go Module Proxy).
212213

213214
if infosErr != nil {
214-
sw := toolchain.NewSwitcher(modload.LoaderState)
215+
sw := toolchain.NewSwitcher(moduleLoaderState)
215216
sw.Error(infosErr)
216217
if sw.NeedSwitch() {
217218
sw.Switch(ctx)
@@ -220,7 +221,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
220221
// when we can.
221222
}
222223

223-
if !haveExplicitArgs && modload.WorkFilePath(modload.LoaderState) == "" {
224+
if !haveExplicitArgs && modload.WorkFilePath(moduleLoaderState) == "" {
224225
// 'go mod download' is sometimes run without arguments to pre-populate the
225226
// module cache. In modules that aren't at go 1.17 or higher, it may fetch
226227
// modules that aren't needed to build packages in the main module. This is
@@ -231,7 +232,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
231232
// TODO(#64008): In the future, report an error if go.mod or go.sum need to
232233
// be updated after loading the build list. This may require setting
233234
// the mode to "mod" or "readonly" depending on haveExplicitArgs.
234-
if err := modload.WriteGoMod(modload.LoaderState, ctx, modload.WriteOpts{}); err != nil {
235+
if err := modload.WriteGoMod(moduleLoaderState, ctx, modload.WriteOpts{}); err != nil {
235236
base.Fatal(err)
236237
}
237238
}
@@ -291,8 +292,8 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
291292
// with no arguments we download the module pattern "all",
292293
// which may include dependencies that are normally pruned out
293294
// of the individual modules in the workspace.
294-
if haveExplicitArgs || modload.WorkFilePath(modload.LoaderState) != "" {
295-
sw := toolchain.NewSwitcher(modload.LoaderState)
295+
if haveExplicitArgs || modload.WorkFilePath(moduleLoaderState) != "" {
296+
sw := toolchain.NewSwitcher(moduleLoaderState)
296297
// Add errors to the Switcher in deterministic order so that they will be
297298
// logged deterministically.
298299
for _, m := range mods {
@@ -347,8 +348,8 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
347348
//
348349
// Don't save sums for 'go mod download' without arguments unless we're in
349350
// workspace mode; see comment above.
350-
if haveExplicitArgs || modload.WorkFilePath(modload.LoaderState) != "" {
351-
if err := modload.WriteGoMod(modload.LoaderState, ctx, modload.WriteOpts{}); err != nil {
351+
if haveExplicitArgs || modload.WorkFilePath(moduleLoaderState) != "" {
352+
if err := modload.WriteGoMod(moduleLoaderState, ctx, modload.WriteOpts{}); err != nil {
352353
base.Error(err)
353354
}
354355
}

src/cmd/go/internal/modcmd/edit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func init() {
209209
}
210210

211211
func runEdit(ctx context.Context, cmd *base.Command, args []string) {
212+
moduleLoaderState := modload.NewState()
212213
anyFlags := *editModule != "" ||
213214
*editGo != "" ||
214215
*editToolchain != "" ||
@@ -232,7 +233,7 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {
232233
if len(args) == 1 {
233234
gomod = args[0]
234235
} else {
235-
gomod = modload.ModFilePath(modload.LoaderState)
236+
gomod = modload.ModFilePath(moduleLoaderState)
236237
}
237238

238239
if *editModule != "" {

src/cmd/go/internal/modcmd/graph.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,24 @@ func init() {
5252
}
5353

5454
func runGraph(ctx context.Context, cmd *base.Command, args []string) {
55-
modload.InitWorkfile(modload.LoaderState)
55+
moduleLoaderState := modload.NewState()
56+
modload.InitWorkfile(moduleLoaderState)
5657

5758
if len(args) > 0 {
5859
base.Fatalf("go: 'go mod graph' accepts no arguments")
5960
}
60-
modload.LoaderState.ForceUseModules = true
61-
modload.LoaderState.RootMode = modload.NeedRoot
61+
moduleLoaderState.ForceUseModules = true
62+
moduleLoaderState.RootMode = modload.NeedRoot
6263

6364
goVersion := graphGo.String()
6465
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {
65-
toolchain.SwitchOrFatal(modload.LoaderState, ctx, &gover.TooNewError{
66+
toolchain.SwitchOrFatal(moduleLoaderState, ctx, &gover.TooNewError{
6667
What: "-go flag",
6768
GoVersion: goVersion,
6869
})
6970
}
7071

71-
mg, err := modload.LoadModGraph(modload.LoaderState, ctx, goVersion)
72+
mg, err := modload.LoadModGraph(moduleLoaderState, ctx, goVersion)
7273
if err != nil {
7374
base.Fatal(err)
7475
}

src/cmd/go/internal/modcmd/init.go

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

3737
func runInit(ctx context.Context, cmd *base.Command, args []string) {
38+
moduleLoaderState := modload.NewState()
3839
if len(args) > 1 {
3940
base.Fatalf("go: 'go mod init' accepts at most one argument")
4041
}
@@ -43,6 +44,6 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) {
4344
modPath = args[0]
4445
}
4546

46-
modload.LoaderState.ForceUseModules = true
47-
modload.CreateModFile(modload.LoaderState, ctx, modPath) // does all the hard work
47+
moduleLoaderState.ForceUseModules = true
48+
modload.CreateModFile(moduleLoaderState, ctx, modPath) // does all the hard work
4849
}

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (f *goVersionFlag) Set(s string) error {
105105
}
106106

107107
func runTidy(ctx context.Context, cmd *base.Command, args []string) {
108+
moduleLoaderState := modload.NewState()
108109
if len(args) > 0 {
109110
base.Fatalf("go: 'go mod tidy' accepts no arguments")
110111
}
@@ -119,18 +120,18 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
119120
// those packages. In order to make 'go test' reproducible for the packages
120121
// that are in 'all' but outside of the main module, we must explicitly
121122
// request that their test dependencies be included.
122-
modload.LoaderState.ForceUseModules = true
123-
modload.LoaderState.RootMode = modload.NeedRoot
123+
moduleLoaderState.ForceUseModules = true
124+
moduleLoaderState.RootMode = modload.NeedRoot
124125

125126
goVersion := tidyGo.String()
126127
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {
127-
toolchain.SwitchOrFatal(modload.LoaderState, ctx, &gover.TooNewError{
128+
toolchain.SwitchOrFatal(moduleLoaderState, ctx, &gover.TooNewError{
128129
What: "-go flag",
129130
GoVersion: goVersion,
130131
})
131132
}
132133

133-
modload.LoadPackages(modload.LoaderState, ctx, modload.PackageOpts{
134+
modload.LoadPackages(moduleLoaderState, ctx, modload.PackageOpts{
134135
TidyGoVersion: tidyGo.String(),
135136
Tags: imports.AnyTags(),
136137
Tidy: true,
@@ -141,6 +142,6 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
141142
LoadTests: true,
142143
AllowErrors: tidyE,
143144
SilenceMissingStdImports: true,
144-
Switcher: toolchain.NewSwitcher(modload.LoaderState),
145+
Switcher: toolchain.NewSwitcher(moduleLoaderState),
145146
}, "all")
146147
}

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ func init() {
6666
}
6767

6868
func runVendor(ctx context.Context, cmd *base.Command, args []string) {
69-
modload.InitWorkfile(modload.LoaderState)
70-
if modload.WorkFilePath(modload.LoaderState) != "" {
69+
moduleLoaderState := modload.NewState()
70+
modload.InitWorkfile(moduleLoaderState)
71+
if modload.WorkFilePath(moduleLoaderState) != "" {
7172
base.Fatalf("go: 'go mod vendor' cannot be run in workspace mode. Run 'go work vendor' to vendor the workspace or set 'GOWORK=off' to exit workspace mode.")
7273
}
73-
RunVendor(modload.LoaderState, ctx, vendorE, vendorO, args)
74+
RunVendor(moduleLoaderState, ctx, vendorE, vendorO, args)
7475
}
7576

7677
func RunVendor(loaderstate *modload.State, ctx context.Context, vendorE bool, vendorO string, args []string) {

src/cmd/go/internal/modcmd/verify.go

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

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

4950
if len(args) != 0 {
5051
// NOTE(rsc): Could take a module pattern.
5152
base.Fatalf("go: verify takes no arguments")
5253
}
53-
modload.LoaderState.ForceUseModules = true
54-
modload.LoaderState.RootMode = modload.NeedRoot
54+
moduleLoaderState.ForceUseModules = true
55+
moduleLoaderState.RootMode = modload.NeedRoot
5556

5657
// Only verify up to GOMAXPROCS zips at once.
5758
type token struct{}
5859
sem := make(chan token, runtime.GOMAXPROCS(0))
5960

60-
mg, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
61+
mg, err := modload.LoadModGraph(moduleLoaderState, ctx, "")
6162
if err != nil {
6263
base.Fatal(err)
6364
}
@@ -71,7 +72,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
7172
errsChans[i] = errsc
7273
mod := mod // use a copy to avoid data races
7374
go func() {
74-
errsc <- verifyMod(modload.LoaderState, ctx, mod)
75+
errsc <- verifyMod(moduleLoaderState, ctx, mod)
7576
<-sem
7677
}()
7778
}

src/cmd/go/internal/modcmd/why.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ func init() {
6363
}
6464

6565
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
66-
modload.InitWorkfile(modload.LoaderState)
67-
modload.LoaderState.ForceUseModules = true
68-
modload.LoaderState.RootMode = modload.NeedRoot
66+
moduleLoaderState := modload.NewState()
67+
modload.InitWorkfile(moduleLoaderState)
68+
moduleLoaderState.ForceUseModules = true
69+
moduleLoaderState.RootMode = modload.NeedRoot
6970
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules
7071

7172
loadOpts := modload.PackageOpts{
@@ -83,13 +84,13 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
8384
}
8485
}
8586

86-
mods, err := modload.ListModules(modload.LoaderState, ctx, args, 0, "")
87+
mods, err := modload.ListModules(moduleLoaderState, ctx, args, 0, "")
8788
if err != nil {
8889
base.Fatal(err)
8990
}
9091

9192
byModule := make(map[string][]string)
92-
_, pkgs := modload.LoadPackages(modload.LoaderState, ctx, loadOpts, "all")
93+
_, pkgs := modload.LoadPackages(moduleLoaderState, ctx, loadOpts, "all")
9394
for _, path := range pkgs {
9495
m := modload.PackageModule(path)
9596
if m.Path != "" {
@@ -120,9 +121,9 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
120121
}
121122
} else {
122123
// Resolve to packages.
123-
matches, _ := modload.LoadPackages(modload.LoaderState, ctx, loadOpts, args...)
124+
matches, _ := modload.LoadPackages(moduleLoaderState, ctx, loadOpts, args...)
124125

125-
modload.LoadPackages(modload.LoaderState, ctx, loadOpts, "all") // rebuild graph, from main module (not from named packages)
126+
modload.LoadPackages(moduleLoaderState, ctx, loadOpts, "all") // rebuild graph, from main module (not from named packages)
126127

127128
sep := ""
128129
for _, m := range matches {

0 commit comments

Comments
 (0)