Skip to content

fix(format): keep #! and #+ lines that follow a directive - #2792

Merged
robcohen merged 2 commits into
mainfrom
fix/format-keeps-hash-bang-lines
Oct 28, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/format-keeps-hash-bang-lines

Conversation

@robcohen

Copy link
Copy Markdown
Member

What changed

rledger format (and format_source / try_format_source) deleted #! and #+ lines whenever they followed a directive. The formatter knew only two of the lexer's four line-comment kinds (COMMENT for ; and PERCENT_COMMENT for %), so a SHEBANG or EMACS_DIRECTIVE token was dropped in these places:

  • as a directive's leading trivia, where it also cut off the comments after it, so a ; comment on the next line was lost too;
  • as a same-line trailing comment;
  • inside a transaction body, both before the first posting and between postings.

At the top of the file a different path already kept them, which is why the bug went unnoticed.

All six comment checks in cst/format.rs now call the canonical convert::is_comment_kind, in line with the canonical-function discipline in AGENTS.md. bean-format keeps these lines.

How to test

cargo test -p rustledger-parser --test format_keeps_hash_bang_lines runs 5 tests. Each checks that formatting leaves the file byte-identical and that formatting twice gives the same result. With the format.rs change reverted, 4 of the 5 fail; the top-of-file case passes either way and is there to pin it.

Parser, CLI and LSP test suites, clippy -D warnings, rustdoc -Dwarnings and fmt --check all pass locally. Parse output is unchanged, so no cache-version bump is needed.

Closes #2776

🤖 Generated with Claude Code

The formatter recognized only two of the lexer's four line-comment kinds
(COMMENT and PERCENT_COMMENT). A SHEBANG (#!) or EMACS_DIRECTIVE (#+) line
was dropped as a directive's leading trivia (cutting off the comments after
it too), as a same-line trailing comment, and inside a transaction body.
Every comment emitter now calls the canonical convert::is_comment_kind.

Closes #2776

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner October 28, 2026 22:40
Covers the posting-body and directive-metadata emitters, which no test
exercised, and fixes the emit_leading_comments doc and CHANGELOG wording.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Oct 28, 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

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 45.0ms ± 1.4ms 797.9ms ± 2.0ms 17.7x 34.6 MB / 51.2 MB
investment 68.6ms ± 0.5ms 2908.1ms ± 11.1ms 42.4x 44.6 MB / 59.2 MB
fifo 66.7ms ± 0.5ms 14564.3ms ± 112.0ms 218.5x 40.6 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.8ms ± 0.3ms 45.0ms ± 1.4ms +0.6% ✅ within noise 34.5 MB → 34.6 MB (+0.4%)
investment 68.5ms ± 0.4ms 68.6ms ± 0.5ms +0.2% ✅ within noise 44.5 MB → 44.6 MB (+0.1%)
fifo 66.9ms ± 0.7ms 66.7ms ± 0.5ms -0.3% ✅ within noise 40.6 MB → 40.6 MB (+0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.6ms, investment 0.3ms, 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

@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 821
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 eb4ecbd into main Oct 28, 2026
60 of 61 checks passed
@robcohen
robcohen deleted the fix/format-keeps-hash-bang-lines branch October 28, 2026 23:16
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.

fix(format): rledger format deletes #! and #+ lines that follow a directive

1 participant