cmd/go: inject State parameter into modcmd.runDownload

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>
This commit is contained in:
Ian Alexander 2025-10-08 14:51:13 -04:00
parent e7c66a58d5
commit e176dff41c
7 changed files with 20 additions and 20 deletions

View 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)
}
}

View 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)
}

View 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)
}

View 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)
}

View 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
}

View 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")

View 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))
}