Skip to content

Commit d79e6be

Browse files
matloobgopherbot
authored andcommitted
cmd/go: populate build info for test mains
Before this change, test binaries didn't have build info populated on them unless they were tests for package main. Now we generate them for all test binaries so that they can be inspected like other binaries. We don't need to add the default GODEBUG in printLinkerConfig because it will now always be present on the build info, and when build info is present we use it to generate the hash. Fixes #33976 Change-Id: Ib4f51c04f87df3c7f2f21c400ab446e70d66a101 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/613096 Auto-Submit: Michael Matloob <[email protected]> Reviewed-by: Sam Thanawalla <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2c5b707 commit d79e6be

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/cmd/go/internal/load/test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,13 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p
293293

294294
pb := p.Internal.Build
295295
pmain.DefaultGODEBUG = defaultGODEBUG(pmain, pb.Directives, pb.TestDirectives, pb.XTestDirectives)
296-
if pmain.Internal.BuildInfo != nil && pmain.DefaultGODEBUG != p.DefaultGODEBUG {
297-
// The DefaultGODEBUG used to build the test main package is different from the DefaultGODEBUG
298-
// used to build the package under test. That makes the BuildInfo assigned above from the package
299-
// under test incorrect for the test main package. Recompute the build info for the test main
300-
// package to incorporate the test main's DefaultGODEBUG value.
301-
// Most test binaries do not have build info: p.Internal.BuildInfo is only computed for main
302-
// packages, so ptest only inherits a non-nil BuildInfo value if the test is for package main.
303-
// See issue #68053.
296+
if pmain.Internal.BuildInfo == nil || pmain.DefaultGODEBUG != p.DefaultGODEBUG {
297+
// Either we didn't generate build info for the package under test (because it wasn't package main), or
298+
// the DefaultGODEBUG used to build the test main package is different from the DefaultGODEBUG
299+
// used to build the package under test. If we didn't set build info for the package under test
300+
// pmain won't have buildinfo set (since we copy it from the package under test). If the default GODEBUG
301+
// used for the package under test is different from that of the test main, the BuildInfo assigned above from the package
302+
// under test incorrect for the test main package. Either set or correct pmain's build info.
304303
pmain.setBuildInfo(ctx, opts.AutoVCS)
305304
}
306305

src/cmd/go/internal/work/exec.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,14 +1425,6 @@ func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
14251425
fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
14261426
}
14271427

1428-
// The default godebug is embedded in the binary. For main packages it's
1429-
// already taken into account for the action id through the build info. But
1430-
// to make sure it's included for tests of other packages, where there's no
1431-
// build info, use it as part of the action id. See issue #69203.
1432-
if p != nil {
1433-
fmt.Fprintf(h, "default GODEBUG %q\n", p.DefaultGODEBUG)
1434-
}
1435-
14361428
// The linker writes source file paths that refer to GOROOT,
14371429
// but only if -trimpath is not specified (see [gctoolchain.ld] in gc.go).
14381430
gorootFinal := cfg.GOROOT
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Ensure buildinfo is populated on test binaries even if they
2+
# are not tests for package main. See issue #33976.
3+
4+
[short] skip 'invokes go test'
5+
6+
go mod init foo
7+
go test -v
8+
stdout '(devel)'
9+
10+
-- foo_test.go --
11+
package foo_test
12+
13+
import (
14+
"runtime/debug"
15+
"testing"
16+
)
17+
18+
func TestBuildInfo(t *testing.T) {
19+
info, ok := debug.ReadBuildInfo()
20+
if !ok {
21+
t.Fatal("no debug info")
22+
}
23+
t.Log(info.Main.Version)
24+
}

0 commit comments

Comments
 (0)