Skip to content

Commit 2c4fd7b

Browse files
committed
cmd/go: use local state object in run.runRun
This commit modifies `run.runRun` 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/run rf ' add run.go:/func runRun\(/-0 var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runRun://+0 moduleLoaderState := modload.NewState() rm run.go:/var moduleLoaderState \*modload.State/ ' Change-Id: I76e56798b2e0d23a0fefe65d997740e3e411d99d Reviewed-on: https://go-review.googlesource.com/c/go/+/711116 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ade9f33 commit 2c4fd7b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/cmd/go/internal/run/run.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,22 @@ func init() {
7171
}
7272

7373
func runRun(ctx context.Context, cmd *base.Command, args []string) {
74+
moduleLoaderState := modload.NewState()
7475
if shouldUseOutsideModuleMode(args) {
7576
// Set global module flags for 'go run cmd@version'.
7677
// This must be done before modload.Init, but we need to call work.BuildInit
7778
// before loading packages, since it affects package locations, e.g.,
7879
// for -race and -msan.
79-
modload.LoaderState.ForceUseModules = true
80-
modload.LoaderState.RootMode = modload.NoRoot
81-
modload.AllowMissingModuleImports(modload.LoaderState)
82-
modload.Init(modload.LoaderState)
80+
moduleLoaderState.ForceUseModules = true
81+
moduleLoaderState.RootMode = modload.NoRoot
82+
modload.AllowMissingModuleImports(moduleLoaderState)
83+
modload.Init(moduleLoaderState)
8384
} else {
84-
modload.InitWorkfile(modload.LoaderState)
85+
modload.InitWorkfile(moduleLoaderState)
8586
}
8687

87-
work.BuildInit(modload.LoaderState)
88-
b := work.NewBuilder("", modload.LoaderState.VendorDirOrEmpty)
88+
work.BuildInit(moduleLoaderState)
89+
b := work.NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
8990
defer func() {
9091
if err := b.Close(); err != nil {
9192
base.Fatal(err)
@@ -107,18 +108,18 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
107108
base.Fatalf("go: cannot run *_test.go files (%s)", file)
108109
}
109110
}
110-
p = load.GoFilesPackage(modload.LoaderState, ctx, pkgOpts, files)
111+
p = load.GoFilesPackage(moduleLoaderState, ctx, pkgOpts, files)
111112
} else if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
112113
arg := args[0]
113114
var pkgs []*load.Package
114115
if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) {
115116
var err error
116-
pkgs, err = load.PackagesAndErrorsOutsideModule(modload.LoaderState, ctx, pkgOpts, args[:1])
117+
pkgs, err = load.PackagesAndErrorsOutsideModule(moduleLoaderState, ctx, pkgOpts, args[:1])
117118
if err != nil {
118119
base.Fatal(err)
119120
}
120121
} else {
121-
pkgs = load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args[:1])
122+
pkgs = load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, args[:1])
122123
}
123124

124125
if len(pkgs) == 0 {
@@ -140,7 +141,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
140141
load.CheckPackageErrors([]*load.Package{p})
141142

142143
if cfg.BuildCover {
143-
load.PrepareForCoverageBuild(modload.LoaderState, []*load.Package{p})
144+
load.PrepareForCoverageBuild(moduleLoaderState, []*load.Package{p})
144145
}
145146

146147
p.Internal.OmitDebug = true
@@ -166,7 +167,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
166167
p.Internal.ExeName = p.DefaultExecName()
167168
}
168169

169-
a1 := b.LinkAction(modload.LoaderState, work.ModeBuild, work.ModeBuild, p)
170+
a1 := b.LinkAction(moduleLoaderState, work.ModeBuild, work.ModeBuild, p)
170171
a1.CacheExecutable = true
171172
a := &work.Action{Mode: "go run", Actor: work.ActorFunc(buildRunProgram), Args: cmdArgs, Deps: []*work.Action{a1}}
172173
b.Do(ctx, a)

0 commit comments

Comments
 (0)