Skip to content

fix(lsp): publish diagnostics for unopened included files - #1487

Merged
robcohen merged 2 commits into
mainfrom
fix/lsp-included-file-diagnostics
Jun 21, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/lsp-included-file-diagnostics

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

LSP: a validation error living in an included file that the user hasn't opened was completely invisible. Found by LSP dogfounding (round 3).

Example — open main.beancount which does include "bad.beancount", where bad.beancount has an unbalanced transaction. bean-check reports the imbalance; the LSP reported nothing (no diagnostic on main, and none on bad either).

Two causes:

  1. validation_errors_to_diagnostics filters errors to the open file's id, dropping included-file errors.
  2. publish_diagnostics only ever runs for an open buffer, so a file with no open buffer never receives a publishDiagnostics — and the LSP spec has no implicit "clear".

How

When a ledger is loaded, after publishing the open buffer's diagnostics, also compute and publish diagnostics for every ledger file (reachable via include) that is not open in any buffer:

  • compute_unopened_ledger_diagnostics validates each unopened file against the full ledger by reusing the existing, tested all_diagnostics — filtered to that file's id and mapped against that file's own source (so ranges are correct), with the open buffers' fresh parses overlaid so unsaved edits are reflected.
  • publish_cross_file_diagnostics publishes non-empty sets and tracks their URIs in cross_file_diag_uris, so when the errors are fixed an explicit empty set is sent to clear them (these files have no on_did_close to do it).

Open files are skipped (they self-publish via didOpen/didChange). The work only runs for multi-file ledgers, and is O(unopened ledger files) validations — real-world ledgers have few files. (A single-pass distribution would remove the per-file re-validation; noted as a possible future optimization.)

Tests

included_file_validation_errors_are_published (integration, via the LSP harness): a journal includes good + bad; opening only good, the client receives a publishDiagnostics for bad.beancount's URI carrying the E3001 (unbalanced) error. Full rustledger-lsp suite passes (249 + integration); clippy + fmt clean.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner June 21, 2026 16:56
Copilot AI review requested due to automatic review settings June 21, 2026 16:56
@codecov

codecov Bot commented Jun 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.50549% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-lsp/src/main_loop.rs 94.50% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.8ms ± 0.6ms 24 MB
beancount 878.1ms ± 11.9ms 50.4 MB
Comparison 26.8x faster 2.1x less

vs main branch (2026-06-21)

Metric main this PR Change
Time 37.7ms 32.8ms -13.0% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates rustledger-lsp so validation errors originating in included ledger files are surfaced even when those files have no open editor buffer, by computing and publishing diagnostics for unopened included files after validating an open document.

Changes:

  • Adds tracking and publishing/clearing of diagnostics for unopened included files (cross_file_diag_uris + cross-file publish flow).
  • Computes diagnostics per unopened ledger file by validating against the full ledger with open-buffer overlays applied.
  • Adds an integration regression test ensuring publishDiagnostics is emitted for an unopened included file with an E3001 (unbalanced) error.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
crates/rustledger-lsp/src/main_loop.rs Compute/publish diagnostics for unopened included files and track URIs for explicit clearing.
crates/rustledger-lsp/tests/lsp_protocol.rs Adds an integration regression test asserting diagnostics are published for an unopened included file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-lsp/src/main_loop.rs Resolved
Comment thread crates/rustledger-lsp/src/main_loop.rs Resolved
Comment thread crates/rustledger-lsp/src/main_loop.rs Resolved
Validation errors in an included file the user had not opened were invisible:
they were filtered to the open file id, and publish_diagnostics only ran for
open buffers. Publish diagnostics for every ledger file not open in a buffer,
validated against the full ledger and mapped to its own source, tracking URIs so
fixed errors are cleared. Found via LSP dogfooding.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen force-pushed the fix/lsp-included-file-diagnostics branch from bb06383 to 95a678e Compare June 21, 2026 17:04
- Skip the current buffer overlay when its fresh parse has errors (avoid
  validating other files against a partial overlay).
- Robustly clear stale cross-file diagnostics: any previously-published URL no
  longer erroring and not open is cleared, covering fixed errors, include-graph
  changes, and the ledger becoming single-file/unavailable.
- Warn (instead of silently dropping) when a file path is not a valid file://
  URI. Added included_file_diagnostics_are_cleared_when_fixed regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen merged commit c3e8454 into main Jun 21, 2026
58 checks passed
@robcohen
robcohen deleted the fix/lsp-included-file-diagnostics branch June 21, 2026 17:22
robcohen added a commit that referenced this pull request Jul 19, 2026
…#1813)

* fix(lsp): validate multi-file ledgers once, not once per file (#1799)

The cross-file diagnostics added in #1487 (c3e8454) called all_diagnostics
once per ledger file, and each call re-ran the WHOLE ledger's booking +
plugin passes + Early/Late validation just to keep one file's errors —
O(files) full validations on every keystroke. Because didChange is
handled synchronously on the single LSP main-loop thread, that work
blocked completion requests (dispatched only after the notification
returns), so completions timed out on large multi-file ledgers
(the reporter's 4-file / ~50k-line ledger). Worse without --release,
worse with more currencies — both just mean more directives per pass.

Split the file-independent, expensive half of validation into
run_ledger_validation (sort + book + plugins + Early/Late), computed
ONCE, and map each file's diagnostics from the shared result by
filtering errors on file_id. ledger_diagnostics_multi produces the
current file's AND every unopened file's diagnostics from that single
validation; publish_diagnostics routes multi-file ledgers through it and
leaves the single-file path unchanged. Unopened files are also no longer
re-parsed via a redundant per-file all_diagnostics.

Regression test ledger_diagnostics_multi_validates_once_for_all_files
asserts exactly one whole-ledger validation regardless of file count
(thread-local counter so parallel tests don't interfere). The #1487
behavior tests (included_file_validation_errors_are_published,
included_file_diagnostics_are_cleared_when_fixed) still pass.

Closes #1799.

* perf(lsp): skip the whole-ledger validation when no target uses it

Copilot review: ledger_diagnostics_multi ran the whole-ledger
book+plugin+validate even when every target failed validation_would_run
(all parse errors, or all over the size cap), discarding the result and
still blocking the main loop. Gate the validation (and the expensive
directive-overlay build) behind `any target validates`; skip to an empty
LedgerValidation otherwise. Regression test extended to assert ZERO
validations when all targets have parse errors.

* fix(lsp): address deep-review findings on the single-pass refactor

Two real regressions the review caught in the first cut:

1. Editing a buffer NOT part of the multi-file ledger (a scratch file,
   current_file_id == None) fell to the else branch, which published an
   EMPTY cross-file set and thereby CLEARED genuine diagnostics in
   unopened included files. Now `is_multi_file` alone routes through the
   cross-file path: the current (non-ledger) buffer self-validates via
   the single-file path while the unopened ledger files' diagnostics are
   still recomputed and republished, not cleared. multi_file_diagnostics
   handles current_file_id == None (unopened targets only).

2. ledger_diagnostics_multi injected the current file's fresh plugins
   into the shared whole-ledger validation UNCONDITIONALLY — even when
   the current parse was broken, whose recovered .plugins list is partial
   or empty. That dropped e.g. auto_accounts from the validation the clean
   unopened files run against, producing phantom E1001s. Now the fresh
   plugins are used only when the current file is a ledger file AND parsed
   cleanly (mirroring the directive-overlay's clean guard); otherwise the
   ledger's stored plugins are kept, matching the old per-file behavior.

Also: restored the size-cap "Skipping validation for large file" debug
log for parity with all_diagnostics, and corrected the merged_plugins
comment.

Regression tests: editing_a_non_ledger_buffer_keeps_unopened_file_diagnostics
(integration) and broken_current_parse_keeps_stored_plugins_for_cross_file_validation
(unit, asserts no phantom E1001).
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.

2 participants