Skip to content

Fix YAML document splitting to only split on unindented --- separators#349

Merged
misraved merged 1 commit into
mainfrom
issue-341-fix-splitting-logic
Mar 6, 2026
Merged

Fix YAML document splitting to only split on unindented --- separators#349
misraved merged 1 commit into
mainfrom
issue-341-fix-splitting-logic

Conversation

@graza-io
Copy link
Copy Markdown
Contributor

Summary

  • Fixes Yaml stream separator in a string breaks parsing manifests #341: YAML stream separator (---) inside a multi-line string value was incorrectly treated as a document boundary, breaking manifest parsing
  • Replaced naive strings.Split(content, "---") with a regex split (?m)^---\s*$ that only matches --- at column 0 (per YAML spec), across all 4 split locations in utils.go and helm_template_render.go
  • Added 14 unit tests covering normal splits, indented separators, the exact issue scenario, edge cases

Test plan

  • go build ./... passes
  • go vet ./... passes
  • go test ./kubernetes/ -run TestYamlDocSeparator — all 14 tests pass
  • Manual test with a manifest containing an indented --- inside a string value (e.g. the Datadog ConfigMap from the issue)

🤖 Generated with Claude Code

#341)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@steakunderscore steakunderscore left a comment

Choose a reason for hiding this comment

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

LGTM

@graza-io graza-io requested a review from Copilot March 5, 2026 20:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes YAML manifest splitting so document boundaries are recognized only on unindented --- separator lines, preventing accidental splits inside multi-line string values.

Changes:

  • Introduced a package-level regex (yamlDocSeparator) to match YAML document separators at column 0.
  • Replaced strings.Split(..., "---") with yamlDocSeparator.Split(..., -1) across manifest/template parsing paths.
  • Added unit tests covering correct splits and the reported issue scenario.

Reviewed changes

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

File Description
kubernetes/utils.go Adds the anchored YAML separator regex and uses it for manifest splitting.
kubernetes/helm_template_render.go Uses the anchored separator regex when splitting Helm-rendered manifests/templates.
kubernetes/yaml_split_test.go Adds focused unit tests validating separator splitting behavior (including issue #341).

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

Comment thread kubernetes/utils.go
Comment on lines +40 to +43
// of a line (column 0), optionally followed by trailing whitespace. Per the
// YAML spec, an indented "---" inside a multi-line string value is NOT a
// document separator, so we must anchor to the beginning of the line.
var yamlDocSeparator = regexp.MustCompile(`(?m)^---\s*$`)
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

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

This regex won’t treat a valid YAML document start marker with an inline comment (e.g. --- # doc 1) as a separator. YAML allows comments after ---, so this can cause multi-document manifests to be parsed as a single document when users include such comments. Consider updating the pattern to allow an optional comment, e.g. (?m)^---\\s*(?:#.*)?$ (still anchored to column 0).

Suggested change
// of a line (column 0), optionally followed by trailing whitespace. Per the
// YAML spec, an indented "---" inside a multi-line string value is NOT a
// document separator, so we must anchor to the beginning of the line.
var yamlDocSeparator = regexp.MustCompile(`(?m)^---\s*$`)
// of a line (column 0), optionally followed by trailing whitespace and an
// optional inline comment. Per the YAML spec, an indented "---" inside a
// multi-line string value is NOT a document separator, so we must anchor to
// the beginning of the line.
var yamlDocSeparator = regexp.MustCompile(`(?m)^---\s*(?:#.*)?$`)

Copilot uses AI. Check for mistakes.
input: "apiVersion: v1\nkind: Pod\n--- \napiVersion: v1\nkind: Service",
expected: []string{"apiVersion: v1\nkind: Pod\n", "\napiVersion: v1\nkind: Service"},
},
{
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

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

There isn’t a test case covering a valid separator line with an inline YAML comment (e.g. --- # doc separator). Adding a testcase for that ensures the regex continues to match YAML-compliant separators if the pattern is adjusted (and prevents regressions).

Suggested change
{
{
name: "separator with inline comment",
input: "apiVersion: v1\nkind: Pod\n--- # doc separator\napiVersion: v1\nkind: Service",
expected: []string{"apiVersion: v1\nkind: Pod\n", "\napiVersion: v1\nkind: Service"},
},
{

Copilot uses AI. Check for mistakes.
@misraved misraved merged commit 6f21e97 into main Mar 6, 2026
5 checks passed
@misraved misraved deleted the issue-341-fix-splitting-logic branch March 6, 2026 04:54
Copilot AI mentioned this pull request Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Yaml stream separator in a string breaks parsing manifests

4 participants