Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion cmd/ralphex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"runtime/debug"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -48,6 +49,29 @@ type opts struct {

var revision = "unknown"

// resolveVersion returns the best available version string.
// priority: ldflags revision → module version from go install → VCS commit hash → "unknown".
func resolveVersion() string {
if revision != "unknown" {
return revision
}
bi, ok := debug.ReadBuildInfo()
if !ok {
return revision
}
// go install sets module version to the tag (e.g. v0.10.0)
if bi.Main.Version != "" && bi.Main.Version != "(devel)" {
return bi.Main.Version
}
// local build without ldflags — try VCS revision
for _, s := range bi.Settings {
if s.Key == "vcs.revision" && len(s.Value) >= 7 {
return s.Value[:7]
}
}
return revision
}

// stderrLog is a simple logger that writes to stderr.
// satisfies notify.logger interface for use before progress logger is available.
type stderrLog struct{}
Expand Down Expand Up @@ -79,7 +103,7 @@ type executePlanRequest struct {
}

func main() {
fmt.Printf("ralphex %s\n", revision)
fmt.Printf("ralphex %s\n", resolveVersion())

var o opts
parser := flags.NewParser(&o, flags.Default)
Expand Down
19 changes: 19 additions & 0 deletions cmd/ralphex/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,22 @@ func setupTestRepo(t *testing.T) string {

return dir
}

func TestResolveVersion(t *testing.T) {
t.Run("ldflags_set", func(t *testing.T) {
orig := revision
t.Cleanup(func() { revision = orig })
revision = "v1.2.3-abc1234"
assert.Equal(t, "v1.2.3-abc1234", resolveVersion())
})

t.Run("fallback_to_build_info", func(t *testing.T) {
orig := revision
t.Cleanup(func() { revision = orig })
revision = "unknown"
// in test context, debug.ReadBuildInfo returns (devel) module version
// but VCS info should be available from the git repo
v := resolveVersion()
assert.NotEmpty(t, v)
})
Comment on lines +897 to +905
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

fallback_to_build_info currently only asserts the resolved version is non-empty, but resolveVersion() can legitimately return the literal string "unknown" here, making this test pass even if the build-info fallback logic is broken. Consider asserting that the result is not equal to "unknown" when build info contains either a non-"(devel)" module version or a vcs.revision setting, and otherwise explicitly asserting it equals "unknown" (branching based on debug.ReadBuildInfo() in the test to avoid flakiness).

Copilot uses AI. Check for mistakes.
}
Loading