]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: inject State parameter into `modcmd.runInit`
authorIan Alexander <jitsu@google.com>
Thu, 2 Oct 2025 03:04:06 +0000 (23:04 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 19:57:12 +0000 (12:57 -0700)
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>
src/cmd/go/internal/modcmd/init.go
src/cmd/go/internal/modload/init.go

index 618c673bf86f24ed085b20b3fbd9aaccee3bcabc..32eb5d06f9ee23177ce30e294081da3bb6bf6101 100644 (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
 }
index 9b8e520d74a47b087e4db5f645e531af31e88ec9..bdc201bd94d498444b0b912aee7953e7a1bc6127 100644 (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)
        }