Skip to content

query/report exit 0 and emit data from a file that failed to parse #1908

Closed

Description

@robcohen

rledger query and rledger report return exit 0 on a file they failed to parse, and emit data derived from the partially-parsed ledger. rledger check on the same file correctly fails.

Any automation that pipes rledger query --format csv into another tool receives silently wrong numbers alongside a success status.

Repro

2020-01-01 open Assets:A
this is not valid beancount at all !!!
2020-01-02 * "x"
  Assets:A  5 USD
  Assets:A -5 USD
$ rledger check broken.beancount ; echo "exit=$?"
...error...
exit=2

$ rledger query --format csv broken.beancount "SELECT account, sum(number) AS n GROUP BY account" ; echo "exit=$?"
account,n
Assets:A,0
exit=0            # <-- success, with data from a file that does not parse

$ rledger report broken.beancount balances ; echo "exit=$?"
exit=0

The parse error IS written to stderr (LOAD: parse errors in ...), so an interactive user sees it. Automation checking $?, or consuming stdout, gets no signal at all.

Why this matters more than a usual exit-code nit

The output is not empty, it is plausible. Assets:A,0 looks like a real answer. For a tool whose entire job is producing numbers people rely on, "confidently wrong and reporting success" is the worst available failure mode — worse than crashing.

It also means check and query disagree about whether a file is usable, so a CI pipeline that gates on check and then reports with query is inconsistent with itself.

How it was found

By deepening the beancount oracle to compare values rather than shapes (see the linked PR). The existing comprehensive compat test compares check_match, accounts_match, posting_count_match and error_presence_match — how MANY postings and WHETHER there was an error, never an amount. This class of bug is invisible to it by construction.

Second, smaller finding from the same run

rledger cannot parse non-ASCII account names that beancount accepts:

$ grep CORP ledger_unicode.beancount
  Assets:CORP✨      100 CORP {1 USD}

$ rledger query --format csv ledger_unicode.beancount "SELECT DISTINCT account"
account
Assets:Cash
Assets:CORP        # <-- truncated at the non-ASCII character

beancount reports 0 errors on that file and the account Assets:CORP✨. rledger reports a parse error, and the truncation means Assets:CORP✨ and Assets:CORP would collide. Worth its own issue if the exit-code behaviour above is fixed first — with a hard failure, the truncated account never reaches a result set.

Suggested decision

Whether query/report should hard-fail on parse errors, or gain a --allow-errors style opt-in for the interactive case, is a policy call rather than an obvious fix — hence filing rather than patching.

Activity

  1. robcohen commented on Jul 31, 2026

    @robcohen
    MemberAuthor

    Correction: this is beancount-compatible behavior, not a divergence

    I filed this as a plain bug. Before fixing it I checked what bean-query actually does, and it does the same thing:

    $ bean-query broken.beancount "SELECT account, sum(number) AS n GROUP BY account" ; echo "exit=$?"
    account   n
    --------  -
    Assets:A  0
    exit=0

    Byte-identical to rledger, same exit code. And beancount's loader behaves the same way underneath — on a file with a LexerError it still returns the one valid transaction:

    broken.beancount    errors=['LexerError', 'ValidationError']  transactions_booked=1
    

    The rledger code says so explicitly, and it is deliberate:

    // Report errors to stderr (matching bean-query behavior)
    // Continue with successfully parsed directives rather than bailing

    So rledger is not misbehaving here. It is faithfully matching the reference implementation.

    What the question actually is

    Not "fix a bug" but "do we deliberately deviate from beancount?" — which CLAUDE.md's compatibility policy has a bucket for:

    Fixable — we deviate, stay stricter or more correct than Python.

    The case for deviating:

    • Emitting plausible numbers from a file that did not parse is the worst failure mode for an accounting tool. Assets:A 0 looks like an answer.
    • rledger check already exits non-zero on the same file, so the CLI contradicts itself: one command calls the file unusable, another reports on it and claims success.
    • Automation consuming query --format csv has no signal at all — the parse error only reaches stderr.

    The case against:

    • Compatibility is the product's core promise, and this is behavior a bean-query user may rely on.
    • A hard failure would show up as a divergence in the BQL compat suite unless registered under KNOWN_RUST_DIVERGENCES.
    • It could break an existing pipeline that tolerates messy input today.

    A narrower option

    Fail only on parse errors, leave validation errors (unopened account, failed assertion) reporting exactly as now. That keeps compat for the common messy-ledger case, where beancount's own booking is sound, and refuses only when the entry stream is genuinely incomplete. It is a much smaller deviation and it targets precisely the case where the numbers are untrustworthy.

    Deferring to a maintainer decision rather than changing beancount-compatible behavior unilaterally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions