cmd/go: use local state object in env.runEnv

This commit modifies `env.runEnv` 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/envcmd
rf '
  add env.go:/func runEnv\(/-0 var moduleLoaderState *modload.State
  ex {
    import "cmd/go/internal/modload";
    modload.LoaderState -> moduleLoaderState
  }
  add runEnv://+0 moduleLoaderState := modload.NewState()
  rm env.go:/var moduleLoaderState \*modload.State/
'

Change-Id: I1177df5d09a6ee642eade6cfa4278bb1629843a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/711131
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-09 21:05:35 -04:00
parent f2dd3d7e31
commit 26a8a21d7f

View file

@ -272,6 +272,7 @@ func argKey(arg string) string {
}
func runEnv(ctx context.Context, cmd *base.Command, args []string) {
moduleLoaderState := modload.NewState()
if *envJson && *envU {
base.Fatalf("go: cannot use -json with -u")
}
@ -306,7 +307,7 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
}
env := cfg.CmdEnv
env = append(env, ExtraEnvVars(modload.LoaderState)...)
env = append(env, ExtraEnvVars(moduleLoaderState)...)
if err := fsys.Init(); err != nil {
base.Fatal(err)
@ -336,8 +337,8 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
}
}
if needCostly {
work.BuildInit(modload.LoaderState)
env = append(env, ExtraEnvVarsCostly(modload.LoaderState)...)
work.BuildInit(moduleLoaderState)
env = append(env, ExtraEnvVarsCostly(moduleLoaderState)...)
}
if len(args) > 0 {