Incremental QMD writer drops bytes outside the first-to-last block range

Open
#963 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start with crates/pampa/src/writers/incremental.rs, especially assemble and emit_metadata_prefix, then trace how block offsets are obtained through SourceInfo::preimage_in. Reproduce both cases with qmd_to_pandoc, compute_reconciliation(&original, &original), and incremental_write. Done means unchanged input, including front matter and trailing blank lines, is reproduced byte for byte.

Written by the indexing model from the issue text.

Description

Reproduction

At tag v0.37.0 (and on main at a37801fe4), parse with pampa::wasm_entry_points::qmd_to_pandoc, compute quarto_ast_reconcile::compute_reconciliation(&original, &original) (no edit), and call pampa::writers::incremental::incremental_write.

Input A (33 bytes, front matter only, no body blocks):

---
title: Only front matter
---

Output: the empty string. The front matter is lost.

Input B (36 bytes):

First paragraph.

Last paragraph.


Output: 34 bytes; the two trailing blank lines after Last paragraph. are dropped. Editing any block in the document drops the trailing blank lines the same way — the bytes are not anchored to a block that survives editing.

Observed vs expected

Observed: with no edits, the output differs from the input — it is missing either the whole document (when it has no blocks) or the bytes after the last block (when it has one or more).

Expected: with no edits, the output equals the input exactly. Bytes before the first block (when front matter is unchanged) and bytes after the last block are copied verbatim from the original, the same way the gap between two kept blocks already is.

Cause

In crates/pampa/src/writers/incremental.rs:

  • assemble walks the coarsened block list and emits a metadata prefix, then each block with its separator, but never emits anything for the region from the end of the last block to the end of the input. There is no "epilogue" step mirroring the "prefix" step.
  • emit_metadata_prefix computes first_block_start as None when original_ast.blocks is empty, which means "no metadata prefix region" — so a document that is entirely front matter (zero body blocks) never has its front matter copied or rewritten at all.

Suggested fix

In assemble, after the main block loop, append original_qmd[last_block_end..] where last_block_end is the original AST's last block's end offset (via SourceInfo::preimage_in), when that block list is non-empty. In emit_metadata_prefix, treat a blocks-empty document's "first block start" as original_qmd.len() instead of None, so the whole document is considered the candidate metadata-prefix region instead of being skipped outright.

Dominant language
Rust
Stars
302
Forks
22
Avg merge
5h 2m
Merged PRs (30d)
213

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quarto-dev/q2

All issues in quarto-dev/q2

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.