-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Open
Description
Background
I yearn for the day debug.BuildInfo.Main.Version is all a binary needs to know its own version.
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
panic("can't retrieve version: this binary must be built with Go modules")
}
fmt.Println(buildInfo.Main.Version)
There are four common scenarios for building Go binaries:
go install example.com/cli@latestgit clone example.com/cli; cd cli; go build- Package managers, which know the version they are building
- Download source tarball and
go build
(1) always worked with debug.BuildInfo.Main.Version.
(2) has worked since #50603.
This proposal would address (3).
I can't think of a way to address (4) without taking the version from a file, which feels error prone. I welcome ideas/opinions.
Proposal
Add the -buildversion build flag, to let build processes that know the version provide it to cmd/go, in a similar way to how the version is stamped from VCS info with -buildvcs.
-buildversion version
The version of the main module being built. This overrides the version
derived from -buildvcs information. It is an error to use this flag with
"go install" with a version suffix. version must be a valid semver string
with a "v" prefix.
(While implementing this, we should also document debug.BuildInfo.Main.Version sources, I can't find anything in the docs about #50603.)
Defelo, mweinelt, jaloren, jakebailey, simonhammes and 4 more