Skip to content

Commit 1f72ce3

Browse files
committed
cmd/go/internal/load: recompute test variant's build info if necessary
The buildinfo used for a testmain is a copy from the buildinfo produced for the package under test, and that in turn is only computed if the package under test is package main. If there are //go:debug directives in a test file for package main, the godebugs for the testmain (which are computed using the regular package files as well as the test files' //go:debug directives) will be different from those used to produce the buildinfo of the package under test (computed using the //go:debug directives only in the main package). In that case, recompute the buildinfo for the testmain to incorporate the new godebug information. Since we've only been generating buildinfo for tests on package main, in this CL we'll only recompute the buildinfo if the test is for package main. It's not clear to me though if we should be computing the buildinfo for all test mains (or none of them?) Fixes #68053 Change-Id: Ib6cdb118e2f233de483c33e171c0cd03df1fc7be Reviewed-on: https://go-review.googlesource.com/c/go/+/595961 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
1 parent c7227bc commit 1f72ce3

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ 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.
304+
pmain.setBuildInfo(ctx, opts.AutoVCS)
305+
}
296306

297307
// The generated main also imports testing, regexp, and os.
298308
// Also the linker introduces implicit dependencies reported by LinkerDeps.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[short] skip 'builds test binary'
2+
3+
go list -test -f '{{.ImportPath}} {{.DefaultGODEBUG}}'
4+
stdout 'example.com/foo\.test.*panicnil=1.*'
5+
6+
go test -c
7+
go version -m ./foo.test
8+
stdout 'build\tDefaultGODEBUG=.*panicnil=1.*'
9+
10+
-- go.mod --
11+
module example.com/foo
12+
13+
go 1.23
14+
-- main_test.go --
15+
//go:debug panicnil=1
16+
package main_test
17+
18+
import (
19+
"runtime/debug"
20+
"testing"
21+
)
22+
23+
func TestFoo(t *testing.T) {
24+
defer func() {
25+
t.Fatal(recover())
26+
}()
27+
28+
t.Log(debug.ReadBuildInfo())
29+
panic(nil)
30+
}

0 commit comments

Comments
 (0)