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

Account type tag inconsistently recognized when declared in multiple files #2202

Closed pwseo closed 3 weeks ago

pwseo commented 3 weeks ago

Account type: tags seem to be inconsistently recognized.

I have set up a minimum working example:

$ hledger --version
hledger 1.34, linux-x86_64
$ ls
a.j  b.j
$ cat a.j
account Node:Leaf A  ; type: Cash
include b.j
$ cat b.j
account Node:Leaf B  ; type: Cash

Now we use hledger's accounts' listing and request the account types as well:

$ hledger -s -f a.j accounts --types
Node:Leaf A    ; type: 
Node:Leaf B    ; type: C

Shouldn't Node:Leaf A also be of type: C?

-- Note: this does not happen if all account directives with type tags are declared in the same file.

pwseo commented 3 weeks ago

Some follow-up to what I have mentioned above: if I remove the account type: C tag from Node:Leaf B, suddenly it starts working correctly:

$ cat a.j
account Node:Leaf A  ; type: Cash
$ cat b.j 
account Node:Leaf B
$ hledger -s -f a.j accounts --types
Node:Leaf A    ; type: C
Node:Leaf B    ; type: 

Is there a file-scoped limit for account type tags? If so, I was not aware (and did not expect it, since account directives are global). Still, it seems like a bug is at play: when the type tag is read from an included file, previous tags are being discarded.

pwseo commented 3 weeks ago

Okay, so things are a little bit weirder than I antecipated. This behaviour manifests only for type tags with the same value.

Let's see another similar example:

$ cat a.j 
account Node:Leaf A  ; type: C
include b.j
include c.j
$ cat b.j 
account Node:Leaf B  ; type: C
$ cat c.j 
account Node:Leaf C  ; type: C

In this case, both Node:Leaf A and Node:Leaf B tags are ignored, only Node:Leaf C is kept:

$ hledger -s -f a.j accounts --types
Node:Leaf A    ; type: 
Node:Leaf B    ; type: 
Node:Leaf C    ; type: C

If we were to change Node:Leaf B to another type, both Node:Leaf B and Node:Leaf C would be correctly typed. Node:Leaf A's type will still be blank:

$ sed -i b.j -e 's/type: C/type: X/'
$ hledger -s -f a.j accounts --types
Node:Leaf A    ; type: 
Node:Leaf B    ; type: X
Node:Leaf C    ; type: C

And finally, if we change Node:Leaf A's type tag to something else, everything works:

$ sed -i a.j -e 's/type: C/type: R/'
$ hledger -s -f a.j accounts --types
Node:Leaf A    ; type: R
Node:Leaf B    ; type: X
Node:Leaf C    ; type: C
simonmichael commented 3 weeks ago

I agree, that is weird. Thanks for the report.

pwseo commented 3 weeks ago

@simonmichael, I believe the commits 531d34a and a18425c should reference issue #2201.

pwseo commented 3 weeks ago

I agree, that is weird. Thanks for the report.

I tried running hledger with --debug=9 but didn't find anything that might help. Can I help with anything else? (my Haskell skills are not very good).

simonmichael commented 3 weeks ago

@simonmichael, I believe the commits 531d34a and a18425c should reference issue #2201.

Right you are! I edited and force-pushed them.

I tried running hledger with --debug=9 but didn't find anything that might help. Can I help with anything else? (my Haskell skills are not very good).

I appreciate that. I tried the same, then I started adding dbg0 calls and reloading in ghci (just ghci). I have seen that Hledger.Read.Common.addDeclaredAccountTags is adding each tag declaration, but on each call it sees that jdeclaredaccounttags j is empty, which is strange (it should be accumulating results from each call.

simonmichael commented 3 weeks ago

I believe this is now fixed in master. Good catch (a bug with the original implementation of type:, not a regression).

pwseo commented 3 weeks ago

It is fixed indeed! I thank you for your dedication to this project :)