Skip to content

Commit ea9cf26

Browse files
committed
cmd/go: use local state object in list.runList
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 <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 9926e11 commit ea9cf26

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,20 @@ func (v *jsonFlag) needAny(fields ...string) bool {
419419
var nl = []byte{'\n'}
420420

421421
func runList(ctx context.Context, cmd *base.Command, args []string) {
422-
modload.InitWorkfile(modload.LoaderState)
422+
moduleLoaderState := modload.NewState()
423+
modload.InitWorkfile(moduleLoaderState)
423424

424425
if *listFmt != "" && listJson {
425426
base.Fatalf("go list -f cannot be used with -json")
426427
}
427428
if *listReuse != "" && !*listM {
428429
base.Fatalf("go list -reuse cannot be used without -m")
429430
}
430-
if *listReuse != "" && modload.HasModRoot(modload.LoaderState) {
431+
if *listReuse != "" && modload.HasModRoot(moduleLoaderState) {
431432
base.Fatalf("go list -reuse cannot be used inside a module")
432433
}
433434

434-
work.BuildInit(modload.LoaderState)
435+
work.BuildInit(moduleLoaderState)
435436
out := newTrackingWriter(os.Stdout)
436437
defer out.w.Flush()
437438

@@ -479,7 +480,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
479480
fm := template.FuncMap{
480481
"join": strings.Join,
481482
"context": context,
482-
"module": func(path string) *modinfo.ModulePublic { return modload.ModuleInfo(modload.LoaderState, ctx, path) },
483+
"module": func(path string) *modinfo.ModulePublic { return modload.ModuleInfo(moduleLoaderState, ctx, path) },
483484
}
484485
tmpl, err := template.New("main").Funcs(fm).Parse(*listFmt)
485486
if err != nil {
@@ -496,12 +497,12 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
496497
}
497498
}
498499

499-
modload.Init(modload.LoaderState)
500+
modload.Init(moduleLoaderState)
500501
if *listRetracted {
501502
if cfg.BuildMod == "vendor" {
502503
base.Fatalf("go list -retracted cannot be used when vendoring is enabled")
503504
}
504-
if !modload.Enabled(modload.LoaderState) {
505+
if !modload.Enabled(moduleLoaderState) {
505506
base.Fatalf("go list -retracted can only be used in module-aware mode")
506507
}
507508
}
@@ -525,11 +526,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
525526
base.Fatalf("go list -test cannot be used with -m")
526527
}
527528

528-
if modload.Init(modload.LoaderState); !modload.Enabled(modload.LoaderState) {
529+
if modload.Init(moduleLoaderState); !modload.Enabled(moduleLoaderState) {
529530
base.Fatalf("go: list -m cannot be used with GO111MODULE=off")
530531
}
531532

532-
modload.LoadModFile(modload.LoaderState, ctx) // Sets cfg.BuildMod as a side-effect.
533+
modload.LoadModFile(moduleLoaderState, ctx) // Sets cfg.BuildMod as a side-effect.
533534
if cfg.BuildMod == "vendor" {
534535
const actionDisabledFormat = "go: can't %s using the vendor directory\n\t(Use -mod=mod or -mod=readonly to bypass.)"
535536

@@ -569,7 +570,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
569570
if *listReuse != "" && len(args) == 0 {
570571
base.Fatalf("go: list -m -reuse only has an effect with module@version arguments")
571572
}
572-
mods, err := modload.ListModules(modload.LoaderState, ctx, args, mode, *listReuse)
573+
mods, err := modload.ListModules(moduleLoaderState, ctx, args, mode, *listReuse)
573574
if !*listE {
574575
for _, m := range mods {
575576
if m.Error != nil {
@@ -613,7 +614,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
613614
SuppressBuildInfo: !*listExport && !listJsonFields.needAny("Stale", "StaleReason"),
614615
SuppressEmbedFiles: !*listExport && !listJsonFields.needAny("EmbedFiles", "TestEmbedFiles", "XTestEmbedFiles"),
615616
}
616-
pkgs := load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, args)
617+
pkgs := load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, args)
617618
if !*listE {
618619
w := 0
619620
for _, pkg := range pkgs {
@@ -648,10 +649,10 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
648649
sema.Release(1)
649650
wg.Done()
650651
}
651-
pmain, ptest, pxtest = load.TestPackagesAndErrors(modload.LoaderState, ctx, done, pkgOpts, p, nil)
652+
pmain, ptest, pxtest = load.TestPackagesAndErrors(moduleLoaderState, ctx, done, pkgOpts, p, nil)
652653
} else {
653654
var perr *load.Package
654-
pmain, ptest, pxtest, perr = load.TestPackagesFor(modload.LoaderState, ctx, pkgOpts, p, nil)
655+
pmain, ptest, pxtest, perr = load.TestPackagesFor(moduleLoaderState, ctx, pkgOpts, p, nil)
655656
if perr != nil {
656657
base.Fatalf("go: can't load test package: %s", perr.Error)
657658
}
@@ -713,7 +714,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
713714
// Do we need to run a build to gather information?
714715
needStale := (listJson && listJsonFields.needAny("Stale", "StaleReason")) || strings.Contains(*listFmt, ".Stale")
715716
if needStale || *listExport || *listCompiled {
716-
b := work.NewBuilder("", modload.LoaderState.VendorDirOrEmpty)
717+
b := work.NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
717718
if *listE {
718719
b.AllowErrors = true
719720
}
@@ -727,22 +728,22 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
727728
b.NeedExport = *listExport
728729
b.NeedCompiledGoFiles = *listCompiled
729730
if cfg.BuildCover {
730-
load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
731+
load.PrepareForCoverageBuild(moduleLoaderState, pkgs)
731732
}
732733
a := &work.Action{}
733734
// TODO: Use pkgsFilter?
734735
for _, p := range pkgs {
735736
if len(p.GoFiles)+len(p.CgoFiles) > 0 {
736-
a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, work.ModeInstall, work.ModeInstall, p))
737+
a.Deps = append(a.Deps, b.AutoAction(moduleLoaderState, work.ModeInstall, work.ModeInstall, p))
737738
}
738739
}
739740
b.Do(ctx, a)
740741
}
741742

742743
for _, p := range pkgs {
743744
// Show vendor-expanded paths in listing
744-
p.TestImports = p.Resolve(modload.LoaderState, p.TestImports)
745-
p.XTestImports = p.Resolve(modload.LoaderState, p.XTestImports)
745+
p.TestImports = p.Resolve(moduleLoaderState, p.TestImports)
746+
p.XTestImports = p.Resolve(moduleLoaderState, p.XTestImports)
746747
p.DepOnly = !cmdline[p]
747748

748749
if *listCompiled {
@@ -850,7 +851,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
850851
if *listRetracted {
851852
mode |= modload.ListRetracted
852853
}
853-
rmods, err := modload.ListModules(modload.LoaderState, ctx, args, mode, *listReuse)
854+
rmods, err := modload.ListModules(moduleLoaderState, ctx, args, mode, *listReuse)
854855
if err != nil && !*listE {
855856
base.Error(err)
856857
}

0 commit comments

Comments
 (0)