]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use local state object in `generate.runGenerate`
authorIan Alexander <jitsu@google.com>
Fri, 10 Oct 2025 01:10:56 +0000 (21:10 -0400)
committerIan Alexander <jitsu@google.com>
Sat, 25 Oct 2025 01:10:58 +0000 (18:10 -0700)
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>

src/cmd/go/internal/generate/generate.go

index 4250916b8d09d67106af406ea4d1017ba2abccce..2a5a5a6764af955f16d5d2993059d15073ba2717 100644 (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