Skip to content

fix(check): match bean-check on close-with-balance; move advisory to lint - #1446

Merged
robcohen merged 2 commits into
mainfrom
fix/close-balance-to-lint
Jun 20, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/close-balance-to-lint

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

rledger check emitted E1004 / AccountCloseNotEmpty when an account was closed while still holding a non-zero balance. Python beancount's bean-check (3.2.3) never emits this — close records that an account is inactive from a date; it does not require a zero balance. So check was stricter than beancount on valid input, including our own valid-ledger.beancount fixture (which the check_clean snapshot test was asserting with a warning).

Per the Python Compatibility Policy (check mirrors bean-check), this moves the advisory out of check into a dedicated lint rather than dropping it — the "you closed an account with money still in it" nudge is genuinely useful, just not a check-level concern.

How

  • ErrorCode::is_advisory_only() marks E1004 as advisory-only.
  • check skips advisory-only codes in both its rendered report and its warning count (is_advisory_only_code), so valid-ledger.beancount now checks clean — matching bean-check.
  • The detection is unchanged in rustledger-validate, so the LSP/editor still surfaces the advisory.
  • New rledger lint closed-nonempty reuses the canonical process(validate: true) pipeline and keeps only E1004, with text and JSON output.

Verify

# before: E1004 warning; after: clean (matches bean-check)
rledger check valid-ledger.beancount

# the advisory now lives here
rledger lint closed-nonempty ledger.beancount
# -> ledger.beancount:7: Cannot close account Assets:Cash with non-zero balance
#    1 account(s) closed with a non-zero balance.
  • check_clean_{text,json} snapshots updated → "✓ No errors found", warning_count: 0.
  • Regression tests in closed_nonempty.rs: reports a balance-bearing close; silent on a zero-balance close.
  • cargo test -p rustledger -p rustledger-validate, clippy, and fmt all green.

🤖 Generated with Claude Code

…lint

Closing an account that still holds a non-zero balance produced E1004
(AccountCloseNotEmpty), which Python beancount's bean-check never emits:
close records that an account is inactive from a date, it does not
require a zero balance. This made `rledger check` stricter than beancount
on valid input, including our own valid-ledger.beancount fixture.

Per the Python Compatibility Policy, `check` mirrors bean-check. This
moves the diagnostic from check to a dedicated lint rather than dropping
it outright:

- ErrorCode::is_advisory_only() marks E1004 as advisory-only.
- `check` filters advisory-only codes from its report and its warning
  count (is_advisory_only_code), so valid-ledger.beancount now checks
  clean, matching bean-check.
- The detection is unchanged in validate, so the LSP/editor still warns.
- New `rledger lint closed-nonempty` reuses the canonical
  process(validate:true) pipeline and keeps only E1004, with text and
  JSON output.

Snapshots for check_clean_{text,json} updated (now "No errors found").
Regression tests: the lint reports a balance-bearing close and stays
silent on a zero-balance close.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner June 20, 2026 11:03
Copilot AI review requested due to automatic review settings June 20, 2026 11:03
@codecov

codecov Bot commented Jun 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.98131% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/lint/closed_nonempty.rs 89.13% 10 Missing ⚠️
crates/rustledger-validate/src/error.rs 50.00% 3 Missing ⚠️
crates/rustledger/src/cmd/lint/mod.rs 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

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 aligns rledger check behavior with Python beancount’s bean-check by treating E1004 / AccountCloseNotEmpty as advisory-only (not reported by check), while preserving the advisory via a dedicated rledger lint closed-nonempty subcommand.

Changes:

  • Mark ErrorCode::AccountCloseNotEmpty as advisory-only in rustledger-validate and filter advisory-only diagnostics from rledger check output + warning counts.
  • Add new rledger lint closed-nonempty command that reports only E1004 findings in text or JSON.
  • Update check_clean CLI snapshots to expect no warnings/diagnostics.

Reviewed changes

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

Show a summary per file
File Description
crates/rustledger/tests/snapshots/cli_snapshot_test__check_clean_text.snap Updates snapshot to expect clean check output (no E1004 warning).
crates/rustledger/tests/snapshots/cli_snapshot_test__check_clean_json.snap Updates JSON snapshot to expect empty diagnostics and warning_count: 0.
crates/rustledger/src/cmd/lint/mod.rs Registers the new closed-nonempty lint subcommand and dispatch.
crates/rustledger/src/cmd/lint/closed_nonempty.rs Implements rledger lint closed-nonempty (text/JSON reporting + tests).
crates/rustledger/src/cmd/check.rs Filters advisory-only codes from check diagnostics and warning count.
crates/rustledger-validate/src/error.rs Adds ErrorCode::is_advisory_only() and marks E1004 as advisory-only.

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

Comment thread crates/rustledger/src/cmd/check.rs Resolved
@github-actions

github-actions Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 36.1ms ± 0.3ms 24.6 MB
beancount 754.2ms ± 4ms 50.7 MB
Comparison 20.9x faster 2.1x less

vs main branch (2026-06-20)

Metric main this PR Change
Time 41.3ms 36.1ms -12.6% 🟢
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 20, 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

…rrors

Addresses Copilot review on #1446:

- Replace the hard-coded "E1004" literals in check.rs and
  closed_nonempty.rs with the canonical source: a new
  rustledger_validate::is_advisory_only_code() (string-keyed counterpart
  to ErrorCode::is_advisory_only) and ErrorCode::AccountCloseNotEmpty
  .code(). No more code-string drift if the code is ever renumbered.
- lint closed-nonempty now bails on a file with load errors instead of
  silently clearing them and reporting "no accounts" for input that
  didn't parse. The Loader surfaces parse/include errors in `errors`
  (not as Err), so the lint checks that list and fails loudly. Adds a
  regression test (missing include -> hard error).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen

Copy link
Copy Markdown
Member Author

Addressed all three Copilot findings in e1d5bc8:

  • closed_nonempty.rs (clearing errors): the lint now bails on a file with load errors instead of silently reporting "no accounts"; added a regression test (missing include → hard error).
  • check.rs / closed_nonempty.rs (hardcoded E1004): replaced both literals with the canonical rustledger_validate::is_advisory_only_code() and ErrorCode::AccountCloseNotEmpty.code() — no more code-string drift.

@robcohen
robcohen merged commit 81e90e8 into main Jun 20, 2026
42 checks passed
@robcohen
robcohen deleted the fix/close-balance-to-lint branch June 20, 2026 12:11
robcohen added a commit that referenced this pull request Oct 9, 2026
…2534)

* fix(query): skip advisory-only diagnostics on stderr, as check does

Closing an account that still holds a balance is E1004, which is
advisory-only: beancount does not flag it, so bean-check and bean-query
print nothing, and `rledger check` already skips it (#1446). #2238 turned
validation on in `query` (for `#balances.discrepancy`) without that skip,
so `rledger query` printed "E1004: Cannot close account ... with non-zero
balance" on ledgers `rledger check` called clean.

`query` now filters with the same `is_advisory_only_code` predicate as
`check`. Other diagnostics on the same load are still printed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

* fix(query): print option diagnostics, as check and bean-query do

`query` printed `ledger.errors` only. Option diagnostics (E7001 unknown
option, E7002 invalid value, the rest of E7xxx) are kept on
`options.warnings`, so a misspelled option was reported by `rledger
check` and bean-query and went unreported here. They are now printed
first, as bean-query does, ahead of the validation errors, through the
same `--no-errors` switch.

Neither kind of diagnostic changes query's exit status (bean-query
also exits 0).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[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

Development

Successfully merging this pull request may close these issues.

2 participants