Skip to content

Preserve all formatting on YAML --preserve set (no more flow-style collapse)#11

Merged
machado144 merged 1 commit into
mainfrom
fix/preserve-multiline-flow-style
Jun 29, 2026
Merged

Preserve all formatting on YAML --preserve set (no more flow-style collapse)#11
machado144 merged 1 commit into
mainfrom
fix/preserve-multiline-flow-style

Conversation

@machado144

Copy link
Copy Markdown
Contributor

Problem

pipekit <fmt> set --preserve promises a surgical edit — change only the targeted value, touch nothing else. That held for editing an existing single-line scalar, but inserting a new key (e.g. bumping a chart's image.tag when the key isn't there yet) fell through to the node re-encode fallback, which reformats the entire document:

  • Multi-line flow-style collections collapsed to one long line:
    args: [
      '--config',
      '/etc/app.yaml'
    ]
    became args: ['--config', '/etc/app.yaml'].
  • Blank lines got dropped.

The data was always correct (it re-parses to the same structure), but the diff was far noisier than the one intended change — exactly the thing --preserve exists to avoid.

Fix

Add a surgical insert path between the scalar-splice fast path and the re-encode fallback. It:

  • Anchors at the deepest existing block-style mapping along the path.
  • Splices in only the missing tail — a single leaf line, or a nested block when intermediate maps are missing too.
  • Mirrors the sibling key indentation; leaves trailing blank lines below the new entry.
  • Validates the result by re-parsing and confirming the value resolves at the requested path before accepting it.
  • Defers to the existing safe fallback for anything it can't do surgically (flow-style parents, non-scalar values, indexing into a scalar).

Net effect: everything outside the inserted line(s) stays byte-for-byte identical.

Before / after

Inserting .image.tag=v1.4.0 into a values file that has a multi-line args: [ ... ] and podAnnotations: { ... } elsewhere now produces a single-line diff (+ tag: v1.4.0); previously every multi-line flow block collapsed.

Tests

New byte-for-byte coverage:

  • multi-line flow style ([...] and {...}) preserved on insert
  • blank lines / comment alignment around the insertion point
  • top-level key insert, nested-map creation, deep nested creation
  • ambiguous-string quoting on insert (enabled: "yes")
  • no-trailing-newline insert
  • insert-under-a-scalar surfaces an error (defers to fallback)

All 275 tests pass; go vet clean.

🤖 Generated with Claude Code

The --preserve set fast path only handled editing an existing single-line
scalar. New keys (and missing intermediate maps) fell through to the node
re-encode path, which reformats the WHOLE document: multi-line flow-style
collections (args: [ ... ] / annotations: { ... } spanning lines) collapsed
to one long line and blank lines were dropped. The targeted value was correct,
but the diff was far noisier than the single intended change.

Add a surgical insert path that anchors at the deepest existing block mapping
along the path and splices in just the missing tail (a leaf key, or a nested
block when intermediate maps are absent too). Everything outside the inserted
line(s) stays byte-for-byte identical, and the result is still validated by
re-parsing before it is accepted; anything it can't do surgically (flow-style
parents, non-scalar values, indexing into a scalar) defers to the safe
fallback as before.

Add byte-for-byte tests covering multi-line flow preservation on insert,
blank lines, top-level and nested-map creation, ambiguous-string quoting,
no-trailing-newline, and the insert-under-scalar error.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@machado144 machado144 merged commit 4e81da1 into main Jun 29, 2026
1 check passed
@machado144 machado144 deleted the fix/preserve-multiline-flow-style branch June 29, 2026 14:18
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.

2 participants