-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go: avoid stamping VCS metadata in test binaries
Invoking a VCS tool requires that the VCS tool be installed, and also adds latency to build commands. Unfortunately, we had been mistakenly loading VCS metadata for tests of "main" packages. Users almost never care about versioning for test binaries, because 'go test' runs the test in the source tree and test binaries are only rarely used outside of 'go test'. So the user already knows exactly which version the test is built against, because the source code is right there — it's not worth the overhead to stamp. Fixes #51723. Change-Id: I96f191c5a765f5183e5e10b6dfb75a0381c99814 Reviewed-on: https://go-review.googlesource.com/c/go/+/393894 Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Trust: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gopher Robot <gobot@golang.org>
- Loading branch information
Bryan C. Mills
committed
Mar 18, 2022
1 parent
9465878
commit 67f6b8c
Showing
5 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# https://go.dev/issue/51723: 'go test' should not stamp VCS metadata | ||
# in the build settings. (It isn't worth the latency hit, given that | ||
# test binaries are almost never distributed to users.) | ||
|
||
[short] skip | ||
[!exec:git] skip | ||
|
||
exec git init | ||
|
||
# The test binaries should not have VCS settings stamped. | ||
# (The test itself verifies that.) | ||
go test . ./testonly | ||
|
||
|
||
# Remove 'git' from $PATH. The test should still build. | ||
# This ensures that we aren't loading VCS metadata that | ||
# we subsequently throw away. | ||
env PATH='' | ||
env path='' | ||
|
||
# Compiling the test should not require the VCS tool. | ||
go test -c -o $devnull . | ||
|
||
|
||
# When listing a main package, in general we need its VCS metadata to determine | ||
# the .Stale and .StaleReason fields. | ||
! go list . | ||
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.' | ||
|
||
# Adding the -test flag should be strictly additive — it should not suppress the error. | ||
! go list -test . | ||
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.' | ||
|
||
# Adding the suggested flag should suppress the error. | ||
go list -test -buildvcs=false . | ||
! stderr . | ||
|
||
|
||
# Since the ./testonly package can't produce an actual binary, we shouldn't | ||
# invoke a VCS tool to compute a build stamp when listing it. | ||
go list ./testonly | ||
! stderr . | ||
go list -test ./testonly | ||
! stderr . | ||
|
||
|
||
-- go.mod -- | ||
module example | ||
|
||
go 1.18 | ||
-- example.go -- | ||
package main | ||
-- example_test.go -- | ||
package main | ||
|
||
import ( | ||
"runtime/debug" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestDetail(t *testing.T) { | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
t.Fatal("BuildInfo not present") | ||
} | ||
for _, s := range bi.Settings { | ||
if strings.HasPrefix(s.Key, "vcs.") { | ||
t.Fatalf("unexpected VCS setting: %s=%s", s.Key, s.Value) | ||
} | ||
} | ||
} | ||
-- testonly/main_test.go -- | ||
package main | ||
|
||
import ( | ||
"runtime/debug" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestDetail(t *testing.T) { | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
t.Fatal("BuildInfo not present") | ||
} | ||
for _, s := range bi.Settings { | ||
if strings.HasPrefix(s.Key, "vcs.") { | ||
t.Fatalf("unexpected VCS setting: %s=%s", s.Key, s.Value) | ||
} | ||
} | ||
} |