Skip to content

fix(engine): guard Files.Lines against empty file panic - #32280

Closed
SudhanshuMatrix wants to merge 1 commit into
helm:mainfrom
SudhanshuMatrix:fix/files-lines-empty-panic
Closed

SudhanshuMatrix wants to merge 1 commit into
helm:mainfrom
SudhanshuMatrix:fix/files-lines-empty-panic

Conversation

@SudhanshuMatrix

Copy link
Copy Markdown

What this PR does / why we need it:
This PR fixes a runtime panic in the .Files.Lines template helper function when referencing an empty file inside a chart.

An empty file inside a chart is loaded and stored as a non-nil, zero-length byte slice ([]byte{}). The function previously checked only f[path] == nil, which evaluated to false, and then attempted to trim the newline character using s[len(s)-1] on the empty string s, resulting in a panic: runtime error: index out of range [-1].

To resolve this, we:

  • Guard the helper by checking if the slice length is zero: len(f[path]) == 0.
  • Trim the trailing newline safely using strings.TrimSuffix(s, "\n").

closes #32279

Special notes for your reviewer:

  • Checked and verified that behavior remains identical for non-empty files (both with and without trailing newlines) and non-existent files.
  • Added unit test cases for empty files and non-existent files under TestLines in pkg/engine/files_test.go.

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

Avoid panicking with index out of range when calling Lines on an empty file. Checking the length of the slice instead of checking for nil ensures empty files do not cause panic, and using strings.TrimSuffix makes newline trimming safer.

Signed-off-by: Sudhanshu <[email protected]>
Copilot AI review requested due to automatic review settings July 2, 2026 04:08
@pull-request-size pull-request-size Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jul 2, 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 runtime panic in the pkg/engine template helper .Files.Lines when the referenced chart file exists but is empty ([]byte{}), ensuring template rendering succeeds across Helm CLI and SDK usage.

Changes:

  • Update files.Lines to treat zero-length file contents as empty input and avoid out-of-range indexing.
  • Replace manual trailing newline removal with strings.TrimSuffix.
  • Add unit test coverage for empty-file and non-existent-file cases in TestLines.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/engine/files.go Adds a zero-length guard and safe newline trimming to prevent panics in .Files.Lines.
pkg/engine/files_test.go Extends TestLines with empty and missing file cases to validate the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kunalworldwide kunalworldwide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The guard is correct — an empty file should return an empty slice, not panic. Using len(f[path]) == 0 also correctly handles the nil map entry case, so the behavior is unchanged for missing files.

Switching from manual index slicing to strings.TrimSuffix(s, "\n") is safer and handles the empty-string case naturally. The added test cases for empty and non-existent files cover the regression paths.

LGTM.

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

Labels

size/S Denotes a PR that changes 10-29 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

3 participants