Skip to content

fix(format): keep a trailing comment outside a multi-line string - #3048

Merged
robcohen merged 1 commit into
mainfrom
fix/format-comment-multiline-string
Nov 26, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/format-comment-multiline-string

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

rledger format moved a same-line trailing ; comment into a multi-line string when the string's closing line held the comment, silently changing the string's value (#3046). This keeps the comment after the closing quote, as bean-format 3.2.3 does.

Cause

emit_directive in crates/rustledger-parser/src/cst/format.rs spliced the trailing comment before the first \n of the emitted directive. With a multi-line string in the header, that \n is inside the string. emit_posting had the same splice and the same bug with a multi-line cost-spec label ({10 EUR, "lab\nel"}).

Fix

New first_line_end_outside_strings finds the first \n outside a string literal using the existing lexer-backed StringLiterals (the same oracle the CRLF helpers use), and both splices use it.

Shapes fixed (all verified against bean-format 3.2.3, apart from rledger's existing single-space comment canonicalization): transaction narration, payee, narration followed by tags/links, query name/string, custom name/value, note, event type/description, document, option, plugin config, include, and a posting's cost label. Metadata values (directive, transaction, posting) were already correct, and fixtures now pin them.

The LSP's document formatting and range formatting share the emitter, so they are fixed too. WASM format shares it as well; formatter output is not in any cached payload, so CACHE_VERSION is unchanged.

Tests

  • format_compat fixtures multiline_string_trailing_comment_{narration,payee,query,custom,note_event_document,option_plugin_include,metadata,cost_label}. The narration fixture is added to REQUIRED_FIXTURES.
  • New drift guard tests/format_preserves_directives.rs: formatting must leave the parsed directives, options, includes and plugins unchanged and must be idempotent. It runs over every clean format_compat input, a fixed sweep of every string slot × comment variants × multi-line bodies, and a proptest of random bodies ([a-z ;#\n]). With the fix reverted it fails on every affected shape. scripts/format-preserves-comments.sh could not catch this because the comment text survived inside the string.
  • LSP: test_handle_formatting_trailing_comment_after_multiline_string (LF and CRLF) and test_handle_range_formatting_trailing_comment_after_multiline_string. Both fail without the fix.

How to test

cargo test -p rustledger-parser --test format_compat --test format_preserves_directives
cargo test -p rustledger-lsp --all-features --lib multiline_string

Closes #3046

🤖 Generated with Claude Code

The formatter spliced a directive's same-line trailing comment before the
first `\n` of the emitted directive. When the header holds a multi-line
string (narration, payee, query, custom, note, event, document, option,
plugin, include), that `\n` is inside the string, so the comment became
part of the string's value. The posting splice had the same bug with a
multi-line cost label.

Both splices now find the first `\n` outside a string literal, read by
the lexer through `StringLiterals`. The LSP's document and range
formatting share the emitter and are fixed with it.

Adds `format_compat` fixtures for every shape, LSP tests, and a drift
guard (`format_preserves_directives`) asserting that formatting never
changes the parsed directives, options, includes or plugins.

Closes #3046

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 26, 2026 15:05
@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 53.0ms ± 2.7ms 800.5ms ± 7.8ms 15.1x 43.1 MB / 51.2 MB
investment 77.7ms ± 0.8ms 2873.2ms ± 7.0ms 37.0x 46.4 MB / 59.1 MB
fifo 74.8ms ± 0.5ms 14480.3ms ± 122.7ms 193.7x 48.5 MB / 58.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.5ms ± 0.5ms 53.0ms ± 2.7ms +3.0% ✅ within noise 43.3 MB → 43.1 MB (-0.5%)
investment 77.7ms ± 0.9ms 77.7ms ± 0.8ms -0.0% ✅ within noise 47.4 MB → 46.4 MB (-1.9%)
fifo 74.9ms ± 0.7ms 74.8ms ± 0.5ms -0.1% ✅ within noise 49.1 MB → 48.5 MB (-1.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.2ms, investment 0.5ms, fifo 0.4ms), 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 26, 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

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 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

@robcohen
robcohen merged commit 991c0e2 into main Nov 26, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/format-comment-multiline-string branch November 26, 2026 16:09
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.

[Bug]: rledger format moves a trailing comment into a multi-line string

1 participant