fix: report module version for go install builds - #4
Merged
Conversation
GoReleaser injects the version via -ldflags, so release binaries report the right version, but `go install ...@latest` left it as "dev". Fall back to the module version Go embeds in the build info when the ldflag is unset, so installed binaries report e.g. 1.0.0 (the "v" prefix is trimmed for parity with GoReleaser output). Local builds without a VCS version keep "dev". The binary smoke test no longer asserts a literal version, since a local build's version now depends on build context (ldflag, module version, or VCS stamp). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
go install github.com/tanem/mt5-pnl-cli@latestproduces a binary whoseversionsubcommand reportsdev:GoReleaser injects the version via
-ldflags "-X main.version=...", so the downloaded release binaries report1.0.0correctly — butgo installnever applies those flags, leaving thedevdefault. This is the standard GoReleaser/go installgap.Fix
When the ldflag is unset (
version == "dev"), fall back to the module version Go embeds in the build info —runtime/debug.ReadBuildInfo().Main.Version— which isv1.0.0forgo install ...@v1.0.0/@latest. The leadingvis trimmed for parity with GoReleaser output. Local builds with no VCS version ((devel)/empty) keepdev.Release builds are unaffected: the ldflag wins whenever it's set, so GoReleaser output stays
1.0.0.Notes
versionFrom(ldflag, reader)so all branches are unit-tested deterministically (the packageversionvar anddebug.ReadBuildInfo()can't be exercised cleanly otherwise).Test plan
go test -race ./...greengofmt -l .clean,go vet ./...cleanTestVersionFromcovers: ldflag wins, go-install resolves module version,(devel)→ dev, empty → dev, missing build info → dev.🤖 Generated with Claude Code