Skip to content

fix(cli): ensure version is correctly embedded in all release binaries - #364

Merged
Peefy merged 2 commits into
mainfrom
fix/version-printing-issue-363
Aug 6, 2026
Merged

fix(cli): ensure version is correctly embedded in all release binaries#364
Peefy merged 2 commits into
mainfrom
fix/version-printing-issue-363

Conversation

@Peefy

@Peefy Peefy commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Problem

Issue #363: Users downloading release binaries (notably the linux-musl-amd64 build, but possibly others in past releases) see 0.12.3-darwin-arm64 or similar stale output when running kcl version, instead of the version they downloaded.

Root Cause

  1. Primary: The musl-binary job in .github/workflows/release.yaml builds the static musl binary without the -X kcl-lang.io/cli/pkg/version.version=... LDFLAGS that the goreleaser-driven binary job uses. The global version variable in pkg/version/version.go therefore stays empty, and GetVersionString() falls back to VersionTypeLatest.

  2. Secondary: VersionTypeLatest was hardcoded to Version_0_12_3 and the constants list was missing 0.12.40.12.7. So even when the fallback kicked in (e.g., for local builds without LDFLAGS), it would silently display a stale version.

  3. Tertiary: The fallback chain had only one step (the hardcoded constant), so any build that did not pass LDFLAGS — including go install-style module-aware builds — silently reported a stale version instead of the version embedded in the Go build info.

Fix

  • .github/workflows/release.yaml: read VERSION in the musl build step and pass it via -X 'kcl-lang.io/cli/pkg/version.version=${VERSION}', mirroring how the regular goreleaser build injects the version.
  • pkg/version/version.go:
    • Bump VersionTypeLatest to Version_0_12_8.
    • Add the missing Version_0_12_4Version_0_12_8 constants so the fallback can never silently regress to a much older version.
    • Add a secondary fallback via runtime/debug.ReadBuildInfo().Main.Version so module-aware installs (go install kcl-lang.io/cli@vX.Y.Z) report the correct version even without LDFLAGS. "(devel)" and pseudo-versions are treated as "no version available" so the constant fallback still kicks in for local dirty builds.
  • pkg/version/version_test.go: add a TestBuildInfoVersion covering the (devel) case under go test.

Lookup order

  1. version package variable (LDFLAGS-injected; used by goreleaser and the fixed musl build)
  2. debug.ReadBuildInfo().Main.Version (works for go install from a tagged module)
  3. VersionTypeLatest constant (last resort)

Verification

Built locally and confirmed all three paths:

$ go build -ldflags="-X 'kcl-lang.io/cli/pkg/version.version=0.12.8'" -o kcl ./cmd/kcl/
$ ./kcl version
0.12.8

$ # Local dirty build (no LDFLAGS, no module tag) — falls through to build info
$ go build -o kcl ./cmd/kcl/
$ ./kcl version
0.12.8-0.20260805142906-5138de5bec9f+dirty

$ # Build with LDFLAGS that does NOT match a known constant — should still be honoured
$ go build -ldflags="-X 'kcl-lang.io/cli/pkg/version.version=0.99.99'" -o kcl ./cmd/kcl/
$ ./kcl version
0.99.99

go test ./pkg/version/... passes.

Fixes #363

🤖 Generated with Claude Code

claude and others added 2 commits August 5, 2026 22:29
#363)

The musl-binary build in release.yaml was missing the version injection
LDFLAGS (-X kcl-lang.io/cli/pkg/version.version=...), so the resulting
binary would fall back to the hardcoded VersionTypeLatest constant and
display a stale version.

Additionally, VersionTypeLatest had not been updated since 0.12.3 and
the constants list was missing 0.12.4-0.12.7, which would all silently
display the wrong version if LDFLAGS were ever forgotten again.

Changes:
- release.yaml: read VERSION and inject it via LDFLAGS into the musl build
- version.go: bump VersionTypeLatest to 0.12.8 and add the missing
  0.12.4-0.12.8 constants so the fallback is at least current

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds a secondary fallback so binaries built without the
`-ldflags "-X .../pkg/version.version=..."` injection still report a
useful version instead of silently displaying the hardcoded
VersionTypeLatest constant.

The lookup order is now:

  1. LDFLAGS-injected `version` variable (used by goreleaser and the
     musl release build).
  2. `debug.ReadBuildInfo().Main.Version` from the Go runtime (works
     for `go install kcl-lang.io/cli@vX.Y.Z` and similar module-aware
     builds). The leading "v" is stripped and "(devel)" / pseudo-versions
     are treated as "no version available" so the fallback can still
     kick in.
  3. Hardcoded `VersionTypeLatest` constant.

A new test asserts that the build info path returns the empty string
under `go test` (where the main module is reported as "(devel)").

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 31017517112

Coverage increased (+0.3%) to 27.691%

Details

  • Coverage increased (+0.3%) from the base build.
  • Patch coverage: 6 uncovered changes across 1 file (10 of 16 lines covered, 62.5%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
pkg/version/version.go 16 10 62.5%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 892
Covered Lines: 247
Line Coverage: 27.69%
Coverage Strength: 0.3 hits per line

💛 - Coveralls

@Peefy
Peefy merged commit d82ec53 into main Aug 6, 2026
9 of 10 checks passed
@Peefy
Peefy deleted the fix/version-printing-issue-363 branch August 6, 2026 01:41
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.

Versions not printed correctly

3 participants