From 29a81624f744f0435925cb7c349f68c86213f09c Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Fri, 3 Oct 2025 15:04:30 -0400 Subject: [PATCH] cmd/go: inject state parameter into `fmtcmd.runFmt` This command modifies the call tree starting at `fmtcmd.runFmt` to inject a `State` parameter to every function that is currently using the global `modload.LoaderState` variable. By explicilty passing a `State` parameter, we can begin to eliminate the usage of the global `modload.LoaderState`. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/fmtcmd rf ' inject modload.LoaderState runFmt add fmt.go var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runFmt://+0 moduleLoaderState := modload.NewState() rm fmt.go:/var moduleLoaderState \*modload.State/ ' cd .. ./rf-cleanup.zsh Change-Id: Ib6692aba37a2cbc5b52d3bb705ec2b442afd26eb Reviewed-on: https://go-review.googlesource.com/c/go/+/709989 Reviewed-by: Michael Matloob Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/fmtcmd/fmt.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/fmtcmd/fmt.go b/src/cmd/go/internal/fmtcmd/fmt.go index a42e775305..cd689b510d 100644 --- a/src/cmd/go/internal/fmtcmd/fmt.go +++ b/src/cmd/go/internal/fmtcmd/fmt.go @@ -50,6 +50,7 @@ See also: go fix, go vet. } func runFmt(ctx context.Context, cmd *base.Command, args []string) { + moduleLoaderState := modload.NewState() printed := false gofmt := gofmtPath() @@ -59,8 +60,8 @@ func runFmt(ctx context.Context, cmd *base.Command, args []string) { baseGofmtArgs := len(gofmtArgs) baseGofmtArgLen := gofmtArgLen - for _, pkg := range load.PackagesAndErrors(modload.LoaderState, ctx, load.PackageOpts{}, args) { - if modload.Enabled(modload.LoaderState) && pkg.Module != nil && !pkg.Module.Main { + for _, pkg := range load.PackagesAndErrors(moduleLoaderState, ctx, load.PackageOpts{}, args) { + if modload.Enabled(moduleLoaderState) && pkg.Module != nil && !pkg.Module.Main { if !printed { fmt.Fprintf(os.Stderr, "go: not formatting packages in dependency modules\n") printed = true -- 2.52.0