]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use local state object in `run.runRun`
authorIan Alexander <jitsu@google.com>
Thu, 2 Oct 2025 16:52:26 +0000 (12:52 -0400)
committerIan Alexander <jitsu@google.com>
Sat, 25 Oct 2025 01:08:10 +0000 (18:08 -0700)
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>

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

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