cmd/go: use local state object in generate.runGenerate

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

Change-Id: Iea9d662ba47657aa5daa70d32117cdf52fd02b7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/711132
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:10:56 -04:00
parent 26a8a21d7f
commit 7b506d106f

View file

@ -182,7 +182,8 @@ func init() {
}
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile(modload.LoaderState)
moduleLoaderState := modload.NewState()
modload.InitWorkfile(moduleLoaderState)
if generateRunFlag != "" {
var err error
@ -204,8 +205,8 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
// Even if the arguments are .go files, this loop suffices.
printed := false
pkgOpts := load.PackageOpts{IgnoreImports: true}
for _, pkg := range load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args) {
if modload.Enabled(modload.LoaderState) && pkg.Module != nil && !pkg.Module.Main {
for _, pkg := range load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, args) {
if modload.Enabled(moduleLoaderState) && pkg.Module != nil && !pkg.Module.Main {
if !printed {
fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n")
printed = true