From 0f820aca291dfd091c87c045147715cc1bf7266b Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Wed, 1 Oct 2025 23:18:10 -0400 Subject: [PATCH] cmd/go: inject State parameter into `modcmd.runVendor` This command modifies the call tree starting at `modcmd.runVendor` 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 runVendor' cd .. ./rf-cleanup.zsh Change-Id: I0572e165d291e34d212ded9a420871688b7915ad Reviewed-on: https://go-review.googlesource.com/c/go/+/709984 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob --- src/cmd/go/internal/modcmd/vendor.go | 44 +++++++++++++-------------- src/cmd/go/internal/modget/get.go | 4 +-- src/cmd/go/internal/modload/init.go | 8 ++--- src/cmd/go/internal/workcmd/vendor.go | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/cmd/go/internal/modcmd/vendor.go b/src/cmd/go/internal/modcmd/vendor.go index 45d0aeff9c..c7be3899f3 100644 --- a/src/cmd/go/internal/modcmd/vendor.go +++ b/src/cmd/go/internal/modcmd/vendor.go @@ -70,15 +70,15 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) { if modload.WorkFilePath(modload.LoaderState) != "" { base.Fatalf("go: 'go mod vendor' cannot be run in workspace mode. Run 'go work vendor' to vendor the workspace or set 'GOWORK=off' to exit workspace mode.") } - RunVendor(ctx, vendorE, vendorO, args) + RunVendor(modload.LoaderState, ctx, vendorE, vendorO, args) } -func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) { +func RunVendor(loaderstate *modload.State, ctx context.Context, vendorE bool, vendorO string, args []string) { if len(args) != 0 { base.Fatalf("go: 'go mod vendor' accepts no arguments") } - modload.LoaderState.ForceUseModules = true - modload.LoaderState.RootMode = modload.NeedRoot + loaderstate.ForceUseModules = true + loaderstate.RootMode = modload.NeedRoot loadOpts := modload.PackageOpts{ Tags: imports.AnyTags(), @@ -88,7 +88,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) AllowErrors: vendorE, SilenceMissingStdImports: true, } - _, pkgs := modload.LoadPackages(modload.LoaderState, ctx, loadOpts, "all") + _, pkgs := modload.LoadPackages(loaderstate, ctx, loadOpts, "all") var vdir string switch { @@ -97,7 +97,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) case vendorO != "": vdir = filepath.Join(base.Cwd(), vendorO) default: - vdir = filepath.Join(modload.VendorDir(modload.LoaderState)) + vdir = filepath.Join(modload.VendorDir(loaderstate)) } if err := os.RemoveAll(vdir); err != nil { base.Fatal(err) @@ -106,7 +106,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) modpkgs := make(map[module.Version][]string) for _, pkg := range pkgs { m := modload.PackageModule(pkg) - if m.Path == "" || modload.LoaderState.MainModules.Contains(m.Path) { + if m.Path == "" || loaderstate.MainModules.Contains(m.Path) { continue } modpkgs[m] = append(modpkgs[m], pkg) @@ -116,13 +116,13 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) includeAllReplacements := false includeGoVersions := false isExplicit := map[module.Version]bool{} - gv := modload.LoaderState.MainModules.GoVersion(modload.LoaderState) - if gover.Compare(gv, "1.14") >= 0 && (modload.FindGoWork(modload.LoaderState, base.Cwd()) != "" || modload.ModFile().Go != nil) { + gv := loaderstate.MainModules.GoVersion(loaderstate) + if gover.Compare(gv, "1.14") >= 0 && (modload.FindGoWork(loaderstate, base.Cwd()) != "" || modload.ModFile(loaderstate).Go != nil) { // If the Go version is at least 1.14, annotate all explicit 'require' and // 'replace' targets found in the go.mod file so that we can perform a // stronger consistency check when -mod=vendor is set. - for _, m := range modload.LoaderState.MainModules.Versions() { - if modFile := modload.LoaderState.MainModules.ModFile(m); modFile != nil { + for _, m := range loaderstate.MainModules.Versions() { + if modFile := loaderstate.MainModules.ModFile(m); modFile != nil { for _, r := range modFile.Require { isExplicit[r.Mod] = true } @@ -156,20 +156,20 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) w = io.MultiWriter(&buf, os.Stderr) } - if modload.LoaderState.MainModules.WorkFile() != nil { + if loaderstate.MainModules.WorkFile() != nil { fmt.Fprintf(w, "## workspace\n") } replacementWritten := make(map[module.Version]bool) for _, m := range vendorMods { - replacement := modload.Replacement(modload.LoaderState, m) + replacement := modload.Replacement(loaderstate, m) line := moduleLine(m, replacement) replacementWritten[m] = true io.WriteString(w, line) goVersion := "" if includeGoVersions { - goVersion = modload.ModuleInfo(modload.LoaderState, ctx, m.Path).GoVersion + goVersion = modload.ModuleInfo(loaderstate, ctx, m.Path).GoVersion } switch { case isExplicit[m] && goVersion != "": @@ -184,7 +184,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) sort.Strings(pkgs) for _, pkg := range pkgs { fmt.Fprintf(w, "%s\n", pkg) - vendorPkg(vdir, pkg) + vendorPkg(loaderstate, vdir, pkg) } } @@ -192,8 +192,8 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) // Record unused and wildcard replacements at the end of the modules.txt file: // without access to the complete build list, the consumer of the vendor // directory can't otherwise determine that those replacements had no effect. - for _, m := range modload.LoaderState.MainModules.Versions() { - if workFile := modload.LoaderState.MainModules.WorkFile(); workFile != nil { + for _, m := range loaderstate.MainModules.Versions() { + if workFile := loaderstate.MainModules.WorkFile(); workFile != nil { for _, r := range workFile.Replace { if replacementWritten[r.Old] { // We already recorded this replacement. @@ -208,14 +208,14 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string) } } } - if modFile := modload.LoaderState.MainModules.ModFile(m); modFile != nil { + if modFile := loaderstate.MainModules.ModFile(m); modFile != nil { for _, r := range modFile.Replace { if replacementWritten[r.Old] { // We already recorded this replacement. continue } replacementWritten[r.Old] = true - rNew := modload.Replacement(modload.LoaderState, r.Old) + rNew := modload.Replacement(loaderstate, r.Old) if rNew == (module.Version{}) { // There is no replacement. Don't try to write it. continue @@ -268,8 +268,8 @@ func moduleLine(m, r module.Version) string { return b.String() } -func vendorPkg(vdir, pkg string) { - src, realPath, _ := modload.Lookup(modload.LoaderState, "", false, pkg) +func vendorPkg(loaderstate *modload.State, vdir, pkg string) { + src, realPath, _ := modload.Lookup(loaderstate, "", false, pkg) if src == "" { base.Errorf("internal error: no pkg for %s\n", pkg) return @@ -315,7 +315,7 @@ func vendorPkg(vdir, pkg string) { } } var embedPatterns []string - if gover.Compare(modload.LoaderState.MainModules.GoVersion(modload.LoaderState), "1.22") >= 0 { + if gover.Compare(loaderstate.MainModules.GoVersion(loaderstate), "1.22") >= 0 { embedPatterns = bp.EmbedPatterns } else { // Maintain the behavior of https://github.com/golang/go/issues/63473 diff --git a/src/cmd/go/internal/modget/get.go b/src/cmd/go/internal/modget/get.go index b8018c04c9..8735dc4a59 100644 --- a/src/cmd/go/internal/modget/get.go +++ b/src/cmd/go/internal/modget/get.go @@ -410,7 +410,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) { } // Everything succeeded. Update go.mod. - oldReqs := reqsFromGoMod(modload.ModFile()) + oldReqs := reqsFromGoMod(modload.ModFile(modload.LoaderState)) if err := modload.WriteGoMod(modload.LoaderState, ctx, opts); err != nil { // A TooNewError can happen for 'go get go@newversion' @@ -421,7 +421,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) { toolchain.SwitchOrFatal(modload.LoaderState, ctx, err) } - newReqs := reqsFromGoMod(modload.ModFile()) + newReqs := reqsFromGoMod(modload.ModFile(modload.LoaderState)) r.reportChanges(oldReqs, newReqs) if gowork := modload.FindGoWork(modload.LoaderState, base.Cwd()); gowork != "" { diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index bdc201bd94..eeb6288f61 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -314,11 +314,11 @@ const ( // will be lost at the next call to WriteGoMod. // To make permanent changes to the require statements // in go.mod, edit it before loading. -func ModFile() *modfile.File { - Init(LoaderState) - modFile := LoaderState.MainModules.ModFile(LoaderState.MainModules.mustGetSingleMainModule(LoaderState)) +func ModFile(loaderstate *State) *modfile.File { + Init(loaderstate) + modFile := loaderstate.MainModules.ModFile(loaderstate.MainModules.mustGetSingleMainModule(loaderstate)) if modFile == nil { - die(LoaderState) + die(loaderstate) } return modFile } diff --git a/src/cmd/go/internal/workcmd/vendor.go b/src/cmd/go/internal/workcmd/vendor.go index 36c1f7b522..f3f074c156 100644 --- a/src/cmd/go/internal/workcmd/vendor.go +++ b/src/cmd/go/internal/workcmd/vendor.go @@ -51,5 +51,5 @@ func runVendor(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)") } - modcmd.RunVendor(ctx, vendorE, vendorO, args) + modcmd.RunVendor(modload.LoaderState, ctx, vendorE, vendorO, args) } -- 2.52.0