cmd/go: inject State parameter into test.runTest

This command modifies the call tree starting at `test.runTest` 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/test
rf 'inject modload.LoaderState runTest'
cd ..
./rf-cleanup.zsh

Change-Id: I6ee495c3beabdc5568ad338f4998a5927491db1a
Reviewed-on: https://go-review.googlesource.com/c/go/+/709986
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>
This commit is contained in:
Ian Alexander 2025-10-02 09:43:40 -04:00
parent 35e8309be2
commit 441e7194a4
4 changed files with 19 additions and 19 deletions

View file

@ -2797,7 +2797,7 @@ func PackageList(roots []*Package) []*Package {
// TestPackageList returns the list of packages in the dag rooted at roots
// as visited in a depth-first post-order traversal, including the test
// imports of the roots. This ignores errors in test packages.
func TestPackageList(ctx context.Context, opts PackageOpts, roots []*Package) []*Package {
func TestPackageList(loaderstate *modload.State, ctx context.Context, opts PackageOpts, roots []*Package) []*Package {
seen := map[*Package]bool{}
all := []*Package{}
var walk func(*Package)
@ -2813,7 +2813,7 @@ func TestPackageList(ctx context.Context, opts PackageOpts, roots []*Package) []
}
walkTest := func(root *Package, path string) {
var stk ImportStack
p1, err := loadImport(modload.LoaderState, ctx, opts, nil, path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
p1, err := loadImport(loaderstate, ctx, opts, nil, path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
if err != nil && root.Error == nil {
// Assign error importing the package to the importer.
root.Error = err

View file

@ -872,7 +872,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
// Select for coverage all dependencies matching the -coverpkg
// patterns.
plist := load.TestPackageList(ctx, pkgOpts, pkgs)
plist := load.TestPackageList(modload.LoaderState, ctx, pkgOpts, pkgs)
testCoverPkgs = load.SelectCoverPackages(modload.LoaderState, plist, match, "test")
if len(testCoverPkgs) > 0 {
// create a new singleton action that will collect up the
@ -951,7 +951,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
"testing": true,
"time": true,
}
for _, p := range load.TestPackageList(ctx, pkgOpts, pkgs) {
for _, p := range load.TestPackageList(modload.LoaderState, ctx, pkgOpts, pkgs) {
if !skipInstrumentation[p.ImportPath] {
p.Internal.FuzzInstrument = true
}
@ -1048,7 +1048,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
reportSetupFailed(firstErrPkg, firstErrPkg.Error)
continue
}
buildTest, runTest, printTest, perr, err := builderTest(b, ctx, pkgOpts, p, allImports[p], writeCoverMetaAct)
buildTest, runTest, printTest, perr, err := builderTest(modload.LoaderState, b, ctx, pkgOpts, p, allImports[p], writeCoverMetaAct)
if err != nil {
reportErr(perr, err)
reportSetupFailed(perr, err)
@ -1129,7 +1129,7 @@ var windowsBadWords = []string{
"update",
}
func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts, p *load.Package, imported bool, writeCoverMetaAct *work.Action) (buildAction, runAction, printAction *work.Action, perr *load.Package, err error) {
func builderTest(loaderstate *modload.State, b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts, p *load.Package, imported bool, writeCoverMetaAct *work.Action) (buildAction, runAction, printAction *work.Action, perr *load.Package, err error) {
if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
build := b.CompileAction(work.ModeBuild, work.ModeBuild, p)
run := &work.Action{
@ -1157,7 +1157,7 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
run.Deps = append(run.Deps, writeCoverMetaAct)
writeCoverMetaAct.Deps = append(writeCoverMetaAct.Deps, build)
}
addTestVet(b, p, run, nil)
addTestVet(loaderstate, b, p, run, nil)
print := &work.Action{
Mode: "test print",
Actor: work.ActorFunc(builderPrintTest),
@ -1181,7 +1181,7 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
Paths: cfg.BuildCoverPkg,
}
}
pmain, ptest, pxtest, perr := load.TestPackagesFor(modload.LoaderState, ctx, pkgOpts, p, cover)
pmain, ptest, pxtest, perr := load.TestPackagesFor(loaderstate, ctx, pkgOpts, p, cover)
if perr != nil {
return nil, nil, nil, perr, perr.Error
}
@ -1221,7 +1221,7 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
}
}
a := b.LinkAction(modload.LoaderState, work.ModeBuild, work.ModeBuild, pmain)
a := b.LinkAction(loaderstate, work.ModeBuild, work.ModeBuild, pmain)
a.Target = testDir + testBinary + cfg.ExeSuffix
if cfg.Goos == "windows" {
// There are many reserved words on Windows that,
@ -1347,10 +1347,10 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
}
if len(ptest.GoFiles)+len(ptest.CgoFiles) > 0 {
addTestVet(b, ptest, vetRunAction, installAction)
addTestVet(loaderstate, b, ptest, vetRunAction, installAction)
}
if pxtest != nil {
addTestVet(b, pxtest, vetRunAction, installAction)
addTestVet(loaderstate, b, pxtest, vetRunAction, installAction)
}
if installAction != nil {
@ -1365,12 +1365,12 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
return buildAction, runAction, printAction, nil, nil
}
func addTestVet(b *work.Builder, p *load.Package, runAction, installAction *work.Action) {
func addTestVet(loaderstate *modload.State, b *work.Builder, p *load.Package, runAction, installAction *work.Action) {
if testVet.off {
return
}
vet := b.VetAction(work.ModeBuild, work.ModeBuild, p)
vet := b.VetAction(loaderstate, work.ModeBuild, work.ModeBuild, p)
runAction.Deps = append(runAction.Deps, vet)
// Install will clean the build directory.
// Make sure vet runs first.

View file

@ -260,10 +260,10 @@ func run(ctx context.Context, cmd *base.Command, args []string) {
}
if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 {
// The test package includes all the files of primary package.
root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
root.Deps = append(root.Deps, b.VetAction(modload.LoaderState, work.ModeBuild, work.ModeBuild, ptest))
}
if pxtest != nil {
root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
root.Deps = append(root.Deps, b.VetAction(modload.LoaderState, work.ModeBuild, work.ModeBuild, pxtest))
}
}
b.Do(ctx, root)

View file

@ -867,13 +867,13 @@ func (b *Builder) cgoAction(p *load.Package, objdir string, deps []*Action, hasC
// It depends on the action for compiling p.
// If the caller may be causing p to be installed, it is up to the caller
// to make sure that the install depends on (runs after) vet.
func (b *Builder) VetAction(mode, depMode BuildMode, p *load.Package) *Action {
a := b.vetAction(mode, depMode, p)
func (b *Builder) VetAction(loaderstate *modload.State, mode, depMode BuildMode, p *load.Package) *Action {
a := b.vetAction(loaderstate, mode, depMode, p)
a.VetxOnly = false
return a
}
func (b *Builder) vetAction(mode, depMode BuildMode, p *load.Package) *Action {
func (b *Builder) vetAction(loaderstate *modload.State, mode, depMode BuildMode, p *load.Package) *Action {
// Construct vet action.
a := b.cacheAction("vet", p, func() *Action {
a1 := b.CompileAction(mode|ModeVetOnly, depMode, p)
@ -889,7 +889,7 @@ func (b *Builder) vetAction(mode, depMode BuildMode, p *load.Package) *Action {
deps = []*Action{a1}
}
for _, p1 := range p.Internal.Imports {
deps = append(deps, b.vetAction(mode, depMode, p1))
deps = append(deps, b.vetAction(loaderstate, mode, depMode, p1))
}
a := &Action{