Skip to content

Commit 12ec09f

Browse files
committed
cmd/go: use local state object in work.runBuild and work.runInstall
This commit modifies `runBuild` and `runInstall` 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/work rf ' add build.go:/func runBuild\(/-0 var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runBuild://+0 moduleLoaderState := modload.NewState() add runInstall://+0 moduleLoaderState := modload.NewState() rm build.go:/var moduleLoaderState \*modload.State/ ' Change-Id: I1137e0b898a5bda8697dce8713f96f238ae8b76c Reviewed-on: https://go-review.googlesource.com/c/go/+/711135 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 643f80a commit 12ec09f

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/cmd/go/internal/work/build.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,17 @@ func oneMainPkg(pkgs []*load.Package) []*load.Package {
459459
var pkgsFilter = func(pkgs []*load.Package) []*load.Package { return pkgs }
460460

461461
func runBuild(ctx context.Context, cmd *base.Command, args []string) {
462-
modload.InitWorkfile(modload.LoaderState)
463-
BuildInit(modload.LoaderState)
464-
b := NewBuilder("", modload.LoaderState.VendorDirOrEmpty)
462+
moduleLoaderState := modload.NewState()
463+
modload.InitWorkfile(moduleLoaderState)
464+
BuildInit(moduleLoaderState)
465+
b := NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
465466
defer func() {
466467
if err := b.Close(); err != nil {
467468
base.Fatal(err)
468469
}
469470
}()
470471

471-
pkgs := load.PackagesAndErrors(modload.LoaderState, ctx, load.PackageOpts{AutoVCS: true}, args)
472+
pkgs := load.PackagesAndErrors(moduleLoaderState, ctx, load.PackageOpts{AutoVCS: true}, args)
472473
load.CheckPackageErrors(pkgs)
473474

474475
explicitO := len(cfg.BuildO) > 0
@@ -503,7 +504,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
503504
}
504505

505506
if cfg.BuildCover {
506-
load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
507+
load.PrepareForCoverageBuild(moduleLoaderState, pkgs)
507508
}
508509

509510
if cfg.BuildO != "" {
@@ -527,7 +528,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
527528
p.Target += cfg.ExeSuffix
528529
p.Stale = true
529530
p.StaleReason = "build -o flag in use"
530-
a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, ModeInstall, depMode, p))
531+
a.Deps = append(a.Deps, b.AutoAction(moduleLoaderState, ModeInstall, depMode, p))
531532
}
532533
if len(a.Deps) == 0 {
533534
base.Fatalf("go: no main packages to build")
@@ -544,17 +545,17 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
544545
p.Target = cfg.BuildO
545546
p.Stale = true // must build - not up to date
546547
p.StaleReason = "build -o flag in use"
547-
a := b.AutoAction(modload.LoaderState, ModeInstall, depMode, p)
548+
a := b.AutoAction(moduleLoaderState, ModeInstall, depMode, p)
548549
b.Do(ctx, a)
549550
return
550551
}
551552

552553
a := &Action{Mode: "go build"}
553554
for _, p := range pkgs {
554-
a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, ModeBuild, depMode, p))
555+
a.Deps = append(a.Deps, b.AutoAction(moduleLoaderState, ModeBuild, depMode, p))
555556
}
556557
if cfg.BuildBuildmode == "shared" {
557-
a = b.buildmodeShared(modload.LoaderState, ModeBuild, depMode, args, pkgs, a)
558+
a = b.buildmodeShared(moduleLoaderState, ModeBuild, depMode, args, pkgs, a)
558559
}
559560
b.Do(ctx, a)
560561
}
@@ -687,17 +688,18 @@ func libname(args []string, pkgs []*load.Package) (string, error) {
687688
}
688689

689690
func runInstall(ctx context.Context, cmd *base.Command, args []string) {
691+
moduleLoaderState := modload.NewState()
690692
for _, arg := range args {
691693
if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) {
692-
installOutsideModule(modload.LoaderState, ctx, args)
694+
installOutsideModule(moduleLoaderState, ctx, args)
693695
return
694696
}
695697
}
696698

697-
modload.InitWorkfile(modload.LoaderState)
698-
BuildInit(modload.LoaderState)
699-
pkgs := load.PackagesAndErrors(modload.LoaderState, ctx, load.PackageOpts{AutoVCS: true}, args)
700-
if cfg.ModulesEnabled && !modload.HasModRoot(modload.LoaderState) {
699+
modload.InitWorkfile(moduleLoaderState)
700+
BuildInit(moduleLoaderState)
701+
pkgs := load.PackagesAndErrors(moduleLoaderState, ctx, load.PackageOpts{AutoVCS: true}, args)
702+
if cfg.ModulesEnabled && !modload.HasModRoot(moduleLoaderState) {
701703
haveErrors := false
702704
allMissingErrors := true
703705
for _, pkg := range pkgs {
@@ -722,10 +724,10 @@ func runInstall(ctx context.Context, cmd *base.Command, args []string) {
722724
load.CheckPackageErrors(pkgs)
723725

724726
if cfg.BuildCover {
725-
load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
727+
load.PrepareForCoverageBuild(moduleLoaderState, pkgs)
726728
}
727729

728-
InstallPackages(modload.LoaderState, ctx, args, pkgs)
730+
InstallPackages(moduleLoaderState, ctx, args, pkgs)
729731
}
730732

731733
// omitTestOnly returns pkgs with test-only packages removed.

0 commit comments

Comments
 (0)