hledger icon indicating copy to clipboard operation
hledger copied to clipboard

aliases from included files are not applied in including files

Open mildred opened this issue 5 years ago • 3 comments

I'm playing with account hierarchy to learn accounting, and I noticed that aliases defined in a ledger file included in a global file are not applied to the global file. I'm using the following file structure:

global.ledger file:

#!/usr/bin/env hledger-exec

include accounts.ledger

# Following are the transactions
...

accounts.ledger file contains the account hierarchy with its aliases:

account financial accounts:banks:main bank account  ; type:Asset no:512
alias bank=financial accounts:banks:main bank account
alias a512=financial accounts:banks:main bank account
...

account expenses:buying ; type:Expense no:60
alias a60=expenses:buying
...

account revenue:products sold ; type:Revenue no:70
alias a70=revenue:products sold
...

I'm using this hierarchy because when running hledger-web or hledger-ui from the main files, new transactions made from the UI are appended to the main file. However, I want the account hierarchy to be separate, simply because I might modify it while I'm using hledger-web or ui at the same time.

When I declare aliases in accounts.ledger, they are not used in the global file. I have to declare them in the global file for them to be understood.

The fact that aliases are not propagated to including files is not in the documentation.

Perhaps the documentation should be updated, and perhaps aliases should be allowed to span to including files. having them restricted to the files they are in is also understandable, but perhaps use case around this should be discussed more.

mildred avatar Apr 10 '19 16:04 mildred

'Tis mentioned a bit, at https://hledger.org/manual.html#basic-aliases, and particularly at https://hledger.org/manual.html#directives - see the notes there. Basically alias and some other directives are designed to last only until the end of the current file.

I see how this can be surprising/unsatisfying. I often find it annoying myself. Why does it work this way? It's to preserve these properties, which I think are worthwhile:

  1. reordering files does not change their meaning
  2. adding a file does not change the meaning of the other files

As a practical matter for right now,

  • ~put aliases inline at the top of the main journal file~ (or an including file, like your global.ledger). This should cause no problems for hledger-web or hledger-ui --watch, they will detect your file edits.
  • ~we should add this to the FAQ (done)~
  • this should be made clearer at https://hledger.org/hledger.html#directives and https://hledger.org/hledger.html#alias-directive

simonmichael avatar Apr 10 '19 16:04 simonmichael

Aliases actually propagated to included files. Since migration to hledger I changed my layout to something like:

# effective-hledger.dat

commodity $                
    ; note American Dollars    
    format $1,000,000,000.00          

alias tips=Expenses:Tips

include ledger-2009.dat
# ...
include ledger-2019.journal

Though it is only chain and I cannot have anymore currencies/accounts/aliases split in different files it works for me.

I guess this is come-back of #510 . Maybe it worth to consider export include accounts.ledger syntax or something similar.

ony avatar Apr 13 '19 12:04 ony

Ran into the same issue. Somewhat unpleasant issue

xeroc avatar Dec 02 '20 12:12 xeroc