Skip to content

fix: insert newline after doc separators glued to content by template trimming#31868

Merged
TerryHowe merged 1 commit into
helm:mainfrom
matheuscscp:fix-31867
Mar 9, 2026
Merged

fix: insert newline after doc separators glued to content by template trimming#31868
TerryHowe merged 1 commit into
helm:mainfrom
matheuscscp:fix-31867

Conversation

@matheuscscp

Copy link
Copy Markdown
Contributor

Fixes: #31867

Copilot AI review requested due to automatic review settings February 26, 2026 14:24
@pull-request-size pull-request-size Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 26, 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

This PR fixes a regression where Go template whitespace trimming ({{-) could produce invalid YAML by concatenating the document separator --- directly with content (e.g., ---apiVersion), which violates the YAML specification requiring separators to be on their own line.

Changes:

  • Introduced stripLeadingDocSeparators function to remove leading --- separators from rendered templates
  • Integrated stripping logic into annotateAndMerge before parsing YAML documents
  • Added comprehensive test coverage for various separator scenarios

Reviewed changes

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

File Description
pkg/action/action.go Implements stripLeadingDocSeparators function and integrates it into the annotateAndMerge workflow to handle malformed document separators
pkg/action/action_test.go Adds unit tests for stripLeadingDocSeparators and integration tests for annotateAndMerge covering the regression case

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

@matheuscscp
matheuscscp force-pushed the fix-31867 branch 2 times, most recently from 2b40cd8 to 164409a Compare February 26, 2026 15:04
@matheuscscp matheuscscp changed the title Fix regression of ---\n{{- something }}\napiVersion degenerating to ---apiVersion fix: insert newline after doc separators glued to content by template trimming Feb 26, 2026
@scottrigby scottrigby added the bug Categorizes issue or PR as related to a bug. label Feb 26, 2026
@scottrigby scottrigby added this to the 4.1.2 milestone Feb 26, 2026
@matheuscscp

Copy link
Copy Markdown
Contributor Author

@banjoh @gjenkins8 Can you please review? 4.1.2 is coming soon

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

lgtm

Comment thread pkg/action/action_test.go
Copilot AI review requested due to automatic review settings March 6, 2026 18:58

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


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

You can also share your feedback on Copilot code review. Take the survey.

Comment thread pkg/action/action.go
Comment thread pkg/action/action.go Outdated

@scottrigby scottrigby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@matheuscscp thanks for the fix. Note that this is a Helm 4 only regression from Helm 3, so we should ship this in the next Helm 4 patch release.

Also addressed change requested by @TerryHowe and @banjoh in #31868 (comment).

As a follow-up, I would however like to propose that in Charts apiVersion v3 we drop support for special handling around gotemplate whitespace trimming for yaml separators. In general, when a {{- is used, all leading white space should be trimmed from the immediately following text - including "space, horizontal tab, carriage return, and newline" (see https://pkg.go.dev/text/template#hdr-Text_and_spaces). I do not yet know why we handle this special case as opposed to asking chart authors to fix this. We do have to support this for existing apiVersion v2 charts for backwards compatibility (both with Helm 3 and Helm 4), but Charts apiVersion v3 could simplify our code by not handling this in a special way for YAML separators. We would need to add this to a charts v2 -> v3 migration guide if we do.

Comment thread pkg/action/action.go
remaining = remaining[idx+3:]
// If "---" is followed by non-whitespace (e.g. "---apiVersion"),
// insert a newline to make it a proper document separator.
if len(remaining) > 0 && remaining[0] != '\n' && remaining[0] != '\r' && remaining[0] != ' ' && remaining[0] != '\t' {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice. note that this conforms to go's definition of whitespace trimming in templating context: https://pkg.go.dev/text/template#hdr-Text_and_spaces

For this trimming, the definition of white space characters is the same as in Go: space, horizontal tab, carriage return, and newline.

@banjoh banjoh 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 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 merged commit 3e7b38f into helm:main Mar 9, 2026
5 checks passed
@matheuscscp
matheuscscp deleted the fix-31867 branch March 9, 2026 18:33
@scottrigby scottrigby added needs-pick Indicates that a PR needs to be cherry-picked into the next release candidate. picked Indicates that a PR has been cherry-picked into the next release candidate. and removed needs-pick Indicates that a PR needs to be cherry-picked into the next release candidate. labels Mar 10, 2026
@scottrigby scottrigby removed this from the 4.1.2 milestone Mar 11, 2026
@scottrigby scottrigby added this to the 4.1.3 milestone Mar 11, 2026
@matheuscscp

matheuscscp commented Mar 12, 2026

Copy link
Copy Markdown
Contributor Author

@matheuscscp thanks for the fix. Note that this is a Helm 4 only regression from Helm 3, so we should ship this in the next Helm 4 patch release.

Also addressed change requested by @TerryHowe and @banjoh in #31868 (comment).

As a follow-up, I would however like to propose that in Charts apiVersion v3 we drop support for special handling around gotemplate whitespace trimming for yaml separators. In general, when a {{- is used, all leading white space should be trimmed from the immediately following text - including "space, horizontal tab, carriage return, and newline" (see https://pkg.go.dev/text/template#hdr-Text_and_spaces). I do not yet know why we handle this special case as opposed to asking chart authors to fix this. We do have to support this for existing apiVersion v2 charts for backwards compatibility (both with Helm 3 and Helm 4), but Charts apiVersion v3 could simplify our code by not handling this in a special way for YAML separators. We would need to add this to a charts v2 -> v3 migration guide if we do.

@scottrigby We discussed in the Flux dev meeting today about this breaking change in Chart API v3. It was noted that this kind of behavior should not happen only when using postrenderers. The behavior fixed by this PR should be caught as an error for example on helm template, as the charts committing this abuse do not expect any form of postrendering at all, so the behavior with or without postrenderers must be the same.

@scottrigby

Copy link
Copy Markdown
Member

he behavior with or without postrenderers must be the same

@matheuscscp yes, without question 👍 agree that any discrepancy between the two would be a bug, regardless of the expected behavior.

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. picked Indicates that a PR has been cherry-picked into the next release candidate. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression from Helm 3: annotateAndMerge corrupts manifests when rendered template content has --- followed by no newline

6 participants