Skip to content

fix(format): keep an indented comment in a leading gap indented - #3169

Merged
robcohen merged 1 commit into
mainfrom
fix/leading-gap-indented-comment
Nov 30, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/leading-gap-indented-comment

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

rledger format keeps the indent of an indented comment in the gap before a directive, as bean-format does and as the formatter already did for one before the first directive (format.rs, the top-level walk: "An indented comment line here … keeps its indent"). P5's text says so.

Before / after

open A, a blank line, \t; plain, open B (review repro i13a):

output bean-check on the output
input \t; plain line 3: syntax error, unexpected INDENT
bean-format 3.2.3 \t; plain (unchanged) same
rledger before ; plain at column 0 clean: formatting removed an error
rledger after \t; plain (unchanged) same as the input

i13a–i13d (comment after a blank line, at the end of the file, after two blank lines, after a column-0 comment) now format byte-identically to bean-format.

How

emit_leading_comments, which writes the comments of the leading region of every directive after the first, ignored a WHITESPACE token; it now keeps the blanks that start a comment's line and writes them before the comment. Nothing else in the leading region changes (blank lines, trailing whitespace on a blank line, comments at column 0).

rledger still reads such a line as a comment, on purpose (P5): this changes only what the formatter writes.

Proof

  • format_roundtrip_properties::test_format_source_keeps_an_indented_comment_in_a_leading_gap_indented: the fixture formats to itself, and beancount's own errors over the text (the error oracle's reference, __beancount_reference_errors) are on lines 3 and 9 before and after formatting (line 5 sits inside beancount's recovery from line 3; bean-check 3.2.3 agrees).
  • New IDEMPOTENCE_MATRIX entry indented_comment_in_leading_gap, and format_compat fixture cases/indented_comment_in_leading_gap (input = expected = bean-format's output).
  • Formatter corpus: rledger format on 1,221 files (compat corpus, every tracked ledger, profiling workloads), release builds of origin/main 1f4b9dc and this branch: byte-identical output and exit status (none of them holds such a comment). scripts/format-idempotence.sh and scripts/format-preserves-comments.sh on the compat corpus: 676 checked, 0 non-idempotent, 0 diagnostics changed, 0 comment lines dropped.
  • Parser corpus (parse_error_lines --jsonl, semantic_snapshot, 45,182 inputs): byte-identical, as expected for a formatter-only change; so no parity run moves and no cache bump.
  • cargo test -p rustledger-parser -p rustledger-loader -p rustledger-lsp --all-features: 2,221 passed, 0 failed. Clippy -D warnings: clean.
  • CHANGELOG: Fixed.

Merge note

The line-start \r PR (#3162) also edits emit_leading_comments (a CR_BLANK read like whitespace there): a small textual conflict for whichever lands second.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Instruction counts and wasm fuel vs base (deterministic)

Workload base this PR Change vs v0.24.0
check/simple 289.7M 289.7M +0.01% ✅ -17.1%
check/investment 450.0M 450.0M +0.01% ✅ -12.8%
check/tagged 420.1M 420.1M -0.01% ✅ -18.5%
check/multicurrency 439.2M 439.2M +0.01% ✅ -24.8%
check/fifo 439.3M 439.4M +0.01% ✅ -16.8%
check/metadata 614.2M 614.1M -0.01% ✅ -23.1%
check-warm/simple 128.6M 128.6M -0.00% ✅ -18.4%
format/simple 613.2M 613.7M +0.07% ✅ +13.8%
format/metadata 1.52G 1.52G +0.14% ✅ +27.5%
format/errors 92.3M 92.3M -0.01% ✅ +103.7%
check/errors 269.4M 269.3M -0.03% ✅ —
check-json/errors 149.5M 149.4M -0.06% ✅ —
check/junkline 30.6M 30.6M -0.09% ✅ +300.4%
check-json/junkline 43.9M 43.8M -0.19% ✅ +614.3%
query/journal/simple 442.5M 442.4M -0.02% ✅ -29.0%
query/groupby/simple 338.8M 339.1M +0.07% ✅ -26.3%
query/groupby/investment 555.9M 556.5M +0.10% ✅ -25.3%
lsp/open-diagnostics/simple 336.9M 337.0M +0.01% ✅ —
lsp/open-diagnostics/metadata 901.0M 901.0M +0.00% ✅ —
lsp/format/simple 1.03G 1.03G +0.08% ✅ —
lsp/format/metadata 2.53G 2.53G +0.07% ✅ —
lsp/edit/simple 670.9M 670.9M -0.00% ✅ —
Workload (wasmtime fuel) base this PR Change vs v0.24.0
wasm/validate/simple 1.14G 1.14G +0.00% ✅ —
wasm/validate/metadata 2.79G 2.79G +0.00% ✅ —
wasm/format/simple 1.40G 1.40G +0.02% ✅ —
wasm/format/metadata 3.55G 3.55G +0.01% ✅ —
wasm/query/simple 1.28G 1.28G +0.00% ✅ —
ffi/load/simple 1.75G 1.75G +0.00% ✅ —
ffi/load/metadata 4.04G 4.04G +0.00% ✅ —
ffi/query/simple 1.38G 1.38G +0.00% ✅ —
ffi/validate/simple 1.22G 1.22G +0.00% ✅ —
ffi/format/simple 1.46G 1.47G +0.02% ✅ —

Instruction counts (cachegrind I refs) are deterministic: the same binary on the same input gives the same count to within ~0.02%, so unlike wall clock a 1% change is real. 🟡 ≥ 1%, 🔴 ≥ 5%. The wasm/ and ffi/ rows are wasmtime fuel (WebAssembly operators the guest executed), which is exact: the same .wasm on the same input gives the same fuel on any machine. Static data moves the component's heap, so a change to embedded strings alone can move ffi/ by a few hundredths of a percent. The last column is cumulative drift from the pinned v0.24.0 baseline (scripts/perf-baseline.json).

Wall clock (hyperfine)

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 28.5ms ± 1.1ms 529.3ms ± 5.1ms 18.6x 34.7 MB / 51.1 MB
investment 45.6ms ± 1.1ms 1874.1ms ± 24.4ms 41.1x 44.8 MB / 58.9 MB
fifo 46.3ms ± 0.8ms 8045.4ms ± 77.9ms 173.7x 43.1 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 27.7ms ± 1.0ms 28.5ms ± 1.1ms +2.8% ✅ within noise 34.0 MB → 34.7 MB (+2.2%)
investment 46.8ms ± 1.2ms 45.6ms ± 1.1ms -2.6% ✅ within noise 44.8 MB → 44.8 MB (-0.2%)
fifo 46.4ms ± 0.8ms 46.3ms ± 0.8ms -0.1% ✅ within noise 42.3 MB → 43.1 MB (+1.9%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 0.7ms, fifo 0.5ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 180 masked)
Full AST Match 100%
Total Files 828
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

`open A`, a blank line, `\t; plain`, `open B` formatted the comment to
column 0. beancount reads the indented line as `syntax error, unexpected
INDENT` and the column-0 one as an ordinary comment, so formatting
silently removed an error bean-check reports. The top-level walk keeps
such a comment's indent before the first directive (format.rs, the stated
policy); `emit_leading_comments`, which writes the comments in the gap
before every later directive, dropped it. It now keeps the blanks that
start a comment's line, as bean-format does.

Pinned by an IDEMPOTENCE_MATRIX entry, a format_compat fixture
(`indented_comment_in_leading_gap`, identical to bean-format's output),
and a test that beancount's own errors over the text (the reference
oracle) are the same before and after formatting. P5 says so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/leading-gap-indented-comment branch from 416e1ea to 5fb7c3c Compare November 30, 2026 20:12
@robcohen
robcohen merged commit 539c6c2 into main Nov 30, 2026
65 of 67 checks passed
@robcohen
robcohen deleted the fix/leading-gap-indented-comment branch November 30, 2026 20:32
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.

1 participant