Skip to content

fix(engine): prevent Files.Lines panic on empty file - #32290

Merged
TerryHowe merged 1 commit into
helm:mainfrom
mahesh-sadupalli:fix/files-lines-empty-panic
Jul 16, 2026
Merged

TerryHowe merged 1 commit into
helm:mainfrom
mahesh-sadupalli:fix/files-lines-empty-panic

Conversation

@mahesh-sadupalli

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

Files.Lines panics with "index out of range [-1]" when the referenced file is empty. The guard checks f[path] == nil, but an empty file inside a chart is stored as a non-nil zero-length byte slice, so s := string(f[path]) is "" and the trailing-newline check s[len(s)-1] indexes s[-1]. The engine recovers the panic into a render error, so every helm template / install / upgrade / lint and every SDK Engine.Render call that hits the template fails non-zero.

This PR extends the guard to len(f[path]) == 0 and returns an empty slice, matching the existing behaviour for missing files.

closes #32279

Special notes for your reviewer:

  • The new TestLinesEmptyFile panics on the current code and passes with the one-line guard change (verified both directions locally).
  • TestLinesNewlineOnlyFile pins the boundary case ("\n" → [""]), which is unchanged, and TestLinesMissingFile pins the missing-path behaviour.
  • Happy to open the dev-v3 backport right after this is reviewed — flagging the July 8th 2026 cutoff for v3 bug fixes.

If applicable:

  • this PR contains user facing changes (the docs needed label should be applied if so)
  • this PR contains unit tests
  • this PR has been tested for backwards compatibility

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]>
Copilot AI review requested due to automatic review settings July 3, 2026 18:30
@pull-request-size pull-request-size Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Lines to 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.

Comment thread pkg/engine/files.go
Comment on lines +157 to 160
if f == nil || len(f[path]) == 0 {
return []string{}
}
s := string(f[path])
@promptless-for-oss

Copy link
Copy Markdown

Promptless prepared a documentation update related to this change.

Triggered by helm/helm PR #32290

This PR fixes Files.Lines panicking on empty files by returning an empty list (matching the behavior for missing files). The chart template guide previously made no claim about empty files, so we added a short note documenting that Lines returns an empty list for empty or missing files and that a range over it simply produces no output.

Review: docs: note Files.Lines returns empty list for empty/missing files

@gjenkins8

Copy link
Copy Markdown
Member

thanks!

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@TerryHowe TerryHowe added Has One Approval This PR has one approval. It still needs a second approval to be merged. Needs v3 backport Label PRs for v4/main, which are still applicable to v3 so need a separate backport PR labels Jul 16, 2026
@TerryHowe
TerryHowe merged commit ba6c9a2 into helm:main Jul 16, 2026
5 checks passed
@ahachmann

Copy link
Copy Markdown

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.
Thanks

@benoittgt

Copy link
Copy Markdown
Contributor

I think it should be added to the milestone of the next release.

@scottrigby scottrigby added refactor and removed Has One Approval This PR has one approval. It still needs a second approval to be merged. labels Aug 12, 2026
@scottrigby

Copy link
Copy Markdown
Member

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 scottrigby added this to the 4.3.0 milestone Aug 13, 2026
@scottrigby scottrigby added bug Categorizes issue or PR as related to a bug. and removed refactor labels Aug 13, 2026
@bruceauyeung

Copy link
Copy Markdown

@scottrigby any plans to release a v3 version with this bug fixed?

@scottrigby

Copy link
Copy Markdown
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Categorizes issue or PR as related to a bug. Needs v3 backport Label PRs for v4/main, which are still applicable to v3 so need a separate backport PR size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Helm Files.Lines panic on empty file causes deterministic render failure

9 participants