fix: panic in lint when CRD file is empty

Fixes #31571

The YAML decoder can return nil when a CRD file contains only
comments or whitespace. Added nil check to skip validation
in these cases. This fixes a panic in the CRD linting rule.

Signed-off-by: Evans Mungai <mbuevans@gmail.com>
pull/31572/head
Evans Mungai 11 months ago
parent 00e12b8477
commit e252572bda
No known key found for this signature in database
GPG Key ID: BBEB812143DD14E1

@ -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))
}

@ -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)
}

@ -0,0 +1,4 @@
apiVersion: v2
name: emptycrd
description: A Helm chart with empty CRD file
version: 0.1.0

@ -0,0 +1,3 @@
# This file contains only comments and whitespace
# It will decode to nil in the YAML decoder
Loading…
Cancel
Save