cmd/go: include test deps in buildinfo

Fixes #76926

Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19
Reviewed-on: https://go-review.googlesource.com/c/go/+/756240
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Stefan VanBuren
2026-03-17 20:09:35 -04:00
committed by Michael Matloob
parent 79a8ccab29
commit a45c8032bb
2 changed files with 20 additions and 11 deletions

View File

@@ -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

View File

@@ -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)
}
for _, d := range info.Deps {
t.Log(d.Path, d.Version)
}
}