Issue #1518: yaml: a round trip that keeps comments and layout — which edit model?

Open and answer this issue on GitHub.

decision, yaml · opened 2026-11-09 · updated 2026-11-09 · 1 comment

Part of #928. Builds on the writer (#1515).

Where the question comes from

The owner wants full round trips: read a YAML file, change a value, write it back, and keep the comments, key order, indentation, quoting and blank lines. Today the parser drops comments (rule Comment = "#" until("\n" | eof) -> { } in packages/yaml/src/main.nika) and no node keeps its span or style. ADR-yaml-1 D10 lists “format-preserving editing (comments, order, indentation kept)” as a later stage that needs “the tree carrying comments and every token’s span; an ADR of its own for the edit model”. This is that ADR’s question. It is not obvious: there are several consistent designs that differ in cost, in API and in what “kept” means.

Alternatives

0. Do nothing. The package reads and (after #1515) writes canonically, the way serde_yaml does.

1. Lossless syntax tree (rust-analyzer’s rowan, Swift’s libSyntax): every token, including whitespace and comments, is a node; the typed Node tree is a view over it.

2. Comments as trivia on nodes, re-rendered (ruamel.yaml’s round-trip mode, Go yaml.v3 HeadComment/LineComment/FootComment): nodes carry their comments and style; the writer renders the whole tree.

3. Spans and decor, copy what is untouched (toml_edit, Cargo’s way of editing Cargo.toml): every node keeps its source span, its style and the comments/whitespace around it (“decor”). The writer copies an unchanged node’s bytes from the source and renders only changed or new nodes, in the style and indentation of their neighbours.

Other languages

Recommendation

3, spans and decor. It is the only one of the cheaper designs that gives byte identity, and byte identity is what “full round trip” means to a user diffing a file under version control. 1 gives the same guarantee at a much higher price in memory and API.

If decided so, the build:

  1. Node gains span and decor (leading comments/blank lines, trailing comment), the parser fills them (grammar rules return the comments instead of discarding them).
  2. yaml::write_preserving(doc) copies unchanged spans and renders changed nodes with #1515’s emitter in the neighbours’ style.
  3. Done when: every yaml-test-suite input parses and writes back byte-identical, and a set of edit tests (change a scalar, add a key, remove an item, under comments) diff only in the edited lines.
  4. The facade gets no new surface (serde_yaml has none for this); the Nikaia API is the showcase.

Comments

keywan-ghadami, 2026-11-09

Additions to alternative 3 (from an outside review and the owner)

These sharpen the recommendation; they do not change it.

1. One source, spans are numbers. The edit document owns exactly one Bytes, the source. Every node keeps its span as two byte offsets into it; write_preserving copies untouched ranges straight from that source to the output. Scalar text stays a view into the source as today (Part III, bulk text is a tethered slice): while the document lives, the source is held anyway, so a view costs nothing extra. A value taken out of the document and kept after it is the language’s ordinary case: the tether keeps the whole buffer, the compiler warns and suggests .clone() (Part III, Retention, ADR-283 D6). No separate “detach from source” operation: .clone() is that operation, until a measurement shows otherwise.

2. The parser takes Bytes. Then fs::map (a Mapped is a Bytes) and fs::read both go in without a copy. No new memory abstraction.

3. Writing back never overwrites the source in place. A document read from fs::map(p) and written to p would overwrite the pages it is still copying from. write_preserving to a path writes a new file beside it and renames it over the old one; a test covers map-then-write-to-same-path.

4. Only who writes back pays: two entry points, one grammar.

Measurement before the build fixes the tree shape

Per docs/measuring.md (callgrind instruction counts, never time; against tuned Rust):

  1. Measurement above; result and chosen shape in the ADR.
  2. Grammar parameter for decor; Node (or the edit node) gains span and decor per the measurement; the parser takes Bytes.
  3. The edit entry point and yaml::write_preserving: copies untouched spans, renders changed nodes with #1515’s emitter in the neighbours’ style, writes to a new file and renames.
  4. Done when: every yaml-test-suite input parses and writes back byte-identical through the edit entry; edit tests (change a scalar, add a key, remove an item, under comments) differ only in the edited lines; the map-then-write-same-path test passes; yaml::parse instruction counts in benches/yaml are unchanged within the measured rule.

Generated by Claude Code

Back to Open Issues