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.
+ if 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, opts.AutoVCS)
}
fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
}
- // The default godebug is embedded in the binary. For main packages it's
- // already taken into account for the action id through the build info. But
- // to make sure it's included for tests of other packages, where there's no
- // build info, use it as part of the action id. See issue #69203.
- if p != nil {
- fmt.Fprintf(h, "default GODEBUG %q\n", p.DefaultGODEBUG)
- }
-
// The linker writes source file paths that refer to GOROOT,
// but only if -trimpath is not specified (see [gctoolchain.ld] in gc.go).
gorootFinal := cfg.GOROOT
--- /dev/null
+# Ensure buildinfo is populated on test binaries even if they
+# are not tests for package main. See issue #33976.
+
+[short] skip 'invokes go test'
+
+go mod init foo
+go test -v
+stdout '(devel)'
+
+-- foo_test.go --
+package foo_test
+
+import (
+ "runtime/debug"
+ "testing"
+)
+
+func TestBuildInfo(t *testing.T) {
+ info, ok := debug.ReadBuildInfo()
+ if !ok {
+ t.Fatal("no debug info")
+ }
+ t.Log(info.Main.Version)
+}
\ No newline at end of file