Balance assertions with multiple files

I'm working on a sort of full-fledged-hledger with just ledger, and I'm trying to figure out the logic for importing journals with balance assertions. Given the following main file:

;main.journal
2024/07/30 Opening balances
    Assets:Alipay                               ¥100
    Equity:Opening Balances

And a journal (generated from a bank's CSV using ledger convert):

;importing.journal
2024/08/02 7-11
    Expenses:Unknown                             ¥12
    Assets:Alipay                               ¥-12 = ¥88

With the command ledger --args-only -f main.journal -f importing.journal --sort date print

While parsing file "/home/.../importing.journal", line 4:
While parsing posting:
  Assets:Alipay                               ¥-12 = ¥88
                                                      ^^^^
Error: Balance assertion off by ¥100 (expected to see ¥-12)

Is there a way to make ledger acknowledge these assertions this way?

One solution I'm aware of is to concatenate the files, but in my use case, this makes it harder to track down errors with accounts because the line numbers don't match the lines in the files after concatenation.


And on a related note, if I stick some command directives like account and commodity declarations in another file, those declarations will affect the other files:

;init.dat
account Expenses:Groceries
account Assets:Alipay
account Equity:Opening Balances

commodity ¥
# Doesn't complain about the accounts (except for Expenses:Unknown)
ledger --strict -f init.dat  -f main.journal -f importing.journal --sort date print

In addition, account aliases don't apply if there's a balance assertion in importing.journal (and that assertion fails).

Maybe it works better if you include one or both files ?

I had the same idea just now as I got back from a walk. It seems to work. Thanks Simon!

1 Like

An alternative might be using deferred assertions, it's an under-documented feature (I learned about it from reading about it in the source code).

For example, maybe <Assets:Alipay> ¥-12 = ¥88 does what you want (the angle brackets are not a typo, that's what makes it deferred).

1 Like

Ah, thanks for digging that up! I am considering migrating from a system with deferred assertions, so this information is valuable to me. Since it's apparently undocumented, I don't know how many tools support it, but it'll be fun to try!