Skip to content

fix(version): plumb goreleaser ldflag into --version#19

Merged
aanogueira merged 1 commit intomainfrom
andre.nogueira/version-from-ldflag
May 5, 2026
Merged

fix(version): plumb goreleaser ldflag into --version#19
aanogueira merged 1 commit intomainfrom
andre.nogueira/version-from-ldflag

Conversation

@aanogueira
Copy link
Copy Markdown
Contributor

Summary

`git-context --version` always reported `1.0.0` regardless of which release was installed.

Root cause: `cmd/root.go` hardcoded `Version: "1.0.0"`. The `.goreleaser.yml` already passes `-X main.version={{.Version}}` at build time, but `main.go` had no `version` variable for the ldflag to land in, and the cmd package wouldn't have seen it anyway.

Fix:

  • Declare `var version = "dev"` in `main.go` so the ldflag has a target.
  • Expose `cmd.SetVersion` to overwrite `rootCmd.Version` at startup; call it from `main()`.
  • Default `rootCmd.Version` is now `"dev"` rather than `"1.0.0"`.

Test Plan

  • `go install` / `go build` without ldflags → `git-context version dev`
  • `go build -ldflags="-X main.version=v1.4.2-test" -o /tmp/gc-test .` → `git-context version v1.4.2-test`
  • Existing 176 tests still pass; lint clean

After merge: tag `v1.4.2` and verify the released binary reports the right version.

The cobra root command had `Version: "1.0.0"` hardcoded, so
`git-context --version` always reported 1.0.0 regardless of which
release was installed. .goreleaser.yml already injects the right
value via `-X main.version={{.Version}}`, but main.go had no
matching `version` variable for the ldflag to land in, and even if
it did, cmd/root.go wouldn't see it.

Declare `var version = "dev"` in main.go so the ldflag has a target,
expose `cmd.SetVersion` to overwrite the cobra Version field at
startup, and call it from main(). Local `go install` / `go build`
without ldflags continues to report "dev"; goreleaser builds report
the real tag.

Signed-off-by: Andre Nogueira <aanogueira@protonmail.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/root.go 0.00% 3 Missing ⚠️
main.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@aanogueira aanogueira merged commit dbc93dc into main May 5, 2026
9 checks passed
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.

2 participants