Repository navigation
fix(check): render validation errors with source snippets (closes #901) - #906
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves rledger check diagnostics by propagating validation error locations/spans into LedgerError and using miette::GraphicalReportHandler to render rich source snippets (closing #901), while also cleaning up duplicated [E1234] prefixes and fixing severity/message formatting in snapshots/JSON output.
Changes:
- Stop duplicating error codes in
ValidationErrordisplay output so CLI/JSON renderers can prepend codes consistently. - Propagate
span+file_idintoLedgerError(and derivefile:line:colfromSourceMap) so downstream renderers can locate errors precisely. - Add
render_ledger_errorand switchchecktext output to miette-based snippet rendering when span data is available (with a one-line fallback).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/rustledger/src/report.rs | Adds render_ledger_error to render LedgerError via miette when span+file_id are present. |
| crates/rustledger/src/cmd/check.rs | Switches text-mode error printing to render_ledger_error and uses the ledger SourceMap for snippet rendering. |
| crates/rustledger-loader/src/process.rs | Extends LedgerError with source_span/file_id, propagates location info from validation errors, and marks LedgerError non-exhaustive. |
| crates/rustledger-validate/src/error.rs | Removes [CODE] prefix from ValidationError’s Display to avoid double-tagging. |
| crates/rustledger/tests/snapshots/*.snap | Updates CLI snapshot outputs (text + JSON) to reflect new rich rendering and corrected location/severity/message fields. |
Comments suppressed due to low confidence (1)
crates/rustledger/src/cmd/check.rs:554
let source_map = ledger.source_map;moves thesource_mapfield out ofledger, butledgeris still used later in this scope (e.g.for err in &ledger.errors, and underpython-plugin-wasmledger.options). This will fail to compile due to a partial move. Consider keepingledgerasmutand borrowing&ledger.source_mapwhen rendering, or destructureledgeronce into the individual fields you need (directives/errors/options/source_map) so you don’t access a partially moved value.
let source_map = ledger.source_map;
// Convert process errors to diagnostics, using the phase field to
// split into parse/validate/plugin categories.
for err in &ledger.errors {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
a8512e0 to
b46700e
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs main branch (2026-04-24)
What do these numbers mean?
|
b46700e to
d8bcae2
Compare
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
d8bcae2 to
04f9a2e
Compare
Closes #901. Before: main.beancount: error[E3001]: [E3001] Transaction does not balance: residual -1 USD ✗ 1 error After (miette-rendered, NO_COLOR=1 for clarity): E3001 x Transaction does not balance: residual -1 USD ,-[main.beancount:4:1] 3 | 4 | ,-> 2026-04-07 * "my expense" 5 | | Expenses:Expense 10 USD 6 | |-> Assets:Wallet -11 USD : `---- here `---- ✗ 1 error Three fixes, all using infrastructure that already existed: 1. Remove the `[E1234]` prefix from ValidationError's Display impl. CLI and IDE renderers prepend `error[CODE]:` themselves, so the bare message is what Display should produce. Eliminates the double tag. 2. Populate LedgerError.location (previously hardcoded to None) from ValidationError's span + file_id during run_validation. The source map lookup and span->line:col conversion already existed in the same file; just wire them up. 3. Add `source_span` and `file_id` fields to LedgerError and render validation errors through miette's GraphicalReportHandler (the same path parse errors already use) when both are present. Falls back to a single `file:line:col: error[CODE]: message` line when not. Also marks LedgerError #[non_exhaustive] so future field additions don't break downstream struct-literal users. Snapshot tests updated to reflect the new output; three pre-existing bugs were exposed and fixed as a side effect: - Warnings were emitted as `error[E1004]` instead of `warning[E1004]`. - JSON diagnostics reported line 1 for all validation errors (no location was propagated at all). - JSON message field included the `[CODE]` prefix in the message body. All 565 tests in the affected crates pass.
04f9a2e to
4ec28e6
Compare
Summary
Fixes #901.
rledger checkvalidation errors now show a miette-rendered source snippet of the offending directive with proper file:line:col location, rather than a one-liner that doesn't tell the user which transaction broke.Before
After
(Color+unicode variant in a real terminal; NO_COLOR=1 shown here for readability.)
This matches information parity with Python beancount (
file:line: message+ transaction echo) and is actually more informative — line numbers, span highlighting, and color support. The samemiette::GraphicalReportHandlerpath that parse errors already use.Scope of changes
Three fixes, all using infrastructure that already existed:
[E1234]duplication —ValidationError'sDisplayimpl was#[error("[{code}] {message}")], which the CLI then wrapped witherror[{code}]:, producingerror[E3001]: [E3001] .... Changed to#[error("{message}")]. (crates/rustledger-validate/src/error.rs)Missing location —
run_validationinloader/src/process.rswas hardcodinglocation: Nonewhen convertingValidationError→LedgerError, even thoughValidationError.span+file_idwere available. Now populated viasource_map.get().line_col(). (crates/rustledger-loader/src/process.rs)No source snippet — Added
source_spanandfile_idfields toLedgerErrorso thecheck.rsCLI has the data miette needs. Added arender_ledger_errorhelper inreport.rsthat uses the existing miette handler when span info is present, falls back to a one-liner when not. (crates/rustledger/src/report.rs,crates/rustledger/src/cmd/check.rs)Also marked
LedgerError#[non_exhaustive]so future field additions don't break downstream struct-literal users.Side-effect bug fixes
The snapshot-test updates exposed three pre-existing bugs that are now fixed as a consequence:
error[E1004]instead ofwarning[E1004].line: 1, column: 1for all validation errors (location was never propagated at all).messagefield included the[CODE]prefix inside the message body.Test plan
rustledger-validate,rustledger-loader,rustledgercargo fmt --checkcleanCloses #901