pkgOpts := load.PackageOpts{
IgnoreImports: *listFind,
ModResolveTests: *listTest,
- LoadVCS: true,
+ AutoVCS: true,
// SuppressDeps is set if the user opts to explicitly ask for the json fields they
// need, don't ask for Deps or DepsErrors. It's not set when using a template string,
// even if *listFmt doesn't contain .Deps because Deps are used to find import cycles
// Consider starting this as a background goroutine and retrieving the result
// asynchronously when we're actually ready to build the package, or when we
// actually need to evaluate whether the package's metadata is stale.
- p.setBuildInfo(opts.LoadVCS)
+ p.setBuildInfo(opts.AutoVCS)
}
// unsafe is a fake package.
//
// Note that the GoVersion field is not set here to avoid encoding it twice.
// It is stored separately in the binary, mostly for historical reasons.
-func (p *Package) setBuildInfo(includeVCS bool) {
+func (p *Package) setBuildInfo(autoVCS bool) {
setPkgErrorf := func(format string, args ...any) {
if p.Error == nil {
p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
var vcsCmd *vcs.Cmd
var err error
const allowNesting = true
- if includeVCS && cfg.BuildBuildvcs != "false" && p.Module != nil && p.Module.Version == "" && !p.Standard && !p.IsTestOnly() {
+
+ wantVCS := false
+ switch cfg.BuildBuildvcs {
+ case "true":
+ wantVCS = true // Include VCS metadata even for tests if requested explicitly; see https://go.dev/issue/52648.
+ case "auto":
+ wantVCS = autoVCS && !p.IsTestOnly()
+ case "false":
+ default:
+ panic(fmt.Sprintf("unexpected value for cfg.BuildBuildvcs: %q", cfg.BuildBuildvcs))
+ }
+
+ if wantVCS && p.Module != nil && p.Module.Version == "" && !p.Standard {
repoDir, vcsCmd, err = vcs.FromDir(base.Cwd(), "", allowNesting)
if err != nil && !errors.Is(err, os.ErrNotExist) {
setVCSError(err)
// may be printed for non-literal arguments that match no main packages.
MainOnly bool
- // LoadVCS controls whether we also load version-control metadata for main packages.
- LoadVCS bool
+ // AutoVCS controls whether we also load version-control metadata for main packages
+ // when -buildvcs=auto (the default).
+ AutoVCS bool
// SuppressDeps is true if the caller does not need Deps and DepsErrors to be populated
// on the package. TestPackagesAndErrors examines the Deps field to determine if the test
var b Builder
b.Init()
- pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{LoadVCS: true}, args)
+ pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{AutoVCS: true}, args)
load.CheckPackageErrors(pkgs)
explicitO := len(cfg.BuildO) > 0
modload.InitWorkfile()
BuildInit()
- pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{LoadVCS: true}, args)
+ pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{AutoVCS: true}, args)
if cfg.ModulesEnabled && !modload.HasModRoot() {
haveErrors := false
allMissingErrors := true
[short] skip
[!exec:git] skip
-env GOFLAGS=-buildvcs # override default -buildvcs=auto in GOFLAGS, as a user might
-
exec git init
-# The test binaries should not have VCS settings stamped.
+# The test binaries should not have VCS settings stamped by default.
# (The test itself verifies that.)
go test . ./testonly
+# However, setting -buildvcs explicitly should override that and
+# stamp anyway (https://go.dev/issue/52648).
+go test -buildvcs -c -o ./testonly.exe ./testonly
+! exec ./testonly.exe
+stdout 'unexpected VCS setting: vcs\.modified=true'
+
# Remove 'git' from $PATH. The test should still build.
# This ensures that we aren't loading VCS metadata that
# When listing a main package, in general we need its VCS metadata to determine
# the .Stale and .StaleReason fields.
-! go list .
+! go list -buildvcs=true .
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 .
+! go list -buildvcs=true -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.
! 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.
+# Since the ./testonly package doesn't itself produce an actual binary, we shouldn't
+# invoke a VCS tool to compute a build stamp by default when listing it.
go list ./testonly
! stderr .
go list -test ./testonly
! stderr .
+# Again, setting -buildvcs explicitly should force the use of the VCS tool.
+! go list -buildvcs ./testonly
+stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
+! go list -buildvcs -test ./testonly
+stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
+
-- go.mod --
module example