Skip to content

Commit 5b29875

Browse files
committed
cmd/go: inject State parameter into run.runRun
This command modifies the call tree starting at `run.runRun` to inject a `State` parameter to every function that is currently using the global `modload.LoaderState` variable. By explicilty passing a `State` parameter, we can begin to eliminate the usage of the global `modload.LoaderState`. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/run rf 'inject modload.LoaderState runRun' cd .. ./rf-cleanup.zsh Change-Id: I337323c087ed4e43af28973fad27152791eefbc2 Reviewed-on: https://go-review.googlesource.com/c/go/+/698063 TryBot-Bypass: Ian Alexander <jitsu@google.com> Reviewed-by: Michael Matloob <matloob@google.com> Reviewed-by: Michael Matloob <matloob@golang.org>
1 parent 5113496 commit 5b29875

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+867
-863
lines changed

src/cmd/go/internal/bug/bug.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"cmd/go/internal/base"
2222
"cmd/go/internal/cfg"
2323
"cmd/go/internal/envcmd"
24+
"cmd/go/internal/modload"
2425
"cmd/go/internal/web"
2526
"cmd/go/internal/work"
2627
)
@@ -44,7 +45,7 @@ func runBug(ctx context.Context, cmd *base.Command, args []string) {
4445
if len(args) > 0 {
4546
base.Fatalf("go: bug takes no arguments")
4647
}
47-
work.BuildInit()
48+
work.BuildInit(modload.LoaderState)
4849

4950
var buf strings.Builder
5051
buf.WriteString(bugHeader)

src/cmd/go/internal/clean/clean.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func init() {
120120
}
121121

122122
func runClean(ctx context.Context, cmd *base.Command, args []string) {
123-
modload.InitWorkfile()
123+
modload.InitWorkfile(modload.LoaderState)
124124
if len(args) > 0 {
125125
cacheFlag := ""
126126
switch {
@@ -142,13 +142,13 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
142142
// either the flags and arguments explicitly imply a package,
143143
// or no other target (such as a cache) was requested to be cleaned.
144144
cleanPkg := len(args) > 0 || cleanI || cleanR
145-
if (!modload.Enabled() || modload.HasModRoot()) &&
145+
if (!modload.Enabled(modload.LoaderState) || modload.HasModRoot(modload.LoaderState)) &&
146146
!cleanCache && !cleanModcache && !cleanTestcache && !cleanFuzzcache {
147147
cleanPkg = true
148148
}
149149

150150
if cleanPkg {
151-
for _, pkg := range load.PackagesAndErrors(ctx, load.PackageOpts{}, args) {
151+
for _, pkg := range load.PackagesAndErrors(modload.LoaderState, ctx, load.PackageOpts{}, args) {
152152
clean(pkg)
153153
}
154154
}

src/cmd/go/internal/envcmd/env.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ func findEnv(env []cfg.EnvVar, name string) string {
191191
// ExtraEnvVars returns environment variables that should not leak into child processes.
192192
func ExtraEnvVars() []cfg.EnvVar {
193193
gomod := ""
194-
modload.Init()
195-
if modload.HasModRoot() {
194+
modload.Init(modload.LoaderState)
195+
if modload.HasModRoot(modload.LoaderState) {
196196
gomod = modload.ModFilePath()
197-
} else if modload.Enabled() {
197+
} else if modload.Enabled(modload.LoaderState) {
198198
gomod = os.DevNull
199199
}
200-
modload.InitWorkfile()
201-
gowork := modload.WorkFilePath()
200+
modload.InitWorkfile(modload.LoaderState)
201+
gowork := modload.WorkFilePath(modload.LoaderState)
202202
// As a special case, if a user set off explicitly, report that in GOWORK.
203203
if cfg.Getenv("GOWORK") == "off" {
204204
gowork = "off"
@@ -336,7 +336,7 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
336336
}
337337
}
338338
if needCostly {
339-
work.BuildInit()
339+
work.BuildInit(modload.LoaderState)
340340
env = append(env, ExtraEnvVarsCostly()...)
341341
}
342342

src/cmd/go/internal/fmtcmd/fmt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func runFmt(ctx context.Context, cmd *base.Command, args []string) {
5959
baseGofmtArgs := len(gofmtArgs)
6060
baseGofmtArgLen := gofmtArgLen
6161

62-
for _, pkg := range load.PackagesAndErrors(ctx, load.PackageOpts{}, args) {
63-
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
62+
for _, pkg := range load.PackagesAndErrors(modload.LoaderState, ctx, load.PackageOpts{}, args) {
63+
if modload.Enabled(modload.LoaderState) && pkg.Module != nil && !pkg.Module.Main {
6464
if !printed {
6565
fmt.Fprintf(os.Stderr, "go: not formatting packages in dependency modules\n")
6666
printed = true
@@ -70,7 +70,7 @@ func runFmt(ctx context.Context, cmd *base.Command, args []string) {
7070
if pkg.Error != nil {
7171
if _, ok := errors.AsType[*load.NoGoError](pkg.Error); ok {
7272
// Skip this error, as we will format all files regardless.
73-
} else if _, ok := errors.AsType[*load.EmbedError](pkg.Error); ok && len(pkg.InternalAllGoFiles()) > 0 {
73+
} else if _, ok := errors.AsType[*load.EmbedError](pkg.Error); ok && len(pkg.InternalAllGoFiles()) > 0 {
7474
// Skip this error, as we will format all files regardless.
7575
} else {
7676
base.Errorf("%v", pkg.Error)

src/cmd/go/internal/generate/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func init() {
182182
}
183183

184184
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
185-
modload.InitWorkfile()
185+
modload.InitWorkfile(modload.LoaderState)
186186

187187
if generateRunFlag != "" {
188188
var err error
@@ -204,8 +204,8 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
204204
// Even if the arguments are .go files, this loop suffices.
205205
printed := false
206206
pkgOpts := load.PackageOpts{IgnoreImports: true}
207-
for _, pkg := range load.PackagesAndErrors(ctx, pkgOpts, args) {
208-
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
207+
for _, pkg := range load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args) {
208+
if modload.Enabled(modload.LoaderState) && pkg.Module != nil && !pkg.Module.Main {
209209
if !printed {
210210
fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n")
211211
printed = true

src/cmd/go/internal/list/list.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,19 @@ func (v *jsonFlag) needAny(fields ...string) bool {
419419
var nl = []byte{'\n'}
420420

421421
func runList(ctx context.Context, cmd *base.Command, args []string) {
422-
modload.InitWorkfile()
422+
modload.InitWorkfile(modload.LoaderState)
423423

424424
if *listFmt != "" && listJson {
425425
base.Fatalf("go list -f cannot be used with -json")
426426
}
427427
if *listReuse != "" && !*listM {
428428
base.Fatalf("go list -reuse cannot be used without -m")
429429
}
430-
if *listReuse != "" && modload.HasModRoot() {
430+
if *listReuse != "" && modload.HasModRoot(modload.LoaderState) {
431431
base.Fatalf("go list -reuse cannot be used inside a module")
432432
}
433433

434-
work.BuildInit()
434+
work.BuildInit(modload.LoaderState)
435435
out := newTrackingWriter(os.Stdout)
436436
defer out.w.Flush()
437437

@@ -496,12 +496,12 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
496496
}
497497
}
498498

499-
modload.Init()
499+
modload.Init(modload.LoaderState)
500500
if *listRetracted {
501501
if cfg.BuildMod == "vendor" {
502502
base.Fatalf("go list -retracted cannot be used when vendoring is enabled")
503503
}
504-
if !modload.Enabled() {
504+
if !modload.Enabled(modload.LoaderState) {
505505
base.Fatalf("go list -retracted can only be used in module-aware mode")
506506
}
507507
}
@@ -525,11 +525,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
525525
base.Fatalf("go list -test cannot be used with -m")
526526
}
527527

528-
if modload.Init(); !modload.Enabled() {
528+
if modload.Init(modload.LoaderState); !modload.Enabled(modload.LoaderState) {
529529
base.Fatalf("go: list -m cannot be used with GO111MODULE=off")
530530
}
531531

532-
modload.LoadModFile(ctx) // Sets cfg.BuildMod as a side-effect.
532+
modload.LoadModFile(modload.LoaderState, ctx) // Sets cfg.BuildMod as a side-effect.
533533
if cfg.BuildMod == "vendor" {
534534
const actionDisabledFormat = "go: can't %s using the vendor directory\n\t(Use -mod=mod or -mod=readonly to bypass.)"
535535

@@ -613,7 +613,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
613613
SuppressBuildInfo: !*listExport && !listJsonFields.needAny("Stale", "StaleReason"),
614614
SuppressEmbedFiles: !*listExport && !listJsonFields.needAny("EmbedFiles", "TestEmbedFiles", "XTestEmbedFiles"),
615615
}
616-
pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
616+
pkgs := load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args)
617617
if !*listE {
618618
w := 0
619619
for _, pkg := range pkgs {
@@ -727,7 +727,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
727727
b.NeedExport = *listExport
728728
b.NeedCompiledGoFiles = *listCompiled
729729
if cfg.BuildCover {
730-
load.PrepareForCoverageBuild(pkgs)
730+
load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
731731
}
732732
a := &work.Action{}
733733
// TODO: Use pkgsFilter?

src/cmd/go/internal/load/flag.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package load
66

77
import (
88
"cmd/go/internal/base"
9+
"cmd/go/internal/modload"
910
"cmd/internal/quoted"
1011
"fmt"
1112
"strings"
@@ -63,7 +64,7 @@ func (f *PerPackageFlag) set(v, cwd string) error {
6364
return fmt.Errorf("parameter may not start with quote character %c", v[0])
6465
}
6566
pattern := strings.TrimSpace(v[:i])
66-
match = MatchPackage(pattern, cwd)
67+
match = MatchPackage(modload.LoaderState, pattern, cwd)
6768
v = v[i+1:]
6869
}
6970
flags, err := quoted.Split(v)

src/cmd/go/internal/load/godebug.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func ParseGoDebug(text string) (key, value string, err error) {
4545
// defaultGODEBUG returns the default GODEBUG setting for the main package p.
4646
// When building a test binary, directives, testDirectives, and xtestDirectives
4747
// list additional directives from the package under test.
48-
func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []build.Directive) string {
48+
func defaultGODEBUG(loaderstate *modload.State, p *Package, directives, testDirectives, xtestDirectives []build.Directive) string {
4949
if p.Name != "main" {
5050
return ""
5151
}
52-
goVersion := modload.LoaderState.MainModules.GoVersion()
53-
if modload.LoaderState.RootMode == modload.NoRoot && p.Module != nil {
52+
goVersion := loaderstate.MainModules.GoVersion(loaderstate)
53+
if loaderstate.RootMode == modload.NoRoot && p.Module != nil {
5454
// This is go install pkg@version or go run pkg@version.
5555
// Use the Go version from the package.
5656
// If there isn't one, then assume Go 1.20,
@@ -73,7 +73,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu
7373
}
7474

7575
// Add directives from main module go.mod.
76-
for _, g := range modload.LoaderState.MainModules.Godebugs() {
76+
for _, g := range loaderstate.MainModules.Godebugs(loaderstate) {
7777
if m == nil {
7878
m = make(map[string]string)
7979
}

0 commit comments

Comments
 (0)