]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use local state object in `list.runList`
authorIan Alexander <jitsu@google.com>
Thu, 9 Oct 2025 00:02:36 +0000 (20:02 -0400)
committerIan Alexander <jitsu@google.com>
Sat, 25 Oct 2025 01:08:37 +0000 (18:08 -0700)
This commit modifies `list.runList` to construct a new modload.State
object using the new constructor instead of the current global
`modload.LoaderState` variable.

This commit is part of the overall effort to eliminate global
modloader state.

[git-generate]
cd src/cmd/go/internal/list
rf '
  add list.go:/func runList\(/-0 var moduleLoaderState *modload.State
  ex {
    import "cmd/go/internal/modload";
    modload.LoaderState -> moduleLoaderState
  }
  add runList://+0 moduleLoaderState := modload.NewState()
  rm list.go:/var moduleLoaderState \*modload.State/
'

Change-Id: I7f45205c1c946189eb05c6178d14ce74ae259547
Reviewed-on: https://go-review.googlesource.com/c/go/+/711124
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/go/internal/list/list.go

index 200aca007d235696d6dc8a25046a06244142b999..086a8c2ca390cb0370f3343a0fd1f1438a09554b 100644 (file)
@@ -419,7 +419,8 @@ func (v *jsonFlag) needAny(fields ...string) bool {
 var nl = []byte{'\n'}
 
 func runList(ctx context.Context, cmd *base.Command, args []string) {
-       modload.InitWorkfile(modload.LoaderState)
+       moduleLoaderState := modload.NewState()
+       modload.InitWorkfile(moduleLoaderState)
 
        if *listFmt != "" && listJson {
                base.Fatalf("go list -f cannot be used with -json")
@@ -427,11 +428,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
        if *listReuse != "" && !*listM {
                base.Fatalf("go list -reuse cannot be used without -m")
        }
-       if *listReuse != "" && modload.HasModRoot(modload.LoaderState) {
+       if *listReuse != "" && modload.HasModRoot(moduleLoaderState) {
                base.Fatalf("go list -reuse cannot be used inside a module")
        }
 
-       work.BuildInit(modload.LoaderState)
+       work.BuildInit(moduleLoaderState)
        out := newTrackingWriter(os.Stdout)
        defer out.w.Flush()
 
@@ -479,7 +480,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                fm := template.FuncMap{
                        "join":    strings.Join,
                        "context": context,
-                       "module":  func(path string) *modinfo.ModulePublic { return modload.ModuleInfo(modload.LoaderState, ctx, path) },
+                       "module":  func(path string) *modinfo.ModulePublic { return modload.ModuleInfo(moduleLoaderState, ctx, path) },
                }
                tmpl, err := template.New("main").Funcs(fm).Parse(*listFmt)
                if err != nil {
@@ -496,12 +497,12 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                }
        }
 
-       modload.Init(modload.LoaderState)
+       modload.Init(moduleLoaderState)
        if *listRetracted {
                if cfg.BuildMod == "vendor" {
                        base.Fatalf("go list -retracted cannot be used when vendoring is enabled")
                }
-               if !modload.Enabled(modload.LoaderState) {
+               if !modload.Enabled(moduleLoaderState) {
                        base.Fatalf("go list -retracted can only be used in module-aware mode")
                }
        }
@@ -525,11 +526,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                        base.Fatalf("go list -test cannot be used with -m")
                }
 
-               if modload.Init(modload.LoaderState); !modload.Enabled(modload.LoaderState) {
+               if modload.Init(moduleLoaderState); !modload.Enabled(moduleLoaderState) {
                        base.Fatalf("go: list -m cannot be used with GO111MODULE=off")
                }
 
-               modload.LoadModFile(modload.LoaderState, ctx) // Sets cfg.BuildMod as a side-effect.
+               modload.LoadModFile(moduleLoaderState, ctx) // Sets cfg.BuildMod as a side-effect.
                if cfg.BuildMod == "vendor" {
                        const actionDisabledFormat = "go: can't %s using the vendor directory\n\t(Use -mod=mod or -mod=readonly to bypass.)"
 
@@ -569,7 +570,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                if *listReuse != "" && len(args) == 0 {
                        base.Fatalf("go: list -m -reuse only has an effect with module@version arguments")
                }
-               mods, err := modload.ListModules(modload.LoaderState, ctx, args, mode, *listReuse)
+               mods, err := modload.ListModules(moduleLoaderState, ctx, args, mode, *listReuse)
                if !*listE {
                        for _, m := range mods {
                                if m.Error != nil {
@@ -613,7 +614,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                SuppressBuildInfo:  !*listExport && !listJsonFields.needAny("Stale", "StaleReason"),
                SuppressEmbedFiles: !*listExport && !listJsonFields.needAny("EmbedFiles", "TestEmbedFiles", "XTestEmbedFiles"),
        }
-       pkgs := load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args)
+       pkgs := load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, args)
        if !*listE {
                w := 0
                for _, pkg := range pkgs {
@@ -648,10 +649,10 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                                                sema.Release(1)
                                                wg.Done()
                                        }
-                                       pmain, ptest, pxtest = load.TestPackagesAndErrors(modload.LoaderState, ctx, done, pkgOpts, p, nil)
+                                       pmain, ptest, pxtest = load.TestPackagesAndErrors(moduleLoaderState, ctx, done, pkgOpts, p, nil)
                                } else {
                                        var perr *load.Package
-                                       pmain, ptest, pxtest, perr = load.TestPackagesFor(modload.LoaderState, ctx, pkgOpts, p, nil)
+                                       pmain, ptest, pxtest, perr = load.TestPackagesFor(moduleLoaderState, ctx, pkgOpts, p, nil)
                                        if perr != nil {
                                                base.Fatalf("go: can't load test package: %s", perr.Error)
                                        }
@@ -713,7 +714,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
        // Do we need to run a build to gather information?
        needStale := (listJson && listJsonFields.needAny("Stale", "StaleReason")) || strings.Contains(*listFmt, ".Stale")
        if needStale || *listExport || *listCompiled {
-               b := work.NewBuilder("", modload.LoaderState.VendorDirOrEmpty)
+               b := work.NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
                if *listE {
                        b.AllowErrors = true
                }
@@ -727,13 +728,13 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                b.NeedExport = *listExport
                b.NeedCompiledGoFiles = *listCompiled
                if cfg.BuildCover {
-                       load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
+                       load.PrepareForCoverageBuild(moduleLoaderState, pkgs)
                }
                a := &work.Action{}
                // TODO: Use pkgsFilter?
                for _, p := range pkgs {
                        if len(p.GoFiles)+len(p.CgoFiles) > 0 {
-                               a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, work.ModeInstall, work.ModeInstall, p))
+                               a.Deps = append(a.Deps, b.AutoAction(moduleLoaderState, work.ModeInstall, work.ModeInstall, p))
                        }
                }
                b.Do(ctx, a)
@@ -741,8 +742,8 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
 
        for _, p := range pkgs {
                // Show vendor-expanded paths in listing
-               p.TestImports = p.Resolve(modload.LoaderState, p.TestImports)
-               p.XTestImports = p.Resolve(modload.LoaderState, p.XTestImports)
+               p.TestImports = p.Resolve(moduleLoaderState, p.TestImports)
+               p.XTestImports = p.Resolve(moduleLoaderState, p.XTestImports)
                p.DepOnly = !cmdline[p]
 
                if *listCompiled {
@@ -850,7 +851,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                        if *listRetracted {
                                mode |= modload.ListRetracted
                        }
-                       rmods, err := modload.ListModules(modload.LoaderState, ctx, args, mode, *listReuse)
+                       rmods, err := modload.ListModules(moduleLoaderState, ctx, args, mode, *listReuse)
                        if err != nil && !*listE {
                                base.Error(err)
                        }