diff --git a/pkg/chart/v2/lint/rules/crds.go b/pkg/chart/v2/lint/rules/crds.go index 49e30192a..09ea17bd8 100644 --- a/pkg/chart/v2/lint/rules/crds.go +++ b/pkg/chart/v2/lint/rules/crds.go @@ -80,6 +80,11 @@ func Crds(linter *support.Linter) { return } + // Skip validation if yamlStruct is nil (empty document or only comments/whitespace) + if yamlStruct == nil { + continue + } + linter.RunLinterRule(support.ErrorSev, fpath, validateCrdAPIVersion(yamlStruct)) linter.RunLinterRule(support.ErrorSev, fpath, validateCrdKind(yamlStruct)) } diff --git a/pkg/chart/v2/lint/rules/crds_test.go b/pkg/chart/v2/lint/rules/crds_test.go index e644f182f..d0553dc0d 100644 --- a/pkg/chart/v2/lint/rules/crds_test.go +++ b/pkg/chart/v2/lint/rules/crds_test.go @@ -25,6 +25,7 @@ import ( ) const invalidCrdsDir = "./testdata/invalidcrdsdir" +const emptyCrdDir = "./testdata/emptycrd" func TestInvalidCrdsDir(t *testing.T) { linter := support.Linter{ChartDir: invalidCrdsDir} @@ -34,3 +35,15 @@ func TestInvalidCrdsDir(t *testing.T) { assert.Len(t, res, 1) assert.ErrorContains(t, res[0].Err, "not a directory") } + +// TestEmptyCrd tests that empty CRD files (containing only comments/whitespace) +// don't cause a panic when the YAML decoder returns nil. +// This is a regression test for issue #31571. +func TestEmptyCrd(t *testing.T) { + linter := support.Linter{ChartDir: emptyCrdDir} + Crds(&linter) + res := linter.Messages + + // Should not panic and should have no errors + assert.Len(t, res, 0) +} diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml b/pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml new file mode 100644 index 000000000..80f4faaca --- /dev/null +++ b/pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: emptycrd +description: A Helm chart with empty CRD file +version: 0.1.0 diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml b/pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml new file mode 100644 index 000000000..743b6be06 --- /dev/null +++ b/pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml @@ -0,0 +1,3 @@ +# This file contains only comments and whitespace +# It will decode to nil in the YAML decoder + diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/templates/.gitkeep b/pkg/chart/v2/lint/rules/testdata/emptycrd/templates/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml b/pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml new file mode 100644 index 000000000..0eba2d28f --- /dev/null +++ b/pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml @@ -0,0 +1 @@ +# Empty values file