Repository navigation
fix(engine): prevent Files.Lines panic on empty file - #32290
Conversation
Files.Lines guards against a nil entry but an empty file inside a chart is stored as a non-nil zero-length byte slice, so the trailing newline check indexes s[-1] and panics. The engine recovers the panic into a render error, making every template/install/upgrade/lint that references the file fail. Extend the guard to len(f[path]) == 0 and return an empty slice, matching the behaviour for missing files. Fixes helm#32279 Signed-off-by: Mahesh Sadupalli <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes a deterministic render failure in Helm’s template engine by preventing Files.Lines from panicking when the referenced chart file exists but is empty. This improves reliability for both the Helm CLI and the SDK Engine.Render path.
Changes:
- Update
Files.Linesto treat zero-length file contents as empty and return an empty slice instead of panicking. - Extend unit tests to cover empty files, newline-only files, and missing-path behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/engine/files.go | Adds a len(f[path]) == 0 guard to prevent indexing into an empty string in Files.Lines. |
| pkg/engine/files_test.go | Adds test cases and new unit tests covering empty, newline-only, and missing file paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if f == nil || len(f[path]) == 0 { | ||
| return []string{} | ||
| } | ||
| s := string(f[path]) |
|
Promptless prepared a documentation update related to this change. Triggered by helm/helm PR #32290 This PR fixes Review: docs: note Files.Lines returns empty list for empty/missing files |
|
thanks! |
|
Is this fix planned to be released soon? As I can see it is not part of 4.2.3 but no 4.2.4 is released nor is it ported to that release branch? Any heads up very much appreciated. |
|
I think it should be added to the milestone of the next release. |
|
this probably should have been considered a bug fix, but I just released v4.2.4. the next scheduled release is v4.3.0 in a few weeks so I'll add it to that milestone and label properly. |
|
@scottrigby any plans to release a v3 version with this bug fixed? |
@bruceauyeung v3.21.4 was just released and contains the v3 backport of this fix. |
What this PR does / why we need it:
Files.Linespanics with "index out of range [-1]" when the referenced file is empty. The guard checksf[path] == nil, but an empty file inside a chart is stored as a non-nil zero-length byte slice, sos := string(f[path])is""and the trailing-newline checks[len(s)-1]indexess[-1]. The engine recovers the panic into a render error, so everyhelm template/install/upgrade/lintand every SDKEngine.Rendercall that hits the template fails non-zero.This PR extends the guard to
len(f[path]) == 0and returns an empty slice, matching the existing behaviour for missing files.closes #32279
Special notes for your reviewer:
TestLinesEmptyFilepanics on the current code and passes with the one-line guard change (verified both directions locally).TestLinesNewlineOnlyFilepins the boundary case ("\n"→[""]), which is unchanged, andTestLinesMissingFilepins the missing-path behaviour.dev-v3backport right after this is reviewed — flagging the July 8th 2026 cutoff for v3 bug fixes.If applicable:
docs neededlabel should be applied if so)