From 92aa3e9e98430ae48e3341b01434b5095f3b1c9e Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Wed, 1 Oct 2025 23:04:06 -0400 Subject: [PATCH] cmd/go: inject State parameter into `modcmd.runInit` This command modifies the call tree starting at `modcmd.runInit` 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 runInit' cd .. ./rf-cleanup.zsh Change-Id: Ie8bb8eb0edc2fabceafd9c41a2b11fe2a3532b73 Reviewed-on: https://go-review.googlesource.com/c/go/+/709983 LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob Reviewed-by: Michael Matloob --- src/cmd/go/internal/modcmd/init.go | 2 +- src/cmd/go/internal/modload/init.go | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cmd/go/internal/modcmd/init.go b/src/cmd/go/internal/modcmd/init.go index 618c673bf8..32eb5d06f9 100644 --- a/src/cmd/go/internal/modcmd/init.go +++ b/src/cmd/go/internal/modcmd/init.go @@ -44,5 +44,5 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) { } modload.LoaderState.ForceUseModules = true - modload.CreateModFile(ctx, modPath) // does all the hard work + modload.CreateModFile(modload.LoaderState, ctx, modPath) // does all the hard work } diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 9b8e520d74..bdc201bd94 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -1123,10 +1123,10 @@ func CheckReservedModulePath(path string) error { // translate it to go.mod directives. The resulting build list may not be // exactly the same as in the legacy configuration (for example, we can't get // packages at multiple versions from the same module). -func CreateModFile(ctx context.Context, modPath string) { +func CreateModFile(loaderstate *State, ctx context.Context, modPath string) { modRoot := base.Cwd() - LoaderState.modRoots = []string{modRoot} - Init(LoaderState) + loaderstate.modRoots = []string{modRoot} + Init(loaderstate) modFilePath := modFilePath(modRoot) if _, err := fsys.Stat(modFilePath); err == nil { base.Fatalf("go: %s already exists", modFilePath) @@ -1162,16 +1162,16 @@ func CreateModFile(ctx context.Context, modPath string) { fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath) modFile := new(modfile.File) modFile.AddModuleStmt(modPath) - LoaderState.MainModules = makeMainModules(LoaderState, []module.Version{modFile.Module.Mod}, []string{modRoot}, []*modfile.File{modFile}, []*modFileIndex{nil}, nil) + loaderstate.MainModules = makeMainModules(loaderstate, []module.Version{modFile.Module.Mod}, []string{modRoot}, []*modfile.File{modFile}, []*modFileIndex{nil}, nil) addGoStmt(modFile, modFile.Module.Mod, gover.Local()) // Add the go directive before converted module requirements. - rs := requirementsFromModFiles(LoaderState, ctx, nil, []*modfile.File{modFile}, nil) - rs, err := updateRoots(LoaderState, ctx, rs.direct, rs, nil, nil, false) + rs := requirementsFromModFiles(loaderstate, ctx, nil, []*modfile.File{modFile}, nil) + rs, err := updateRoots(loaderstate, ctx, rs.direct, rs, nil, nil, false) if err != nil { base.Fatal(err) } - LoaderState.requirements = rs - if err := commitRequirements(LoaderState, ctx, WriteOpts{}); err != nil { + loaderstate.requirements = rs + if err := commitRequirements(loaderstate, ctx, WriteOpts{}); err != nil { base.Fatal(err) } -- 2.52.0