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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
This commit is contained in:
Ian Alexander 2025-10-01 23:04:06 -04:00
parent e176dff41c
commit 92aa3e9e98
2 changed files with 9 additions and 9 deletions

View file

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

View file

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