From 1f72ce31f06fbb28c289f5ee7e33d6c54c6894bc Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Mon, 1 Jul 2024 16:34:08 -0400 Subject: [PATCH] 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 Reviewed-by: Sam Thanawalla --- src/cmd/go/internal/load/test.go | 10 +++++++ .../test_buildinfo_godebug_issue68053.txt | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/cmd/go/testdata/script/test_buildinfo_godebug_issue68053.txt diff --git a/src/cmd/go/internal/load/test.go b/src/cmd/go/internal/load/test.go index d29f64a51c..4e85c17053 100644 --- a/src/cmd/go/internal/load/test.go +++ b/src/cmd/go/internal/load/test.go @@ -293,6 +293,16 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p pb := p.Internal.Build pmain.DefaultGODEBUG = defaultGODEBUG(pmain, pb.Directives, pb.TestDirectives, pb.XTestDirectives) + if pmain.Internal.BuildInfo != nil && pmain.DefaultGODEBUG != p.DefaultGODEBUG { + // The DefaultGODEBUG used to build the test main package is different from the DefaultGODEBUG + // used to build the package under test. That makes the BuildInfo assigned above from the package + // under test incorrect for the test main package. Recompute the build info for the test main + // package to incorporate the test main's DefaultGODEBUG value. + // Most test binaries do not have build info: p.Internal.BuildInfo is only computed for main + // packages, so ptest only inherits a non-nil BuildInfo value if the test is for package main. + // See issue #68053. + pmain.setBuildInfo(ctx, opts.AutoVCS) + } // The generated main also imports testing, regexp, and os. // Also the linker introduces implicit dependencies reported by LinkerDeps. diff --git a/src/cmd/go/testdata/script/test_buildinfo_godebug_issue68053.txt b/src/cmd/go/testdata/script/test_buildinfo_godebug_issue68053.txt new file mode 100644 index 0000000000..1257f7352a --- /dev/null +++ b/src/cmd/go/testdata/script/test_buildinfo_godebug_issue68053.txt @@ -0,0 +1,30 @@ +[short] skip 'builds test binary' + +go list -test -f '{{.ImportPath}} {{.DefaultGODEBUG}}' +stdout 'example.com/foo\.test.*panicnil=1.*' + +go test -c +go version -m ./foo.test +stdout 'build\tDefaultGODEBUG=.*panicnil=1.*' + +-- go.mod -- +module example.com/foo + +go 1.23 +-- main_test.go -- +//go:debug panicnil=1 +package main_test + +import ( + "runtime/debug" + "testing" +) + +func TestFoo(t *testing.T) { + defer func() { + t.Fatal(recover()) + }() + + t.Log(debug.ReadBuildInfo()) + panic(nil) +} \ No newline at end of file -- 2.48.1