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 <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-10-02 12:52:26 -04:00
parent ade9f33e1f
commit 2c4fd7b2cd

View file

@ -71,21 +71,22 @@ func init() {
} }
func runRun(ctx context.Context, cmd *base.Command, args []string) { func runRun(ctx context.Context, cmd *base.Command, args []string) {
moduleLoaderState := modload.NewState()
if shouldUseOutsideModuleMode(args) { if shouldUseOutsideModuleMode(args) {
// Set global module flags for 'go run cmd@version'. // Set global module flags for 'go run cmd@version'.
// This must be done before modload.Init, but we need to call work.BuildInit // This must be done before modload.Init, but we need to call work.BuildInit
// before loading packages, since it affects package locations, e.g., // before loading packages, since it affects package locations, e.g.,
// for -race and -msan. // for -race and -msan.
modload.LoaderState.ForceUseModules = true moduleLoaderState.ForceUseModules = true
modload.LoaderState.RootMode = modload.NoRoot moduleLoaderState.RootMode = modload.NoRoot
modload.AllowMissingModuleImports(modload.LoaderState) modload.AllowMissingModuleImports(moduleLoaderState)
modload.Init(modload.LoaderState) modload.Init(moduleLoaderState)
} else { } else {
modload.InitWorkfile(modload.LoaderState) modload.InitWorkfile(moduleLoaderState)
} }
work.BuildInit(modload.LoaderState) work.BuildInit(moduleLoaderState)
b := work.NewBuilder("", modload.LoaderState.VendorDirOrEmpty) b := work.NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
defer func() { defer func() {
if err := b.Close(); err != nil { if err := b.Close(); err != nil {
base.Fatal(err) base.Fatal(err)
@ -107,18 +108,18 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
base.Fatalf("go: cannot run *_test.go files (%s)", file) base.Fatalf("go: cannot run *_test.go files (%s)", file)
} }
} }
p = load.GoFilesPackage(modload.LoaderState, ctx, pkgOpts, files) p = load.GoFilesPackage(moduleLoaderState, ctx, pkgOpts, files)
} else if len(args) > 0 && !strings.HasPrefix(args[0], "-") { } else if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
arg := args[0] arg := args[0]
var pkgs []*load.Package var pkgs []*load.Package
if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) { if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) {
var err error var err error
pkgs, err = load.PackagesAndErrorsOutsideModule(modload.LoaderState, ctx, pkgOpts, args[:1]) pkgs, err = load.PackagesAndErrorsOutsideModule(moduleLoaderState, ctx, pkgOpts, args[:1])
if err != nil { if err != nil {
base.Fatal(err) base.Fatal(err)
} }
} else { } else {
pkgs = load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args[:1]) pkgs = load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, args[:1])
} }
if len(pkgs) == 0 { if len(pkgs) == 0 {
@ -140,7 +141,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
load.CheckPackageErrors([]*load.Package{p}) load.CheckPackageErrors([]*load.Package{p})
if cfg.BuildCover { if cfg.BuildCover {
load.PrepareForCoverageBuild(modload.LoaderState, []*load.Package{p}) load.PrepareForCoverageBuild(moduleLoaderState, []*load.Package{p})
} }
p.Internal.OmitDebug = true p.Internal.OmitDebug = true
@ -166,7 +167,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
p.Internal.ExeName = p.DefaultExecName() p.Internal.ExeName = p.DefaultExecName()
} }
a1 := b.LinkAction(modload.LoaderState, work.ModeBuild, work.ModeBuild, p) a1 := b.LinkAction(moduleLoaderState, work.ModeBuild, work.ModeBuild, p)
a1.CacheExecutable = true a1.CacheExecutable = true
a := &work.Action{Mode: "go run", Actor: work.ActorFunc(buildRunProgram), Args: cmdArgs, Deps: []*work.Action{a1}} a := &work.Action{Mode: "go run", Actor: work.ActorFunc(buildRunProgram), Args: cmdArgs, Deps: []*work.Action{a1}}
b.Do(ctx, a) b.Do(ctx, a)