From 7f188581829900992175c671d4868ec02556601e Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 Aug 2025 21:11:09 +0800 Subject: [PATCH] Fix parseHelmVersion to handle helm versions without 'v' prefix (#2132) * Initial plan * Fix panic in helmfile init when parsing invalid helm versions Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> * Fix parseHelmVersion to handle versions without v prefix Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> * Simplify parseHelmVersion function to be more readable Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --- pkg/helmexec/exec.go | 8 +++++++- pkg/helmexec/exec_test.go | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/helmexec/exec.go b/pkg/helmexec/exec.go index a2044470..e2df4423 100644 --- a/pkg/helmexec/exec.go +++ b/pkg/helmexec/exec.go @@ -69,7 +69,13 @@ func parseHelmVersion(versionStr string) (*semver.Version, error) { return nil, fmt.Errorf("empty helm version") } - v, err := chartify.FindSemVerInfo(versionStr) + // Check if version string starts with "v", if not add it + processedVersion := strings.TrimSpace(versionStr) + if !strings.HasPrefix(processedVersion, "v") { + processedVersion = "v" + processedVersion + } + + v, err := chartify.FindSemVerInfo(processedVersion) if err != nil { return nil, fmt.Errorf("error find helm srmver version '%s': %w", versionStr, err) diff --git a/pkg/helmexec/exec_test.go b/pkg/helmexec/exec_test.go index f0af5aa5..bbf88808 100644 --- a/pkg/helmexec/exec_test.go +++ b/pkg/helmexec/exec_test.go @@ -1140,6 +1140,24 @@ func TestParseHelmVersion(t *testing.T) { want: nil, wantErr: true, }, + { + name: "version without v prefix", + version: "3.2.4", + want: semver.MustParse("v3.2.4"), + wantErr: false, + }, + { + name: "version without v prefix with build info", + version: "3.2.4+ge29ce2a", + want: semver.MustParse("v3.2.4+ge29ce2a"), + wantErr: false, + }, + { + name: "version without v prefix with spaces", + version: " 3.2.4 ", + want: semver.MustParse("v3.2.4"), + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {