diff --git a/src/cmd/go/internal/load/test.go b/src/cmd/go/internal/load/test.go index f9bdd5e1fc..bb1bedd0d8 100644 --- a/src/cmd/go/internal/load/test.go +++ b/src/cmd/go/internal/load/test.go @@ -294,15 +294,6 @@ func TestPackagesAndErrors(loaderstate *modload.State, ctx context.Context, done pb := p.Internal.Build pmain.DefaultGODEBUG = defaultGODEBUG(loaderstate, pmain, pb.Directives, pb.TestDirectives, pb.XTestDirectives) - if !opts.SuppressBuildInfo && (pmain.Internal.BuildInfo == nil || pmain.DefaultGODEBUG != p.DefaultGODEBUG) { - // Either we didn't generate build info for the package under test (because it wasn't package main), or - // the DefaultGODEBUG used to build the test main package is different from the DefaultGODEBUG - // used to build the package under test. If we didn't set build info for the package under test - // pmain won't have buildinfo set (since we copy it from the package under test). If the default GODEBUG - // used for the package under test is different from that of the test main, the BuildInfo assigned above from the package - // under test incorrect for the test main package. Either set or correct pmain's build info. - pmain.setBuildInfo(ctx, loaderstate.Fetcher(), opts.AutoVCS) - } // The generated main also imports testing, regexp, and os. // Also the linker introduces implicit dependencies reported by LinkerDeps. @@ -379,6 +370,15 @@ func TestPackagesAndErrors(loaderstate *modload.State, ctx context.Context, done ptest.Incomplete = true } + if !opts.SuppressBuildInfo { + // Now that pmain.Internal.Imports includes the test dependencies, + // regenerate build info for the test binary. We can't reuse p's + // build info because the test variants of packages can add + // packages from modules that don't already have transitive + // imports from p. + pmain.setBuildInfo(ctx, loaderstate.Fetcher(), opts.AutoVCS) + } + if cover != nil { // Here ptest needs to inherit the proper coverage mode (since // it contains p's Go files), whereas pmain contains only diff --git a/src/cmd/go/testdata/script/test_buildinfo.txt b/src/cmd/go/testdata/script/test_buildinfo.txt index fbf097c0a6..2edc02dc13 100644 --- a/src/cmd/go/testdata/script/test_buildinfo.txt +++ b/src/cmd/go/testdata/script/test_buildinfo.txt @@ -4,15 +4,20 @@ [short] skip 'invokes go test' go mod init foo +go get example.com/version@v1.0.0 + go test -v stdout '(devel)' +stdout 'example.com/version v1.0.0' -- foo_test.go -- -package foo_test +package foo import ( "runtime/debug" "testing" + + _ "example.com/version" ) func TestBuildInfo(t *testing.T) { @@ -21,4 +26,8 @@ func TestBuildInfo(t *testing.T) { t.Fatal("no debug info") } t.Log(info.Main.Version) -} \ No newline at end of file + + for _, d := range info.Deps { + t.Log(d.Path, d.Version) + } +}