...
1# Regression test for https://go.dev/issue/51748: by default, 'go build' should
2# not attempt to stamp VCS information when the VCS tool is not present.
3
4[short] skip
5[!git] skip
6
7cd sub
8exec git init .
9exec git config user.name 'Nameless Gopher'
10exec git config user.email 'nobody@golang.org'
11exec git add sub.go
12exec git commit -m 'initial state'
13cd ..
14
15exec git init
16exec git config user.name 'Nameless Gopher'
17exec git config user.email 'nobody@golang.org'
18exec git submodule add ./sub
19exec git add go.mod example.go
20exec git commit -m 'initial state'
21
22
23# Control case: with a git binary in $PATH,
24# 'go build' on a package in the same git repo
25# succeeds and stamps VCS metadata by default.
26
27go build -o example.exe .
28go version -m example.exe
29stdout '^\tbuild\tvcs=git$'
30stdout '^\tbuild\tvcs.modified=false$'
31
32
33# Building a binary from a different (nested) VCS repo should not stamp VCS
34# info. It should be an error if VCS stamps are requested explicitly with
35# '-buildvcs' (since we know the VCS metadata exists), but not an error
36# with '-buildvcs=auto'.
37
38go build -o sub.exe ./sub
39go version -m sub.exe
40! stdout '^\tbuild\tvcs'
41
42! go build -buildvcs -o sub.exe ./sub
43stderr '\Aerror obtaining VCS status: main package is in repository ".*" but current directory is in repository ".*"\n\tUse -buildvcs=false to disable VCS stamping.\n\z'
44
45cd ./sub
46go build -o sub.exe .
47go version -m sub.exe
48! stdout '^\tbuild\tvcs'
49
50! go build -buildvcs -o sub.exe .
51stderr '\Aerror obtaining VCS status: main module is in repository ".*" but current directory is in repository ".*"\n\tUse -buildvcs=false to disable VCS stamping.\n\z'
52cd ..
53
54
55# After removing 'git' from $PATH, 'go build -buildvcs' should fail...
56
57env PATH=
58env path=
59! go build -buildvcs -o example.exe .
60stderr 'go: missing Git command\. See https://golang\.org/s/gogetcmd$'
61
62# ...but by default we should omit VCS metadata when the tool is missing.
63
64go build -o example.exe .
65go version -m example.exe
66! stdout '^\tbuild\tvcs'
67
68# The default behavior can be explicitly set with '-buildvcs=auto'.
69
70go build -buildvcs=auto -o example.exe .
71go version -m example.exe
72! stdout '^\tbuild\tvcs'
73
74# Other flag values should be rejected with a useful error message.
75
76! go build -buildvcs=hg -o example.exe .
77stderr '\Ainvalid boolean value "hg" for -buildvcs: value is neither ''auto'' nor a valid bool\nusage: go build .*\nRun ''go help build'' for details.\n\z'
78
79
80-- go.mod --
81module example
82
83go 1.18
84-- example.go --
85package main
86
87func main() {}
88-- sub/sub.go --
89package main
90
91func main() {}
View as plain text