From 9c597ef592901bde62abd53351979db4195c7d05 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 9 Apr 2026 13:06:18 +0000 Subject: [PATCH] refactor: improve document splitting in writeRenderedManifests - Use "\n---\n" separator instead of "\n---" to be more explicit - Strip the leading "---\n" Helm marker before splitting so the first document chunk doesn't include an empty pre-separator string - Use bytes.Buffer for accumulating unsourced documents Agent-Logs-Url: https://github.com/helmfile/helmfile/sessions/38cec14e-e49f-4e90-918e-d400de52bd8e Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --- pkg/helmexec/exec.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/helmexec/exec.go b/pkg/helmexec/exec.go index 5e4fe29a..67b58a2f 100644 --- a/pkg/helmexec/exec.go +++ b/pkg/helmexec/exec.go @@ -719,9 +719,14 @@ func writeRenderedManifests(outputDir string, content []byte) error { return nil } - // Split on the YAML document separator. Helm emits "---\n" between - // documents; trim leading/trailing whitespace from each chunk. - rawDocs := bytes.Split(content, []byte("\n---")) + // Helm emits a leading "---\n" before the first document and uses "\n---\n" + // as the separator between subsequent documents. Strip the leading marker + // first so that the initial chunk after splitting is a bare document body, + // not an empty string followed by the first document. + content = bytes.TrimPrefix(content, []byte("---\n")) + + // Split on the YAML document separator. + rawDocs := bytes.Split(content, []byte("\n---\n")) // fileContents maps relative path → accumulated YAML content. fileContents := make(map[string][]byte) @@ -771,17 +776,17 @@ func writeRenderedManifests(outputDir string, content []byte) error { // Write unsourced documents (e.g. injected by a post-renderer) to a // single fallback file. if len(unsourced) > 0 { - var combined []byte + var combined bytes.Buffer for _, doc := range unsourced { - combined = append(combined, []byte("---\n")...) - combined = append(combined, doc...) - combined = append(combined, '\n') + combined.WriteString("---\n") + combined.Write(doc) + combined.WriteByte('\n') } fallback := filepath.Join(outputDir, "manifest.yaml") if err := os.MkdirAll(outputDir, 0755); err != nil { return fmt.Errorf("failed to create output directory %s: %w", outputDir, err) } - if err := os.WriteFile(fallback, combined, 0644); err != nil { + if err := os.WriteFile(fallback, combined.Bytes(), 0644); err != nil { return fmt.Errorf("failed to write %s: %w", fallback, err) } }