Skip to content

Commit 805305e

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: stamp VCS information in test binaries when -buildvcs=true
(But still not when -buildvcs=auto, the default.) Fixes #52648. Change-Id: I87a87d4ea84e8bf9635a4f7c8674c9311c3e21be Reviewed-on: https://go-review.googlesource.com/c/go/+/409177 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent a45bbea commit 805305e

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

src/cmd/go/internal/list/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
592592
pkgOpts := load.PackageOpts{
593593
IgnoreImports: *listFind,
594594
ModResolveTests: *listTest,
595-
LoadVCS: true,
595+
AutoVCS: true,
596596
// SuppressDeps is set if the user opts to explicitly ask for the json fields they
597597
// need, don't ask for Deps or DepsErrors. It's not set when using a template string,
598598
// even if *listFmt doesn't contain .Deps because Deps are used to find import cycles

src/cmd/go/internal/load/pkg.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
19741974
// Consider starting this as a background goroutine and retrieving the result
19751975
// asynchronously when we're actually ready to build the package, or when we
19761976
// actually need to evaluate whether the package's metadata is stale.
1977-
p.setBuildInfo(opts.LoadVCS)
1977+
p.setBuildInfo(opts.AutoVCS)
19781978
}
19791979

19801980
// unsafe is a fake package.
@@ -2264,7 +2264,7 @@ var vcsStatusCache par.Cache
22642264
//
22652265
// Note that the GoVersion field is not set here to avoid encoding it twice.
22662266
// It is stored separately in the binary, mostly for historical reasons.
2267-
func (p *Package) setBuildInfo(includeVCS bool) {
2267+
func (p *Package) setBuildInfo(autoVCS bool) {
22682268
setPkgErrorf := func(format string, args ...any) {
22692269
if p.Error == nil {
22702270
p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
@@ -2420,7 +2420,19 @@ func (p *Package) setBuildInfo(includeVCS bool) {
24202420
var vcsCmd *vcs.Cmd
24212421
var err error
24222422
const allowNesting = true
2423-
if includeVCS && cfg.BuildBuildvcs != "false" && p.Module != nil && p.Module.Version == "" && !p.Standard && !p.IsTestOnly() {
2423+
2424+
wantVCS := false
2425+
switch cfg.BuildBuildvcs {
2426+
case "true":
2427+
wantVCS = true // Include VCS metadata even for tests if requested explicitly; see https://go.dev/issue/52648.
2428+
case "auto":
2429+
wantVCS = autoVCS && !p.IsTestOnly()
2430+
case "false":
2431+
default:
2432+
panic(fmt.Sprintf("unexpected value for cfg.BuildBuildvcs: %q", cfg.BuildBuildvcs))
2433+
}
2434+
2435+
if wantVCS && p.Module != nil && p.Module.Version == "" && !p.Standard {
24242436
repoDir, vcsCmd, err = vcs.FromDir(base.Cwd(), "", allowNesting)
24252437
if err != nil && !errors.Is(err, os.ErrNotExist) {
24262438
setVCSError(err)
@@ -2724,8 +2736,9 @@ type PackageOpts struct {
27242736
// may be printed for non-literal arguments that match no main packages.
27252737
MainOnly bool
27262738

2727-
// LoadVCS controls whether we also load version-control metadata for main packages.
2728-
LoadVCS bool
2739+
// AutoVCS controls whether we also load version-control metadata for main packages
2740+
// when -buildvcs=auto (the default).
2741+
AutoVCS bool
27292742

27302743
// SuppressDeps is true if the caller does not need Deps and DepsErrors to be populated
27312744
// on the package. TestPackagesAndErrors examines the Deps field to determine if the test

src/cmd/go/internal/work/build.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
406406
var b Builder
407407
b.Init()
408408

409-
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{LoadVCS: true}, args)
409+
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{AutoVCS: true}, args)
410410
load.CheckPackageErrors(pkgs)
411411

412412
explicitO := len(cfg.BuildO) > 0
@@ -636,7 +636,7 @@ func runInstall(ctx context.Context, cmd *base.Command, args []string) {
636636

637637
modload.InitWorkfile()
638638
BuildInit()
639-
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{LoadVCS: true}, args)
639+
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{AutoVCS: true}, args)
640640
if cfg.ModulesEnabled && !modload.HasModRoot() {
641641
haveErrors := false
642642
allMissingErrors := true

src/cmd/go/testdata/script/test_buildvcs.txt

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
[short] skip
66
[!exec:git] skip
77

8-
env GOFLAGS=-buildvcs # override default -buildvcs=auto in GOFLAGS, as a user might
9-
108
exec git init
119

12-
# The test binaries should not have VCS settings stamped.
10+
# The test binaries should not have VCS settings stamped by default.
1311
# (The test itself verifies that.)
1412
go test . ./testonly
1513

14+
# However, setting -buildvcs explicitly should override that and
15+
# stamp anyway (https://go.dev/issue/52648).
16+
go test -buildvcs -c -o ./testonly.exe ./testonly
17+
! exec ./testonly.exe
18+
stdout 'unexpected VCS setting: vcs\.modified=true'
19+
1620

1721
# Remove 'git' from $PATH. The test should still build.
1822
# This ensures that we aren't loading VCS metadata that
@@ -26,25 +30,31 @@ go test -c -o $devnull .
2630

2731
# When listing a main package, in general we need its VCS metadata to determine
2832
# the .Stale and .StaleReason fields.
29-
! go list .
33+
! go list -buildvcs=true .
3034
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
3135

3236
# Adding the -test flag should be strictly additive — it should not suppress the error.
33-
! go list -test .
37+
! go list -buildvcs=true -test .
3438
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
3539

3640
# Adding the suggested flag should suppress the error.
3741
go list -test -buildvcs=false .
3842
! stderr .
3943

4044

41-
# Since the ./testonly package can't produce an actual binary, we shouldn't
42-
# invoke a VCS tool to compute a build stamp when listing it.
45+
# Since the ./testonly package doesn't itself produce an actual binary, we shouldn't
46+
# invoke a VCS tool to compute a build stamp by default when listing it.
4347
go list ./testonly
4448
! stderr .
4549
go list -test ./testonly
4650
! stderr .
4751

52+
# Again, setting -buildvcs explicitly should force the use of the VCS tool.
53+
! go list -buildvcs ./testonly
54+
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
55+
! go list -buildvcs -test ./testonly
56+
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
57+
4858

4959
-- go.mod --
5060
module example

0 commit comments

Comments
 (0)