From b8b9cad2ff1a282d9a1c4912f267f702c1375344 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 Aug 2025 04:33:35 +0000 Subject: [PATCH] Fix panic in helmfile init when parsing invalid helm versions Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --- cmd/root.go | 4 +-- cmd/root_error_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 cmd/root_error_test.go diff --git a/cmd/root.go b/cmd/root.go index ed629d12..6f3580e5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "github.com/spf13/cobra" @@ -35,7 +34,8 @@ func toCLIError(g *config.GlobalImpl, err error) error { case *app.Error: return errors.NewExitError(e.Error(), e.Code()) default: - panic(fmt.Errorf("BUG: please file an github issue for this unhandled error: %T: %v", e, e)) + // Handle all other errors gracefully by returning an exit error with code 1 + return errors.NewExitError(e.Error(), 1) } } return err diff --git a/cmd/root_error_test.go b/cmd/root_error_test.go new file mode 100644 index 00000000..2614525d --- /dev/null +++ b/cmd/root_error_test.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "testing" + "fmt" + + "github.com/helmfile/helmfile/pkg/config" +) + +// TestToCLIErrorHandlesStandardErrors tests that toCLIError properly handles +// standard Go errors instead of panicking. This test prevents regression of +// the issue fixed in https://github.com/helmfile/helmfile/issues/2119 +func TestToCLIErrorHandlesStandardErrors(t *testing.T) { + globalImpl := &config.GlobalImpl{} + + tests := []struct { + name string + err error + expectPanic bool + }{ + { + name: "nil error should not panic", + err: nil, + expectPanic: false, + }, + { + name: "standard fmt.Errorf should not panic", + err: fmt.Errorf("error find helm srmver version '%s': unable to find semver info", "invalid"), + expectPanic: false, + }, + { + name: "standard error string should not panic", + err: fmt.Errorf("empty helm version"), + expectPanic: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer func() { + if r := recover(); r != nil { + if !tt.expectPanic { + t.Errorf("toCLIError() panicked unexpectedly: %v", r) + } + } else { + if tt.expectPanic { + t.Errorf("toCLIError() expected to panic but did not") + } + } + }() + + _ = toCLIError(globalImpl, tt.err) + }) + } +} \ No newline at end of file