[go] cmd/go: include test deps in buildinfo

2 views
Skip to first unread message

Stefan VanBuren (Gerrit)

unread,
Mar 17, 2026, 8:37:42 PM (24 hours ago) Mar 17
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Stefan VanBuren has uploaded the change for review

Commit message

cmd/go: include test deps in buildinfo

Fixes #76926
Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19

Change diff

diff --git a/src/cmd/go/internal/load/test.go b/src/cmd/go/internal/load/test.go
index f9bdd5e1..d1ec05f 100644
--- a/src/cmd/go/internal/load/test.go
+++ b/src/cmd/go/internal/load/test.go
@@ -294,15 +294,6 @@

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,12 @@
ptest.Incomplete = true
}

+ if !opts.SuppressBuildInfo {
+ // Now that pmain.Internal.Imports includes the test dependencies,
+ // regenerate build info for the test binary.
+ 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
diff --git a/src/cmd/go/testdata/script/test_buildinfo.txt b/src/cmd/go/testdata/script/test_buildinfo.txt
index fbf097c..e8884e2 100644
--- a/src/cmd/go/testdata/script/test_buildinfo.txt
+++ b/src/cmd/go/testdata/script/test_buildinfo.txt
@@ -3,7 +3,10 @@

[short] skip 'invokes go test'

-go mod init foo
+go mod init example.com/foo
+go mod edit -require=example.com/d...@v0.0.0
+go mod edit -replace=example.com/dep=./dep
+
go test -v
stdout '(devel)'

@@ -13,12 +16,39 @@
import (
"runtime/debug"
"testing"
+
+ "example.com/dep"
)

func TestBuildInfo(t *testing.T) {
+ if dep.Version() == "" {
+ t.Fatal("empty dep version")
+ }
+
info, ok := debug.ReadBuildInfo()
if !ok {
t.Fatal("no debug info")
}
t.Log(info.Main.Version)
-}
\ No newline at end of file
+
+ found := false
+ for _, d := range info.Deps {
+ if d.Path == "example.com/dep" {
+ found = true
+ break
+ }
+ }
+ if !found {
+ t.Fatalf("expected dep example.com/dep in build info, got %v", info.Deps)
+ }
+}
+
+-- dep/go.mod --
+module example.com/dep
+
+-- dep/dep.go --
+package dep
+
+func Version() string {
+ return "v0"
+}

Change information

Files:
  • M src/cmd/go/internal/load/test.go
  • M src/cmd/go/testdata/script/test_buildinfo.txt
Change size: S
Delta: 2 files changed, 38 insertions(+), 11 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19
Gerrit-Change-Number: 756240
Gerrit-PatchSet: 1
Gerrit-Owner: Stefan VanBuren <vanbure...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Mar 17, 2026, 8:42:06 PM (24 hours ago) Mar 17
to Stefan VanBuren, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Message from Gopher Robot

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19
Gerrit-Change-Number: 756240
Gerrit-PatchSet: 1
Gerrit-Owner: Stefan VanBuren <vanbure...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Comment-Date: Wed, 18 Mar 2026 00:42:01 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Matloob (Gerrit)

unread,
11:30 AM (9 hours ago) 11:30 AM
to Stefan VanBuren, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Stefan VanBuren

Michael Matloob voted and added 2 comments

Votes added by Michael Matloob

Code-Review+2

2 comments

File src/cmd/go/internal/load/test.go
Line 373, Patchset 1 (Latest): if !opts.SuppressBuildInfo {

// Now that pmain.Internal.Imports includes the test dependencies,
// regenerate build info for the test binary.
pmain.setBuildInfo(ctx, loaderstate.Fetcher(), opts.AutoVCS)
}
Michael Matloob . unresolved

We should also say here that we can't reuse p's build info in general because the the test variants of the packages can add packages from modules that don't already have transitive imports from `p`.

File src/cmd/go/testdata/script/test_buildinfo.txt
Line 1, Patchset 1 (Latest):# 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 example.com/foo
go mod edit -require=example.com/d...@v0.0.0
go mod edit -replace=example.com/dep=./dep


go test -v
stdout '(devel)'

-- foo_test.go --
package foo_test

import (
"runtime/debug"
"testing"


"example.com/dep"
)

func TestBuildInfo(t *testing.T) {
if dep.Version() == "" {

t.Fatal("empty dep version")
}

info, ok := debug.ReadBuildInfo()
if !ok {
t.Fatal("no debug info")
}
t.Log(info.Main.Version)

found := false

for _, d := range info.Deps {
if d.Path == "example.com/dep" {
found = true
break
}
}
if !found {

t.Fatalf("expected dep example.com/dep in build info, got %v", info.Deps)
}
}

-- dep/go.mod --
module example.com/dep

-- dep/dep.go --
package dep

func Version() string {
return "v0"
}
Michael Matloob . unresolved

It's a little easier to parse test failures when they are caused by assertions in the script test. Also we can use the local fake module proxy we start for the go command tests. How's this?
```suggestion
# 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 get example.com/ver...@v1.0.0

go test -v
stdout '(devel)'

stdout 'example.com/version v1.0.0'

-- foo_test.go --
package foo

import (
"runtime/debug"
"testing"
        _ "example.com/version"
)
func TestBuildInfo(t *testing.T) {

info, ok := debug.ReadBuildInfo()
if !ok {
t.Fatal("no debug info")
}
t.Log(info.Main.Version)
        for _, d := range info.Deps {
t.Log(d.Path, d.Version)
}
}
```
Open in Gerrit

Related details

Attention is currently required from:
  • Stefan VanBuren
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19
Gerrit-Change-Number: 756240
Gerrit-PatchSet: 1
Gerrit-Owner: Stefan VanBuren <vanbure...@gmail.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Stefan VanBuren <vanbure...@gmail.com>
Gerrit-Comment-Date: Wed, 18 Mar 2026 15:30:21 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Michael Matloob (Gerrit)

unread,
11:30 AM (9 hours ago) 11:30 AM
to Stefan VanBuren, goph...@pubsubhelper.golang.org, Michael Matloob, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Stefan VanBuren

Michael Matloob voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Stefan VanBuren
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19
Gerrit-Change-Number: 756240
Gerrit-PatchSet: 1
Gerrit-Owner: Stefan VanBuren <vanbure...@gmail.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Reviewer: Michael Matloob <mat...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Stefan VanBuren <vanbure...@gmail.com>
Gerrit-Comment-Date: Wed, 18 Mar 2026 15:30:34 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Stefan VanBuren (Gerrit)

unread,
2:55 PM (5 hours ago) 2:55 PM
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Michael Matloob, Michael Matloob and Stefan VanBuren

Stefan VanBuren uploaded new patchset

Stefan VanBuren uploaded patch set #2 to this change.
Following approvals got outdated and were removed:
  • Code-Review: +1 by Michael Matloob, +2 by Michael Matloob
Open in Gerrit

Related details

Attention is currently required from:
  • Michael Matloob
  • Michael Matloob
  • Stefan VanBuren
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I822dd6363dea1c4ad73df5958964c1bfe2c46d19
Gerrit-Change-Number: 756240
Gerrit-PatchSet: 2
Gerrit-Owner: Stefan VanBuren <vanbure...@gmail.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Reviewer: Michael Matloob <mat...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Attention: Michael Matloob <mat...@google.com>
Gerrit-Attention: Stefan VanBuren <vanbure...@gmail.com>
unsatisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages