From 562ff982cb37aada7b98a755c8c37563c1e45577 Mon Sep 17 00:00:00 2001 From: Zach Burgess Date: Tue, 1 Jul 2025 13:52:15 -0700 Subject: [PATCH] Early return if the `/crds` directory does not exist and don't silently discard the error from `os.Stat`. Signed-off-by: Zach Burgess --- pkg/lint/lint_test.go | 2 +- pkg/lint/rules/crds.go | 17 ++++++++++------- pkg/lint/rules/crds_test.go | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/lint/lint_test.go b/pkg/lint/lint_test.go index 6c380409c..45e24f533 100644 --- a/pkg/lint/lint_test.go +++ b/pkg/lint/lint_test.go @@ -40,7 +40,7 @@ const invalidChartFileDir = "rules/testdata/invalidchartfile" func TestBadChart(t *testing.T) { m := RunAll(badChartDir, values, namespace).Messages - if len(m) != 9 { + if len(m) != 8 { t.Errorf("Number of errors %v", len(m)) t.Errorf("All didn't fail with expected errors, got %#v", m) } diff --git a/pkg/lint/rules/crds.go b/pkg/lint/rules/crds.go index c978bfc53..4740157b3 100644 --- a/pkg/lint/rules/crds.go +++ b/pkg/lint/rules/crds.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path/filepath" @@ -35,13 +36,13 @@ func Crds(linter *support.Linter) { fpath := "crds/" crdsPath := filepath.Join(linter.ChartDir, fpath) - crdsDirExist := linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath)) - // crds directory is optional - if !crdsDirExist { + if _, err := os.Stat(crdsPath); errors.Is(err, fs.ErrNotExist) { return } + linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath)) + // Load chart and parse CRDs chart, err := loader.Load(linter.ChartDir) @@ -81,10 +82,12 @@ func Crds(linter *support.Linter) { // Validation functions func validateCrdsDir(crdsPath string) error { - if fi, err := os.Stat(crdsPath); err == nil { - if !fi.IsDir() { - return errors.New("not a directory") - } + fi, err := os.Stat(crdsPath) + if err != nil { + return err + } + if !fi.IsDir() { + return errors.New("not a directory") } return nil } diff --git a/pkg/lint/rules/crds_test.go b/pkg/lint/rules/crds_test.go index 52432a130..a84b62a50 100644 --- a/pkg/lint/rules/crds_test.go +++ b/pkg/lint/rules/crds_test.go @@ -23,11 +23,11 @@ import ( "helm.sh/helm/v4/pkg/lint/support" ) -const crdsTestBasedir = "./testdata/withcrd" +const crdsTestBaseDir = "./testdata/withcrd" const invalidCrdsDir = "./testdata/invalidcrdsdir" func TestCrdsDir(t *testing.T) { - linter := support.Linter{ChartDir: crdsTestBasedir} + linter := support.Linter{ChartDir: crdsTestBaseDir} Crds(&linter) res := linter.Messages