From 2c4fd7b2cd08234fe13c0e9de8d42dba33086a81 Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Thu, 2 Oct 2025 12:52:26 -0400 Subject: [PATCH] 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 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/run/run.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/cmd/go/internal/run/run.go b/src/cmd/go/internal/run/run.go index c39fcab200..f821b37f29 100644 --- a/src/cmd/go/internal/run/run.go +++ b/src/cmd/go/internal/run/run.go @@ -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) -- 2.52.0