Fix YAML document splitting to only split on unindented --- separators#349
Conversation
#341) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
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(..., "---")withyamlDocSeparator.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.
| // 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*$`) |
There was a problem hiding this comment.
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).
| // 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*(?:#.*)?$`) |
| input: "apiVersion: v1\nkind: Pod\n--- \napiVersion: v1\nkind: Service", | ||
| expected: []string{"apiVersion: v1\nkind: Pod\n", "\napiVersion: v1\nkind: Service"}, | ||
| }, | ||
| { |
There was a problem hiding this comment.
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).
| { | |
| { | |
| 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"}, | |
| }, | |
| { |
Summary
---) inside a multi-line string value was incorrectly treated as a document boundary, breaking manifest parsingstrings.Split(content, "---")with a regex split(?m)^---\s*$that only matches---at column 0 (per YAML spec), across all 4 split locations inutils.goandhelm_template_render.goTest plan
go build ./...passesgo vet ./...passesgo test ./kubernetes/ -run TestYamlDocSeparator— all 14 tests pass---inside a string value (e.g. the Datadog ConfigMap from the issue)🤖 Generated with Claude Code