]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: inject State parameter into `modcmd.runDownload`
authorIan Alexander <jitsu@google.com>
Wed, 8 Oct 2025 18:51:13 +0000 (14:51 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 19:56:59 +0000 (12:56 -0700)
This command modifies the call tree starting at `modcmd.runDownload`
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/modcmd
rf 'inject modload.LoaderState runDownload'
cd ..
./rf-cleanup.zsh

Change-Id: I64cce3e631a2614b7fabe49205d9d41fc9ba24de
Reviewed-on: https://go-review.googlesource.com/c/go/+/710299
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/modcmd/download.go
src/cmd/go/internal/modcmd/graph.go
src/cmd/go/internal/modcmd/verify.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/buildlist.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/workcmd/sync.go

index 63eb6886a1a15786084f4db31dc40aa2a76c63ce..1b2f850d1b483690d0e7c638c25f54bfe8d74113 100644 (file)
@@ -153,7 +153,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
                                // However, we also need to load the full module graph, to ensure that
                                // we have downloaded enough of the module graph to run 'go list all',
                                // 'go mod graph', and similar commands.
-                               _, err := modload.LoadModGraph(ctx, "")
+                               _, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
                                if err != nil {
                                        // TODO(#64008): call base.Fatalf instead of toolchain.SwitchOrFatal
                                        // here, since we can only reach this point with an outdated toolchain
@@ -231,7 +231,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
                // TODO(#64008): In the future, report an error if go.mod or go.sum need to
                // be updated after loading the build list. This may require setting
                // the mode to "mod" or "readonly" depending on haveExplicitArgs.
-               if err := modload.WriteGoMod(ctx, modload.WriteOpts{}); err != nil {
+               if err := modload.WriteGoMod(modload.LoaderState, ctx, modload.WriteOpts{}); err != nil {
                        base.Fatal(err)
                }
        }
@@ -348,7 +348,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
        // Don't save sums for 'go mod download' without arguments unless we're in
        // workspace mode; see comment above.
        if haveExplicitArgs || modload.WorkFilePath(modload.LoaderState) != "" {
-               if err := modload.WriteGoMod(ctx, modload.WriteOpts{}); err != nil {
+               if err := modload.WriteGoMod(modload.LoaderState, ctx, modload.WriteOpts{}); err != nil {
                        base.Error(err)
                }
        }
index aa3c9f45405240a0df60ecf009698b13c1e2c512..2a1bad0e0efffd1e86df89b159b180ca4f9e8649 100644 (file)
@@ -68,7 +68,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) {
                })
        }
 
-       mg, err := modload.LoadModGraph(ctx, goVersion)
+       mg, err := modload.LoadModGraph(modload.LoaderState, ctx, goVersion)
        if err != nil {
                base.Fatal(err)
        }
index d8227bcd5455a3d80b05d9ecef6cc5369d10da59..bf803fd2ebbaf5a52966ffd8d960bfd9057b3b66 100644 (file)
@@ -57,7 +57,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
        type token struct{}
        sem := make(chan token, runtime.GOMAXPROCS(0))
 
-       mg, err := modload.LoadModGraph(ctx, "")
+       mg, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
        if err != nil {
                base.Fatal(err)
        }
index 9de24c4508a5ee5fe1aa4237bda3721833bf0879..b8018c04c987f4e35d2579f20bfb9f70e6183706 100644 (file)
@@ -412,7 +412,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
        // Everything succeeded. Update go.mod.
        oldReqs := reqsFromGoMod(modload.ModFile())
 
-       if err := modload.WriteGoMod(ctx, opts); err != nil {
+       if err := modload.WriteGoMod(modload.LoaderState, ctx, opts); err != nil {
                // A TooNewError can happen for 'go get go@newversion'
                // when all the required modules are old enough
                // but the command line is not.
@@ -556,7 +556,7 @@ type matchInModuleKey struct {
 func newResolver(ctx context.Context, queries []*query) *resolver {
        // LoadModGraph also sets modload.Target, which is needed by various resolver
        // methods.
-       mg, err := modload.LoadModGraph(ctx, "")
+       mg, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
        if err != nil {
                toolchain.SwitchOrFatal(modload.LoaderState, ctx, err)
        }
@@ -2033,7 +2033,7 @@ func (r *resolver) updateBuildList(ctx context.Context, additions []module.Versi
                return false
        }
 
-       mg, err := modload.LoadModGraph(ctx, "")
+       mg, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
        if err != nil {
                toolchain.SwitchOrFatal(modload.LoaderState, ctx, err)
        }
index 54ec4d23ebe08e0983a5e12ced9fc02037975a77..bd3fc3ec75713b938399facbf53f6291fa5778a2 100644 (file)
@@ -552,14 +552,14 @@ func (mg *ModuleGraph) allRootsSelected(loaderstate *State) bool {
 // Modules are loaded automatically (and lazily) in LoadPackages:
 // LoadModGraph need only be called if LoadPackages is not,
 // typically in commands that care about modules but no particular package.
-func LoadModGraph(ctx context.Context, goVersion string) (*ModuleGraph, error) {
-       rs, err := loadModFile(LoaderState, ctx, nil)
+func LoadModGraph(loaderstate *State, ctx context.Context, goVersion string) (*ModuleGraph, error) {
+       rs, err := loadModFile(loaderstate, ctx, nil)
        if err != nil {
                return nil, err
        }
 
        if goVersion != "" {
-               v, _ := rs.rootSelected(LoaderState, "go")
+               v, _ := rs.rootSelected(loaderstate, "go")
                if gover.Compare(v, gover.GoStrictVersion) >= 0 && gover.Compare(goVersion, v) < 0 {
                        return nil, fmt.Errorf("requested Go version %s cannot load module graph (requires Go >= %s)", goVersion, v)
                }
@@ -569,17 +569,17 @@ func LoadModGraph(ctx context.Context, goVersion string) (*ModuleGraph, error) {
                        // Use newRequirements instead of convertDepth because convertDepth
                        // also updates roots; here, we want to report the unmodified roots
                        // even though they may seem inconsistent.
-                       rs = newRequirements(LoaderState, unpruned, rs.rootModules, rs.direct)
+                       rs = newRequirements(loaderstate, unpruned, rs.rootModules, rs.direct)
                }
 
-               return rs.Graph(LoaderState, ctx)
+               return rs.Graph(loaderstate, ctx)
        }
 
-       rs, mg, err := expandGraph(LoaderState, ctx, rs)
+       rs, mg, err := expandGraph(loaderstate, ctx, rs)
        if err != nil {
                return nil, err
        }
-       LoaderState.requirements = rs
+       loaderstate.requirements = rs
        return mg, nil
 }
 
index 9182f60ee7b1bbd12f4f197184357d7d4b844247..9b8e520d74a47b087e4db5f645e531af31e88ec9 100644 (file)
@@ -1808,9 +1808,9 @@ type WriteOpts struct {
 }
 
 // WriteGoMod writes the current build list back to go.mod.
-func WriteGoMod(ctx context.Context, opts WriteOpts) error {
-       LoaderState.requirements = LoadModFile(LoaderState, ctx)
-       return commitRequirements(LoaderState, ctx, opts)
+func WriteGoMod(loaderstate *State, ctx context.Context, opts WriteOpts) error {
+       loaderstate.requirements = LoadModFile(loaderstate, ctx)
+       return commitRequirements(loaderstate, ctx, opts)
 }
 
 var errNoChange = errors.New("no update needed")
index dfff0c29d227b1f5064ca63c13000d0b15764720..7f45909de5c3771d38a6a444a72b54878c5f8435 100644 (file)
@@ -54,7 +54,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
                base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
        }
 
-       _, err := modload.LoadModGraph(ctx, "")
+       _, err := modload.LoadModGraph(modload.LoaderState, ctx, "")
        if err != nil {
                toolchain.SwitchOrFatal(modload.LoaderState, ctx, err)
        }
@@ -129,7 +129,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
                                SilenceMissingStdImports: true,
                                SilencePackageErrors:     true,
                        }, "all")
-                       modload.WriteGoMod(ctx, modload.WriteOpts{})
+                       modload.WriteGoMod(modload.LoaderState, ctx, modload.WriteOpts{})
                }
                goV = gover.Max(goV, modload.LoaderState.MainModules.GoVersion(modload.LoaderState))
        }