]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: inject State parameter into `modcmd.runVendor`
authorIan Alexander <jitsu@google.com>
Thu, 2 Oct 2025 03:18:10 +0000 (23:18 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 19:59:10 +0000 (12:59 -0700)
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 <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modcmd/vendor.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/workcmd/vendor.go

index 45d0aeff9c36de3501a5a38ab87cb7891b8150ab..c7be3899f3886f79f30a10bc4667237e9cf01f12 100644 (file)
@@ -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
index b8018c04c987f4e35d2579f20bfb9f70e6183706..8735dc4a59649cf12c11945e15128e2184a15fc6 100644 (file)
@@ -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 != "" {
index bdc201bd94d498444b0b912aee7953e7a1bc6127..eeb6288f61a12e26a014f0d21c3a0d7165fe540e 100644 (file)
@@ -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
 }
index 36c1f7b522f9e736e57c36411a9309bcd5d2d26a..f3f074c156c9b6511c984441ded60b73c0598d69 100644 (file)
@@ -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)
 }