cmd/go: use local state object in test.runTest

This commit modifies `test.runTest` 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/test
rf '
  add test.go:/func runTest\(/-0 var moduleLoaderState *modload.State
  ex {
    import "cmd/go/internal/modload";
    modload.LoaderState -> moduleLoaderState
  }
  add runTest://+0 moduleLoaderState := modload.NewState()
  rm test.go:/var moduleLoaderState \*modload.State/
'

Change-Id: Ia387160f30818714ab578c5b78713e532cd394df
Reviewed-on: https://go-review.googlesource.com/c/go/+/711127
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
This commit is contained in:
Ian Alexander 2025-10-08 20:21:59 -04:00
parent fe345ff2ae
commit 2e12c5db11

View file

@ -682,8 +682,9 @@ var defaultVetFlags = []string{
} }
func runTest(ctx context.Context, cmd *base.Command, args []string) { func runTest(ctx context.Context, cmd *base.Command, args []string) {
moduleLoaderState := modload.NewState()
pkgArgs, testArgs = testFlags(args) pkgArgs, testArgs = testFlags(args)
modload.InitWorkfile(modload.LoaderState) // The test command does custom flag processing; initialize workspaces after that. modload.InitWorkfile(moduleLoaderState) // The test command does custom flag processing; initialize workspaces after that.
if cfg.DebugTrace != "" { if cfg.DebugTrace != "" {
var close func() error var close func() error
@ -704,13 +705,13 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
work.FindExecCmd() // initialize cached result work.FindExecCmd() // initialize cached result
work.BuildInit(modload.LoaderState) work.BuildInit(moduleLoaderState)
work.VetFlags = testVet.flags work.VetFlags = testVet.flags
work.VetExplicit = testVet.explicit work.VetExplicit = testVet.explicit
work.VetTool = base.Tool("vet") work.VetTool = base.Tool("vet")
pkgOpts := load.PackageOpts{ModResolveTests: true} pkgOpts := load.PackageOpts{ModResolveTests: true}
pkgs = load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, pkgArgs) pkgs = load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, pkgArgs)
// We *don't* call load.CheckPackageErrors here because we want to report // We *don't* call load.CheckPackageErrors here because we want to report
// loading errors as per-package test setup errors later. // loading errors as per-package test setup errors later.
if len(pkgs) == 0 { if len(pkgs) == 0 {
@ -736,12 +737,12 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
// the module cache (or permanently alter the behavior of std tests for all // the module cache (or permanently alter the behavior of std tests for all
// users) by writing the failing input to the package's testdata directory. // users) by writing the failing input to the package's testdata directory.
// (See https://golang.org/issue/48495 and test_fuzz_modcache.txt.) // (See https://golang.org/issue/48495 and test_fuzz_modcache.txt.)
mainMods := modload.LoaderState.MainModules mainMods := moduleLoaderState.MainModules
if m := pkgs[0].Module; m != nil && m.Path != "" { if m := pkgs[0].Module; m != nil && m.Path != "" {
if !mainMods.Contains(m.Path) { if !mainMods.Contains(m.Path) {
base.Fatalf("cannot use -fuzz flag on package outside the main module") base.Fatalf("cannot use -fuzz flag on package outside the main module")
} }
} else if pkgs[0].Standard && modload.Enabled(modload.LoaderState) { } else if pkgs[0].Standard && modload.Enabled(moduleLoaderState) {
// Because packages in 'std' and 'cmd' are part of the standard library, // Because packages in 'std' and 'cmd' are part of the standard library,
// they are only treated as part of a module in 'go mod' subcommands and // they are only treated as part of a module in 'go mod' subcommands and
// 'go get'. However, we still don't want to accidentally corrupt their // 'go get'. However, we still don't want to accidentally corrupt their
@ -854,7 +855,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
} }
} }
b := work.NewBuilder("", modload.LoaderState.VendorDirOrEmpty) b := work.NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
defer func() { defer func() {
if err := b.Close(); err != nil { if err := b.Close(); err != nil {
base.Fatal(err) base.Fatal(err)
@ -872,8 +873,8 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
// Select for coverage all dependencies matching the -coverpkg // Select for coverage all dependencies matching the -coverpkg
// patterns. // patterns.
plist := load.TestPackageList(modload.LoaderState, ctx, pkgOpts, pkgs) plist := load.TestPackageList(moduleLoaderState, ctx, pkgOpts, pkgs)
testCoverPkgs = load.SelectCoverPackages(modload.LoaderState, plist, match, "test") testCoverPkgs = load.SelectCoverPackages(moduleLoaderState, plist, match, "test")
if len(testCoverPkgs) > 0 { if len(testCoverPkgs) > 0 {
// create a new singleton action that will collect up the // create a new singleton action that will collect up the
// meta-data files from all of the packages mentioned in // meta-data files from all of the packages mentioned in
@ -951,7 +952,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
"testing": true, "testing": true,
"time": true, "time": true,
} }
for _, p := range load.TestPackageList(modload.LoaderState, ctx, pkgOpts, pkgs) { for _, p := range load.TestPackageList(moduleLoaderState, ctx, pkgOpts, pkgs) {
if !skipInstrumentation[p.ImportPath] { if !skipInstrumentation[p.ImportPath] {
p.Internal.FuzzInstrument = true p.Internal.FuzzInstrument = true
} }
@ -981,7 +982,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
// happens we'll wind up building the Q compile action // happens we'll wind up building the Q compile action
// before updating its deps to include sync/atomic). // before updating its deps to include sync/atomic).
if cfg.BuildCoverMode == "atomic" && p.ImportPath != "sync/atomic" { if cfg.BuildCoverMode == "atomic" && p.ImportPath != "sync/atomic" {
load.EnsureImport(modload.LoaderState, p, "sync/atomic") load.EnsureImport(moduleLoaderState, p, "sync/atomic")
} }
// Tag the package for static meta-data generation if no // Tag the package for static meta-data generation if no
// test files (this works only with the new coverage // test files (this works only with the new coverage
@ -1048,7 +1049,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
reportSetupFailed(firstErrPkg, firstErrPkg.Error) reportSetupFailed(firstErrPkg, firstErrPkg.Error)
continue continue
} }
buildTest, runTest, printTest, perr, err := builderTest(modload.LoaderState, b, ctx, pkgOpts, p, allImports[p], writeCoverMetaAct) buildTest, runTest, printTest, perr, err := builderTest(moduleLoaderState, b, ctx, pkgOpts, p, allImports[p], writeCoverMetaAct)
if err != nil { if err != nil {
reportErr(perr, err) reportErr(perr, err)
reportSetupFailed(perr, err) reportSetupFailed(perr, err)