Skip to content

fix: fall back to debug.ReadBuildInfo for version when ldflags not set#332

Open
znd4 wants to merge 7 commits into
norwoodj:masterfrom
znd4:fix/version-fallback-build-info
Open

fix: fall back to debug.ReadBuildInfo for version when ldflags not set#332
znd4 wants to merge 7 commits into
norwoodj:masterfrom
znd4:fix/version-fallback-build-info

Conversation

@znd4

@znd4 znd4 commented Apr 8, 2026

Copy link
Copy Markdown

Problem

When helm-docs is built via go install (e.g., by pre-commit's language: golang hook type for the helm-docs-built hook), no -ldflags are passed, so the version variable remains an empty string. This causes:

  1. helm-docs --version prints helm-docs version with no version
  2. The README footer (Autogenerated from chart metadata using [helm-docs vX.Y.Z]) is not rendered, because the default template checks {{ if .HelmDocsVersion }}

This means the helm-docs-built pre-commit hook produces different output than the release binary, causing CI failures when the committed README was generated by a release binary (or vice versa).

Fix

Add a fallback using runtime/debug.ReadBuildInfo() in an init() function. When Go builds a binary via go install module@version, it embeds the module version in the binary metadata. This is a stdlib-only change with no new dependencies.

  • The ldflags-injected version always takes priority (guard clause returns early)
  • The v prefix from Go module versions is stripped to match the existing convention (the footer template already prepends v)

Reproduction & validation

A test workflow on the fork validates both paths:

  • go install ./cmd/helm-docs (no ldflags): version is set, footer renders ✅
  • pre-commit helm-docs-built hook: footer renders, output is idempotent ✅

znd4 and others added 7 commits April 7, 2026 22:42
When helm-docs is installed via `go install` (e.g., by pre-commit's
`language: golang` hook type), no -ldflags are passed, so the `version`
variable remains empty. This causes the README footer template
(`{{ if .HelmDocsVersion }}`) to produce no output, resulting in
different output from the release binary and CI failures.

Add a fallback using `runtime/debug.ReadBuildInfo()` in an init()
function. When `go install module@version` builds a binary, Go embeds
the module version in the binary metadata. The ldflags-set version
still takes priority when present (e.g., goreleaser builds).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Temporary workflow to verify that the debug.ReadBuildInfo fallback
works correctly for both `go install` and pre-commit helm-docs-built.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
go install from a local checkout sets a pseudo-version, not (devel).
Check that the version string is non-empty instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
YAML heredoc content was indented by the workflow step indentation,
producing invalid Chart.yaml. Use printf instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Strip leading "v" from debug.ReadBuildInfo version since the
  footer template already prepends "v" (avoids "vv0.0.0-..." output)
- Use dedicated directory for test chart to avoid /tmp permission errors

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When helm-docs is built from a local checkout (e.g. pre-commit's
language: golang hook), ReadBuildInfo().Main.Version returns "(devel)"
rather than a meaningful version. Filter this out so the footer
template skips the version rather than rendering "v(devel)".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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