Skip to content

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

Closed

Description

@geo-chen

What happened?

reported via email on 26 May 2026 - no response.

What did you expect to happen?

The Files.Lines template helper in pkg/engine/files.go panics with "index out of range [-1]" when the referenced file is empty. The engine recovers the panic and surfaces it as a render error, but every helm template / install / upgrade / lint and every SDK Engine.Render call that hits the template fails non-zero.

Affected: helm.sh/helm/v3 from 3.13.0 onward, helm.sh/helm/v4 all releases including v4.2.0, confirmed against the main branch.

Root cause: the function checks f[path] == nil but not len(f[path]) == 0. An empty file inside a chart is stored as a non-nil zero-length byte slice. s := string(f[path]) is empty, then s[len(s)-1] panics.

How can we reproduce it (as minimally and precisely as possible)?

Proof of concept: a chart containing an empty file in files/ and a single {{ range .Files.Lines "files/empty.txt" }} block fails every render:

Build helm from the affected commit (or use any release from v3.13.0 onward, including v3.21.0 and v4.2.0):

mkdir -p panicchart/templates panicchart/files
cat > panicchart/Chart.yaml <<'EOF'
apiVersion: v2
name: panicchart
version: 0.1.0
EOF
touch panicchart/files/empty.txt
cat > panicchart/templates/cm.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: panic-test
data:
  lines: |
{{- range .Files.Lines "files/empty.txt" }}
    {{ . }}
{{- end }}
EOF

helm template ./panicchart

Observed:

Error: template: panicchart/templates/cm.yaml:7:16: executing "panicchart/templates/cm.yaml" at <.Files.Lines>: error calling Lines: runtime error: index out of range [-1]
Use --debug flag to render out invalid YAML

helm install --dry-run mytest ./panicchart produces the same error and exits non-zero.

Suggested fix: guard with len(f[path]) == 0 and use strings.TrimSuffix(s, "\n") instead of subscripting.

Helm version

3.13.0

Kubernetes version

Activity

  1. SudhanshuMatrix commented on Jul 2, 2026

    @SudhanshuMatrix

    Hi everyone,
    I have opened a PR #32280 to address this issue

  2. mahesh-sadupalli commented on Jul 3, 2026

    @mahesh-sadupalli
    Contributor

    I'd like to take this one. I verified the root cause on main: files.Lines (pkg/engine/files.go) guards f == nil || f[path] == nil, but an empty file in a chart is stored as a non-nil zero-length byte slice, so s[len(s)-1] panics with index -1.

    Plan:

    • Extend the guard to len(f[path]) == 0 and return []string{}, keeping the nil/missing-path behaviour unchanged.
    • Unit tests in files_test.go: empty file, file containing only a newline (the boundary the trailing-newline trim is about), and the existing non-empty cases untouched.
    • Target main (v4) first per the contributing guide, then open the dev-v3 backport right away, since v3 bug fixes are only accepted until July 8th 2026.

    Commits will be DCO signed. Will have the PR up shortly.

  3. added a commit that references this issue on Jul 16, 2026
    143631f
  4. added a commit that references this issue on Jul 28, 2026
    c02a9ae
  5. added
    bug
    on Aug 13, 2026
  6. added a commit that references this issue on Aug 13, 2026
    955dfab
  7. ahachmann commented on Aug 14, 2026

    @ahachmann

    Is there any reason for this not being added to the new 4.2.4 Release? We were hoping it would make it in.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions