fix(cmd): register the --version flag, and report a real version on go install#55
fix(cmd): register the --version flag, and report a real version on go install#55kanywst wants to merge 4 commits into
Conversation
Execute called SetVersionTemplate but never set RootCmd.Version, and cobra
only registers the --version flag when Version is non-empty. So the
template was dead code and both --version and -v failed:
$ y509 --version
Error: unknown flag: --version
The man page, both shell completions, and scripts/brew-test.sh all
advertise --version, so this was the documented interface and it did not
exist. Only the version subcommand worked.
The version variables are populated by -ldflags at release time, but a
`go install` build gets no ldflags and reported itself as "dev" forever.
The toolchain has already stamped the module version and the VCS revision
into the binary, so read them back out of debug.ReadBuildInfo when ldflags
did not set anything.
before: y509 version dev
after: y509 version v0.12.1-0.20260706091141-6f91c879576a
Build: ... (6f91c87) built on 2026-07-06T09:11:41Z
An -ldflags build still wins: the fallback only runs while Version is
still "dev".
|
Warning Review limit reached
Next review available in: 21 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request ensures that Cobra registers the --version flag by setting RootCmd.Version and adds an init function to retrieve version, VCS revision, and build date from debug.ReadBuildInfo() when they are not provided via -ldflags. The reviewer suggested refactoring the init function to evaluate each variable independently, ensuring that GitCommit and BuildDate can still be populated from build info even if Version was overridden.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| func init() { | ||
| if Version != "dev" { | ||
| // -ldflags won; leave everything alone. | ||
| return | ||
| } | ||
|
|
||
| info, ok := debug.ReadBuildInfo() | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| // "(devel)" is what a build from a local working tree reports. | ||
| if info.Main.Version != "" && info.Main.Version != "(devel)" { | ||
| Version = info.Main.Version | ||
| } | ||
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| GitCommit = setting.Value | ||
| case "vcs.time": | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
By returning early when Version != "dev", any build that sets the version via -ldflags but does not set GitCommit or BuildDate will completely skip reading the VCS revision and build time from debug.ReadBuildInfo(). Evaluating each variable independently allows GitCommit and BuildDate to be populated from the build info even if Version was overridden.
func init() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
// "(devel)" is what a build from a local working tree reports.
if Version == "dev" && info.Main.Version != "" && info.Main.Version != "(devel)" {
Version = info.Main.Version
}
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if GitCommit == "unknown" {
GitCommit = setting.Value
}
case "vcs.time":
if BuildDate == "unknown" {
BuildDate = setting.Value
}
}
}
}Returning early when Version was already set meant a build that supplied only Version through -ldflags would skip the VCS revision and build time the toolchain had recorded, and report "unknown" for both. Let each value fall back on its own.
|
Thanks, good catch. Applied. The early return meant a build supplying only Both paths still behave:
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request ensures that Cobra registers the --version flag by setting RootCmd.Version and updates the version package to dynamically extract the version, VCS revision, and build time from Go's build info when ldflags are absent. Feedback suggests preventing empty VCS values from overwriting default "unknown" values and appending a -dirty suffix to the commit hash if there are uncommitted changes.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| if GitCommit == "unknown" { | ||
| GitCommit = setting.Value | ||
| } | ||
| case "vcs.time": | ||
| if BuildDate == "unknown" { | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
If setting.Value is empty, assigning it directly to GitCommit or BuildDate will overwrite the default "unknown" value with an empty string, leading to malformed version output (e.g., y509 version dev ()). Additionally, we can check vcs.modified to append -dirty to the commit hash if there are uncommitted changes in the working directory.
var modified bool
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if GitCommit == "unknown" && setting.Value != "" {
GitCommit = setting.Value
}
case "vcs.time":
if BuildDate == "unknown" && setting.Value != "" {
BuildDate = setting.Value
}
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
if modified && GitCommit != "unknown" {
GitCommit += "-dirty"
}If the build info carried a vcs.revision or vcs.time setting with an empty value, it replaced the "unknown" default with "", which renders as "y509 version dev ()". Only take a non-empty value.
|
Thanks. Took the empty-value guard; skipped the `-dirty` suffix. Applied — an empty `vcs.revision`/`vcs.time` was overwriting the "unknown" default with "", which renders as `y509 version dev ()`. Now only a non-empty value is taken. Skipped — appending `-dirty` from `vcs.modified`. `GetFullVersion` truncates the commit to 7 characters for display (`3fd855be...-dirty` → `3fd855b`), so the suffix never actually appears. It would be dead code that looks like it does something. The module version already carries `+dirty` for dirty trees, so the information is not lost either way. `make lint` clean, full suite green. /gemini review |
There was a problem hiding this comment.
Code Review
This pull request ensures that Cobra registers the --version flag by setting RootCmd.Version in internal/cmd/root.go. It also adds an init function in internal/version/version.go to dynamically read build information (version, VCS revision, and VCS time) using runtime/debug.ReadBuildInfo() when ldflags are not provided. A review comment suggests checking the vcs.modified setting and appending -dirty to the GitCommit if the working tree was dirty during the build.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| // An empty value must not overwrite "unknown" -- it would render as | ||
| // "y509 version dev ()". | ||
| if GitCommit == "unknown" && setting.Value != "" { | ||
| GitCommit = setting.Value | ||
| } | ||
| case "vcs.time": | ||
| if BuildDate == "unknown" && setting.Value != "" { | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
When building from a dirty working tree, vcs.modified is set to "true". If we don't check this, the reported GitCommit will look like a clean commit, which can be misleading during debugging. We should check vcs.modified and append -dirty to GitCommit if it is true.
var modified bool
var gitCommitFromVCS bool
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
// An empty value must not overwrite "unknown" -- it would render as
// "y509 version dev ()".
if GitCommit == "unknown" && setting.Value != "" {
GitCommit = setting.Value
gitCommitFromVCS = true
}
case "vcs.time":
if BuildDate == "unknown" && setting.Value != "" {
BuildDate = setting.Value
}
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
if modified && gitCommitFromVCS && !strings.HasSuffix(GitCommit, "-dirty") {
GitCommit += "-dirty"
}|
On the `-dirty` suffix — still skipping, and here is the concrete reason it would be dead code. `GetFullVersion` truncates `GitCommit` to 7 characters for display: ```go So `"3fd855be...-dirty"` renders as `(3fd855b)` — the suffix never appears. And a dirty build is already flagged: `debug.ReadBuildInfo`'s `Main.Version` carries `+dirty`, which shows in the primary version string (`y509 version v0.12.1-...+dirty`). Adding it to the truncated commit hash would be both invisible and redundant. The empty-value guard from the last round is in. /gemini review |
There was a problem hiding this comment.
Code Review
This pull request ensures that Cobra registers the --version flag by setting RootCmd.Version in internal/cmd/root.go. It also adds an init function in internal/version/version.go to extract the version, VCS revision, and VCS time from the build info when built without -ldflags (e.g., via go install). The reviewer suggested an improvement to also check vcs.modified and append a -dirty suffix to the commit hash if there are uncommitted changes in the working directory.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| // An empty value must not overwrite "unknown" -- it would render as | ||
| // "y509 version dev ()". | ||
| if GitCommit == "unknown" && setting.Value != "" { | ||
| GitCommit = setting.Value | ||
| } | ||
| case "vcs.time": | ||
| if BuildDate == "unknown" && setting.Value != "" { | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
When building via go install or go build from a local repository, the build might contain uncommitted changes (dirty state). Currently, only vcs.revision and vcs.time are captured, which means a dirty build will report a clean commit hash without any indication of modification.
We can improve this by also checking vcs.modified and appending a -dirty suffix to the GitCommit if the working directory had uncommitted changes.
var vcsCommit string
var modified bool
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if setting.Value != "" {
vcsCommit = setting.Value
}
case "vcs.time":
if BuildDate == "unknown" && setting.Value != "" {
BuildDate = setting.Value
}
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
if GitCommit == "unknown" && vcsCommit != "" {
GitCommit = vcsCommit
if modified {
GitCommit += "-dirty"
}
}|
On `-dirty` again — still declining, and the reason is concrete rather than a preference. `GetFullVersion` truncates the commit to 7 characters: ```go So `"-dirty"` renders as `(<7 chars>)` and the suffix never appears in `y509 version`. A dirty build is already flagged anyway: `debug.ReadBuildInfo().Main.Version` carries `+dirty`, which shows in the primary version string. Appending `-dirty` to the truncated hash would be invisible and redundant. The empty-value guard is in. /gemini review |
There was a problem hiding this comment.
Code Review
This pull request ensures Cobra registers the --version flag by setting RootCmd.Version and introduces an init function in the version package to retrieve version, commit, and build date information from the runtime build info when -ldflags are not provided. The review feedback suggests checking the vcs.modified setting to append a -dirty suffix to the commit hash if there are uncommitted changes, ensuring consistency with the Makefile's versioning behavior.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| // An empty value must not overwrite "unknown" -- it would render as | ||
| // "y509 version dev ()". | ||
| if GitCommit == "unknown" && setting.Value != "" { | ||
| GitCommit = setting.Value | ||
| } | ||
| case "vcs.time": | ||
| if BuildDate == "unknown" && setting.Value != "" { | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
When building from a dirty working tree (e.g., using go install or go build locally with uncommitted changes), vcs.revision will return the commit hash, but the binary is actually dirty. To accurately reflect this, we should check the vcs.modified setting and append -dirty to GitCommit if it is true. This aligns with the behavior of the Makefile's git describe --dirty fallback.
var modified bool
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
// An empty value must not overwrite "unknown" -- it would render as
// "y509 version dev ()".
if GitCommit == "unknown" && setting.Value != "" {
GitCommit = setting.Value
}
case "vcs.time":
if BuildDate == "unknown" && setting.Value != "" {
BuildDate = setting.Value
}
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
if modified && GitCommit != "unknown" && !strings.HasSuffix(GitCommit, "-dirty") {
GitCommit += "-dirty"
}|
Still declining `-dirty`, same concrete reason: `GetFullVersion` truncates the commit to 7 characters (`GitCommit[:7]`), so a `-dirty` suffix never reaches the output. A dirty build is already flagged through `debug.ReadBuildInfo().Main.Version`, which carries `+dirty` in the primary version string. Appending it to the truncated hash would be invisible and redundant. /gemini review |
There was a problem hiding this comment.
Code Review
This pull request registers the --version flag in Cobra by setting RootCmd.Version and adds an init function to the version package to extract version, commit, and build date information from build metadata using runtime/debug.ReadBuildInfo(). The review feedback suggests checking the vcs.modified setting to append a -dirty suffix to the commit hash if there are uncommitted changes in the working tree.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| // An empty value must not overwrite "unknown" -- it would render as | ||
| // "y509 version dev ()". | ||
| if GitCommit == "unknown" && setting.Value != "" { | ||
| GitCommit = setting.Value | ||
| } | ||
| case "vcs.time": | ||
| if BuildDate == "unknown" && setting.Value != "" { | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
When building with go install from a dirty working tree, the vcs.revision setting will return the clean commit hash, but the binary actually contains uncommitted changes. To accurately reflect this, we should also check the vcs.modified setting and append -dirty to the GitCommit if it is true and we are falling back to the VCS revision.
var modified bool
var commitUpdated bool
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
// An empty value must not overwrite "unknown" -- it would render as
// "y509 version dev ()".
if GitCommit == "unknown" && setting.Value != "" {
GitCommit = setting.Value
commitUpdated = true
}
case "vcs.time":
if BuildDate == "unknown" && setting.Value != "" {
BuildDate = setting.Value
}
case "vcs.modified":
modified = setting.Value == "true"
}
}
if modified && commitUpdated && !strings.HasSuffix(GitCommit, "-dirty") {
GitCommit += "-dirty"
}
--versiondid not existExecutecalledSetVersionTemplatebut never setRootCmd.Version. Cobra only registers the--versionflag whenVersionis non-empty, so the template was dead code:This was the documented interface:
man/man1/y509.1,completions/y509.bash,completions/y509.zshandscripts/brew-test.shall advertise--version/-v. Only theversionsubcommand actually worked, andbrew-test.shwould fail today.go installbuilds reported themselves asdevThe version variables come from
-ldflagsat release time, andgo installpasses none. But the toolchain has stamped the module version and the VCS revision into the binary —go version -mshows them. Read them back out ofdebug.ReadBuildInfo()when ldflags set nothing.An
-ldflagsbuild still wins — the fallback only runs whileVersionis still"dev". Verified both paths:make lintclean, full suite green.