Skip to content

fix(cmd): register the --version flag, and report a real version on go install#55

Open
kanywst wants to merge 4 commits into
mainfrom
fix/version-flag
Open

fix(cmd): register the --version flag, and report a real version on go install#55
kanywst wants to merge 4 commits into
mainfrom
fix/version-flag

Conversation

@kanywst

@kanywst kanywst commented Jul 13, 2026

Copy link
Copy Markdown
Owner

--version did not exist

Execute called SetVersionTemplate but never set RootCmd.Version. Cobra only registers the --version flag when Version is non-empty, so the template was dead code:

$ y509 --version
Error: unknown flag: --version
$ y509 -v
Error: unknown shorthand flag: 'v' in -v

This was the documented interface: man/man1/y509.1, completions/y509.bash, completions/y509.zsh and scripts/brew-test.sh all advertise --version / -v. Only the version subcommand actually worked, and brew-test.sh would fail today.

go install builds reported themselves as dev

The version variables come from -ldflags at release time, and go install passes none. But the toolchain has stamped the module version and the VCS revision into the binary — go version -m shows them. Read them back out of debug.ReadBuildInfo() when ldflags set nothing.

before:  y509 version dev
after:   y509 version v0.12.1-0.20260706091141-6f91c879576a
         Build: v0.12.1-... (6f91c87) built on 2026-07-06T09:11:41Z

An -ldflags build still wins — the fallback only runs while Version is still "dev". Verified both paths:

$ make build && ./y509 version        # ldflags
y509 version v0.12.0-9-g6f91c87

$ go install ./cmd/y509 && y509 version   # no ldflags
y509 version v0.12.1-0.20260706091141-6f91c879576a

make lint clean, full suite green.

kanywst added 2 commits July 13, 2026 23:54
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".
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@kanywst, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 21 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 256410b6-3dfd-45db-b7ec-f2bb500087e6

📥 Commits

Reviewing files that changed from the base of the PR and between 6f91c87 and cf1ac28.

📒 Files selected for processing (2)
  • internal/cmd/root.go
  • internal/version/version.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/version-flag

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +22 to +45
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
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.
@kanywst

kanywst commented Jul 14, 2026

Copy link
Copy Markdown
Owner Author

Thanks, good catch. Applied.

The early return meant a build supplying only Version through -ldflags would skip the VCS revision and build time the toolchain had already recorded, and report unknown for both. Each value now falls back independently.

Both paths still behave:

$ y509 version                 # no ldflags, go install style
y509 version v0.12.1-0.20260713145411-92110c4b2daf
Build: ... (92110c4) built on 2026-07-13T14:54:11Z

$ make build && ./y509 version # ldflags still wins
y509 version v0.12.0-11-g92110c4

make lint clean, full suite green.

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +37 to +48
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
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.
@kanywst

kanywst commented Jul 15, 2026

Copy link
Copy Markdown
Owner Author

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +37 to +50
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
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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"
	}

@kanywst

kanywst commented Jul 15, 2026

Copy link
Copy Markdown
Owner Author

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
if len(GitCommit) > 7 { version += " (" + GitCommit[:7] + ")" }
```

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +37 to +50
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
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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"
		}
	}

@kanywst

kanywst commented Jul 16, 2026

Copy link
Copy Markdown
Owner Author

On `-dirty` again — still declining, and the reason is concrete rather than a preference. `GetFullVersion` truncates the commit to 7 characters:

```go
if len(GitCommit) > 7 { version += " (" + GitCommit[:7] + ")" }
```

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +37 to +50
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
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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"
	}

@kanywst

kanywst commented Jul 16, 2026

Copy link
Copy Markdown
Owner Author

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +37 to +50
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
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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"
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant