Skip to content

fix(lsp)!: publish plugin and load errors where rledger check reports them, and validate a scratch buffer alone - #2969

Merged
robcohen merged 2 commits into
mainfrom
fix/lsp-diagnostics-2955-2957-2959
Nov 13, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/lsp-diagnostics-2955-2957-2959

Conversation

@robcohen

@robcohen robcohen commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

What

Three LSP diagnostics bugs. All three are in the same diagnostics-publishing code, so they are fixed in one PR.

#2955: a scratch buffer outside the ledger showed the whole ledger's errors

Cause. In MainLoopState::publish_diagnostics, a buffer that is not a ledger file took the None arm and called all_diagnostics with the loaded ledger, current_file_id = None, and the other open buffers as overlays. That validated the ledger plus the other buffers but left out the scratch file's own directives. Then, because there was no file id, it mapped every ledger error onto the scratch file's text by byte offset, and merged the scratch file's plugin lines in place of file 0's. With no ledger buffer open the overlay was empty and the scratch file got validated alone. So what it showed depended on what else was open. The single-file branch had the same problem when a single-file ledger was loaded.

Fix. A buffer outside the ledger is validated as a single-file ledger of its own text alone, no matter what else is open. Its own includes are not followed, so this is what rledger check scratch.beancount reports only when the scratch file includes nothing (check loads what it includes). all_diagnostics now ignores ledger_state and the overlays when there is no current_file_id, and the None arm in publish_diagnostics passes neither.

Why this choice. The ledger's own files still get their diagnostics on every pass: the unopened includes are still cross-published from the scratch buffer's pass, and open ledger buffers publish themselves. So the only question is what the scratch buffer itself shows. The scratch file is not part of the ledger, and check on the ledger never reads it, so none of the ledger's findings belong to it. Its own text is the only thing it can be judged by. The other options were worse:

  • Showing nothing would hide real errors in it.
  • Validating it "against" the ledger would make up a ledger that does not exist on disk.

Its option diagnostics already followed this rule (#2925).

#2957: plugin errors were published at line 0 of the main file

Cause. LedgerValidation::plugin_errors kept only (severity, code, message), which dropped the LedgerError's location. map_validation_to_diagnostics then showed every plugin error only in file 0, at (0, 0).

Fix. Plugin errors keep the whole LedgerError together with the file id its location names, found by the same path lookup option diagnostics use. Each error is published in that file, on the line check reports (the location plugin_error_to_ledger_error gives it). Errors in an included file are cross-published to that file when it is not open. An error with no location, or a placeholder one such as <auto_accounts>, stays at the top of the main file. Each error lands in exactly one file.

#2959: load errors were not published

Cause. LedgerState::load dropped the loader's errors, so a missing or unreadable include, an include cycle, a duplicate include, or an empty glob never reached publish_diagnostics. Separately, the loader gave an include cycle no location at all: LoadError::at_include only attached the site to Io.

Fix.

  • Loader (breaking). LoadError::IncludeCycle gains a public include_site field, and at_include / include_site() now cover it too. LoadError is not #[non_exhaustive], so an exhaustive pattern or struct literal on the variant needs the field; hence the ! in the title and the ### Breaking CHANGELOG entry, as in fix(loader)!: validate options in included files as beancount does, and locate every option diagnostic #2945. A cycle therefore carries the include that closed it, in the file that holds that include. process now locates a load error's include site with SourceMap::locate. rledger check --format json puts E0002 on that line (also via SourceMap::locate), matching the Io arm, instead of line 1 of the first file of the cycle. WASM's include_site() consumer and the FFI component (its error's filename / line / column, from the LOAD error's location) pick it up without any other change.
  • LSP. LedgerState::load takes the parse errors out (take_parse_errors), because the LSP already reports those per line from a fresh parse. What remains in ledger.errors with code LOAD is exactly the set of load errors, and the new LedgerState::load_errors() exposes it. Both all_diagnostics (single-file ledger) and ledger_diagnostics_multi publish them through one shared helper, ledger_errors_in_file, which also places option diagnostics by the same rule:
    • a missing or unreadable include, and an include cycle, appear on the include line in the including file;
    • errors with no location appear at the top of the main file.
      The message is the loader's LOAD message, the same one WASM validateMultiFile / Ledger.fromFiles report.
  • Clearing. While the ledger has a load error, any watched .beancount change reloads it. The file that fixes a missing include is not part of the ledger yet, so without this, creating it would leave the error published. Cross-file publishing already clears an unopened file whose errors are gone.

The helpers formerly named option_error_* were renamed ledger_error_* because they now serve option, plugin and load errors.

Cache versions

Known limitations (unchanged in kind)

  • Lines can go stale in an edited buffer. Plugin and load error lines come from the loaded ledger, so an unsaved edit that moves lines shifts them until the next reload. Option diagnostics already behave this way. A code comment at the plugin-error placement in handlers/diagnostics.rs records this.
  • A scratch buffer's includes are not followed. See LSP: a scratch buffer outside the ledger shows the whole ledger's errors #2955 above.
  • No line for some load errors. A duplicate include, an empty glob and a path-traversal refusal still carry no include site in the loader, so they appear at the top of the main file.
  • Messages differ by surface. rledger check's text output words an unreadable include as failed to read <path>: .... The loader's LOAD message, which the LSP and WASM use, says failed to read file <path>: ....

Tests

LSP-level tests drive MainLoopState (on_did_open / didChange / watched-file events) and compare what is published on the wire (the publishDiagnostics notifications, by URI, so the same on every platform) with the loader's rledger check errors on the same files. All four fail on main and pass with the fix:

Loader and CLI tests:

  • test_include_cycle_is_located_at_the_include_that_closes_it (loader): the cycle's site and the LOAD error's location.
  • json_locates_an_include_cycle_at_its_include (CLI): check --format json places E0002 on that include.

Gates

  • cargo fmt --all --check
  • cargo clippy --workspace --all-features --all-targets -- -D warnings
  • RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --all-features --no-deps
  • cargo test -p rustledger-lsp --all-features
  • cargo test -p rustledger-loader --all-features
  • cargo test -p rustledger --all-features
  • cargo test -p rustledger-wasm --all-features

Closes #2955
Closes #2957
Closes #2959

🤖 Generated with Claude Code

…them, and validate a scratch buffer alone

- #2955: a buffer outside the loaded ledger is validated as a ledger of its
  own text, as `rledger check` on it does, whatever else is open. It used to
  get the ledger's errors mapped by byte offset onto its lines, and its
  `plugin` lines replaced the main file's.
- #2957: plugin errors keep their location and are published on the file
  and line `check` reports (cross-published to an unopened include); only
  one with no location stays at the top of the main file.
- #2959: load errors (`LOAD`) are published: a missing or unreadable
  include and an include cycle on the `include` line in the including file,
  the rest in the main file. `LoadError::IncludeCycle` gains `include_site`
  so the loader, `check --format json` and WASM locate a cycle on that line
  too. While the ledger has a load error, any watched change reloads it, so
  creating a missing include clears the error. WASM cache v107 (v106 held
  by #2931); loader cache unchanged at v73.

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

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.7ms ± 0.8ms 737.8ms ± 8.1ms 14.0x 44.9 MB / 51.0 MB
investment 81.9ms ± 1.3ms 2781.8ms ± 38.6ms 34.0x 46.9 MB / 59.1 MB
fifo 78.1ms ± 1.4ms 15382.7ms ± 241.1ms 196.8x 48.9 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 52.3ms ± 0.7ms 52.7ms ± 0.8ms +0.7% ✅ within noise 45.1 MB → 44.9 MB (-0.5%)
investment 82.1ms ± 1.5ms 81.9ms ± 1.3ms -0.2% ✅ within noise 47.1 MB → 46.9 MB (-0.4%)
fifo 77.2ms ± 1.3ms 78.1ms ± 1.4ms +1.2% ✅ within noise 49.1 MB → 48.9 MB (-0.5%)

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

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.42029% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/check.rs 95.23% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

robcohen added a commit that referenced this pull request Nov 13, 2026
v107 is claimed by #2969; v106 is held by #2931.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 826
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

…cle field as breaking

- The new main_loop tests read diagnostics off the published
  notifications by URI instead of MainLoopState::diagnostics, whose keys
  differ on Windows (an open buffer's URI path `C:\..` against the
  loader's canonical `\\?\C:\..` for an unopened file).
- CHANGELOG: LoadError::IncludeCycle's new public include_site field
  moves under Breaking, with `rledger check --format json` placing E0002
  on the include line, and the FFI component and WASM locating the cycle.
- A buffer outside the ledger is validated on its own text without its
  includes; docs and CHANGELOG now say so instead of claiming it matches
  `rledger check` on that file in general.
- Comment on why plugin and load error lines drift in an edited buffer
  until the next reload.
- check.rs locates the JSON cycle diagnostic with SourceMap::locate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen robcohen changed the title fix(lsp): publish plugin and load errors where rledger check reports them, and validate a scratch buffer alone fix(lsp)!: publish plugin and load errors where rledger check reports them, and validate a scratch buffer alone Nov 13, 2026
@robcohen
robcohen merged commit 2124386 into main Nov 13, 2026
68 of 69 checks passed
@robcohen
robcohen deleted the fix/lsp-diagnostics-2955-2957-2959 branch November 13, 2026 05:03
robcohen added a commit that referenced this pull request Nov 13, 2026
… beside a same-cost short (#2970)

* fix(booking): read an AVERAGE sale's pool-average cost as a reduction beside a same-cost short

On an AVERAGE account holding a short at 101 beside longs averaging 101, a
sale `-1 X {}` reduced the longs and booked as `-1 X {101 USD}`. Asked again
of the inventory (any consumer realizing a posting with no recorded booking
decision), the #2384 own-side exception read that cost lot by lot: it matched
the short and neither long, so the booked reduction became a second short.

Under AVERAGE a cost is a claim about the pool, so the exception now also
treats the other side as named when its pool accepts the cost as a claim.
The claim rule moves from rustledger-booking's check_average_spec into
rustledger-core (average_claim_mismatch, with written_allowance and its
helpers), and both booking and Inventory::is_booking_reduction call it.
A written `-1 X {101 USD}` in that shape now books like `-1 X {}`.

Commits the proptest seed and adds regression tests at core and validate
level. WASM cache bumped to 107 (106 is held by #2931); loader cache
unchanged.

Closes #2958

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

* chore(wasm): move the #2958 cache bump to v108

v107 is claimed by #2969; v106 is held by #2931.

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

* docs(booking): name covers in the #2958 rule, and state its two consequences

The AVERAGE pool-claim rule applies to a buy that covers shorts as well as
to a sale. Say "a sale or a cover" in compatibility.md section 14 and the
v108 WASM cache note, and pin the cover with a test: holding a long at 101
beside shorts at 100 and 102, +1 X {101 USD} covers the shorts (1 long,
9 short left), where main opened a second long (2 long, 10 short).

The ADR 0007 amendment now states two consequences: a claim the pool
accepts only by rounding beats a lot at exactly that cost, and a ledger
that loaded can now fail with E4002 on an over-reduction of the pool.

Also update the adds_to_its_own_side doc comment to describe the AVERAGE
pool test.

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

* docs(changelog): note the AVERAGE reduction fix (#2958)

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 13, 2026
WASM cache: main holds v107 (#2969), #2970 holds 108 and #2971 holds
109, so this PR moves from 106 to 110. The v107 note now says v106 was
held here and never released; loader cache stays 73.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant