simonmichael / hledger

Robust, fast, intuitive plain text accounting tool with CLI, TUI and web interfaces.
https://hledger.org
GNU General Public License v3.0
2.85k stars 307 forks source link

`check` command should not stop after the first error #2203

Open pwseo opened 3 weeks ago

pwseo commented 3 weeks ago

Currently, if one turns on strict mode or uses the check command (eg. check accounts, check payees), hledger stops after the first undeclared account/payee is encountered.

While this is technically correct, it makes one repeat the edit-check-fix cycle more times than should be needed.

Here's an example:

$ hledger --version
hledger 1.34, linux-x86_64
$ cat a.j
2024-06-08 A | Txn 1
    Undeclared:One      +5 EUR
    Undeclared:Another  -5 EUR

2024-06-08 B | Txn 2
    Undeclared:Another  +5 EUR
    Undeclared:One      -5 EUR
$ hledger -f a.j accounts
Undeclared:Another
Undeclared:One
$ hledger -f a.j payees
A
B

As it stands, we have 2 undeclared accounts and 2 undeclared payees. Let's check them with hledger:

$ hledger -f a.j check accounts
hledger: Error: /tmp/tmp.EQDr9awkqd/a.j:2:
  | 2024-06-08 A | Txn 1
2 |     Undeclared:One               5 EUR
  |     ^^^^^^^^^^^^^^
  |     Undeclared:Another          -5 EUR

Strict account checking is enabled, and
account "Undeclared:One" has not been declared.
Consider adding an account directive. Examples:

account Undeclared:One
account Undeclared:One    ; type:A  ; (L,E,R,X,C,V)

The same thing happens for payees: hledger never sees payee B and thus never complains about it because it quits after reporting A is not declared.

I think it would be helpful if hledger continued processing the data and collected as much undeclared accounts / payees as possible before presenting them to the user. Perhaps this would not belong in the -s argument's default behaviour (so as not to overwhelm new users), but only in the explicit check command (it does make sense to report all undeclared accounts if I ask to check accounts, I think).

simonmichael commented 3 weeks ago

I'd be happy to test a PR exploring this. Some checks are relatively easy to continue from on failure; others are not.

But I don't find it causing pain in practice, do you ? I don't have a slow edit-check-fix cycle.

pwseo commented 3 weeks ago

I'd be happy to test a PR exploring this. Some checks are relatively easy to continue from on failure; others are not.

Maybe one day. Right now my Haskell skills are subpar and I have no familiarity with hledger's code base (sadly). I'll try and change that in the future.

But I don't find it causing pain in practice, do you ? I don't have a slow edit-check-fix cycle.

In a carefully maintained chart of accounts this isn't problematic (especially if one has some kind of auto-complete for account names), but scenarios vary. I recently started tracking my finances with hledger and there was a lot of checking and fixing to do in the beginning (because my chart of accounts wasn't fully developed).

Still, on a more general note, I think having the check command try and report as many errors as it can is a positive change, while passing the --strict flag should still quit after the first error is found.

simonmichael commented 3 weeks ago

Not detracting from your point, but remember strict account checking is optional and simply avoiding it is another option when the chart of accounts hasn't yet stabilised.

pwseo commented 3 weeks ago

Yes, I know, but strictness checking is also the only way to know which accounts have not been declared and are being used. One can always get the used accounts' listing from a non-strict hledger accounts, but it has its downsides.

Thank you for the suggestion, though.