Skip to content

1.19-1.22: balancesheet --tree --no-elide --begin with certain account directives can show wrong balances #1698

Closed

Description

@Flimm

I noticed a difference in the final line between these two commands, even though the only difference is the --tree flag. Here is an example (anonymised):

$ hledger bs --no-elide --begin=2019-01-01
                                       ||                                                 2021-09-14 
=======================================++============================================================
 Assets                                ||                                                            
---------------------------------------++------------------------------------------------------------
 assets:parent:child1                  ||                                                  GBP 90.00 
 assets:parent:child2                  ||                                                GBP 2731.02 
---------------------------------------++------------------------------------------------------------
                                       || GBP 2821.02 
=======================================++============================================================
 Liabilities                           ||                                                            
---------------------------------------++------------------------------------------------------------
---------------------------------------++------------------------------------------------------------
                                       || 
=======================================++============================================================
 Net:                                  || GBP 2821.02

$ hledger bs --no-elide --begin=2019-01-01 --tree
Balance Sheet 2021-09-14

                         ||                                                2021-09-14 
=========================++===========================================================
 Assets                  ||                                                           
-------------------------++-----------------------------------------------------------
 assets                  || GBP 4121.98 
   parent                ||                                               GBP 4121.98 
     child1              ||                                                 GBP 90.00 
     child2              ||                                               GBP 2731.02 
-------------------------++-----------------------------------------------------------
                         || GBP 4121.98
=========================++===========================================================
 Liabilities             ||                                                           
-------------------------++-----------------------------------------------------------
-------------------------++-----------------------------------------------------------
=========================++===========================================================
 Net:                    || GBP 4121.98

I can't explain why the the two Net numbers differ, even though the only difference is the --tree flag. This change in calculation doesn't seem to happen when I don't pass --begin=2019-01-01.

I'm afraid I can't figure out a small reproducible example, that's why I shared an anonymous sample output, in which I removed most of the lines.

I'm using the latest hledger version on Ubuntu:

$ hledger --version
hledger 1.21

Activity

  1. added
    A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.
    on Sep 15, 2021
  2. simonmichael commented on Sep 15, 2021

    @simonmichael
    Member

    Thanks for the report @Flimm. Would you be able to test with the current release ? That's 1.22.2, available from https://hledger.org/download.html

  3. Flimm commented on Sep 15, 2021

    @Flimm
    ContributorAuthor

    Thanks! I thought I had upgraded, but I was mistaken. I did not experience this issue in 1.22.2. My apologies.

  4. Xitian9 commented on Sep 15, 2021

    @Xitian9
    Collaborator

    I'd still be interested in knowing what exactly caused this issue in an old version. That's a rather alarming mistake, and I'd like there to be a test for it.

  5. simonmichael commented on Sep 16, 2021

    @simonmichael
    Member

    Agreed, for such a bad issue we should know the versions affected and advertise it.

  6. Xitian9 commented on Sep 16, 2021

    @Xitian9
    Collaborator

    @Flimm

    Any information you could give us to help reproduce this would be great. How big is your journal, when does it start and end, what is the narrowest date range you can choose which still shows this error (and what postings does that encompass), are there any multicommodity amounts or prices, and so on.

    It would be even better if you could do a git bisect and identify the precise commit which fixes it.

  7. simonmichael commented on Sep 16, 2021

    @simonmichael
    Member

    Usually a smaller repro is possible - keep removing things until it stops happening. There's also hledger print --anon, and secure 1-to-1 matrix chat if that helps.

  8. Flimm commented on Sep 16, 2021

    @Flimm
    ContributorAuthor

    Thanks for encouraging me. I did what you said, and I found a reproducible example. Here is rep.journal:

    account assets:foobar:reserve    ; type: Cash
    account assets:foobar:current    ; type: Cash
    account assets:receivable:anon   ; type: Asset
    
    2018-12-06
        assets:foobar:current              GBP -63.49
        expenses:example
    
    2019-01-04
        assets:foobar:current              GBP -63.49
        expenses:example
    
    

    It turns out the --no-elide is required for the bug to manifest itself.

    $ hledger1.21 --version
    hledger 1.21
    $ hledger1.21 -f rep.journal bs --no-elide --begin=2019-01-01
    Balance Sheet 2019-01-04
    
                           ||  2019-01-04 
    =======================++=============
     Assets                ||             
    -----------------------++-------------
     assets:foobar:current || GBP -126.98 
    -----------------------++-------------
                           || GBP -126.98 
    =======================++=============
     Liabilities           ||             
    -----------------------++-------------
    -----------------------++-------------
                           ||             
    =======================++=============
     Net:                  || GBP -126.98 
    $ hledger1.21 -f rep.journal bs --no-elide --begin=2019-01-01 --tree
    Balance Sheet 2019-01-04
    
                 ||  2019-01-04 
    =============++=============
     Assets      ||             
    -------------++-------------
     assets      ||  GBP -63.49 
       foobar    ||  GBP -63.49 
         current || GBP -126.98 
    -------------++-------------
                 ||  GBP -63.49 
    =============++=============
     Liabilities ||             
    -------------++-------------
    -------------++-------------
                 ||             
    =============++=============
     Net:        ||  GBP -63.49 

    The two commands should have identical Net totals, but they don't.

    I also tried it with hledger 1.22.2, and it seems to be in that version too.

  9. Xitian9 commented on Sep 16, 2021

    @Xitian9
    Collaborator

    Great work! I can reproduce on master.

    This as a weird one, as it seems to require all of:

    • balancesheet mode
    • a start date
    • tree mode
    • no-elide
    • account declarations

    Let me dig into this.

    Tentative hypothesis: historical postings are not being included in subtotals for boring parents in compound balance reports, for some reason. But only when you manually declare account types?

  10. Xitian9 commented on Sep 16, 2021

    @Xitian9
    Collaborator

    Aha! I've got it.

    By declaring account types manually, you've overridden the default rules. This means that the following accouns are not considered asset accounts, and so are not included in the balancesheet: assets, assets:foobar. Since these accounts have no postings in them this would not normally be a problem, except that since you've asked for --no-elide in --tree mode, it goes ahead and puts them in.

    CompoundBalanceReport will filter historical postings based on whether they match the subreport query: in this case whether it's an Asset or Cash account. Since the parent accounts don't match this, it throws out their historical postings.

    A temporary fix is to declare all your parent accounts:

    account assets                   ; type: Cash
    account assets:foobar            ; type: Cash
    account assets:foobar:current    ; type: Cash
    account assets:receivable:anon   ; type: Asset
    
  11. Xitian9 commented on Sep 16, 2021

    @Xitian9
    Collaborator

    You found a knotty one here @Flimm. Thanks so much for your work cutting this down to size!

  12. simonmichael commented on Sep 18, 2021

    @simonmichael
    Member

    Excellent! This is indeed quite hard to wrap one's head around; I'm glad you two did. This shows it in another way:

    hledger 1.22 bal, showing expected output:

    $ hledger-1.22.2 -f c.j bal --begin=2019-01-01 -D -t --no-elide -HE
    
    Ending balances (historical) in 2019-01-01..2019-01-04:
    
                 || 2019-01-01  2019-01-02  2019-01-03   2019-01-04 
    =============++=================================================
     assets      || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
       foobar    || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
         current || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
     expenses    ||  GBP 63.49   GBP 63.49   GBP 63.49   GBP 126.98 
       example   ||  GBP 63.49   GBP 63.49   GBP 63.49   GBP 126.98 
    -------------++-------------------------------------------------
                 ||          0           0           0            0 

    hledger 1.22 bs, showing incorrect numbers for parents in first columns:

    $ hledger-1.22.2 -f c.j bs --begin=2019-01-01 -D --tree --no-elide
    
    Balance Sheet 2019-01-01..2019-01-04
    
                 || 2019-01-01  2019-01-02  2019-01-03   2019-01-04 
    =============++=================================================
     Assets      ||                                                 
    -------------++-------------------------------------------------
     assets      ||          0           0           0   GBP -63.49 
       foobar    ||          0           0           0   GBP -63.49 
         current || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    -------------++-------------------------------------------------
                 ||          0           0           0   GBP -63.49 
    =============++=================================================
     Liabilities ||                                                 
    -------------++-------------------------------------------------
    -------------++-------------------------------------------------
                 ||                                                 
    =============++=================================================
     Net:        ||          0           0           0   GBP -63.49 

    With the #1699 fix, numbers look correct again, similar to bal:

    $ stack exec -- hledger -f c.j bs --begin=2019-01-01 -D --tree --no-elide
    
    Balance Sheet 2019-01-01..2019-01-04
    
                 || 2019-01-01  2019-01-02  2019-01-03   2019-01-04 
    =============++=================================================
     Assets      ||                                                 
    -------------++-------------------------------------------------
     assets      || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
       foobar    || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
         current || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    -------------++-------------------------------------------------
                 || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    =============++=================================================
     Liabilities ||                                                 
    -------------++-------------------------------------------------
    -------------++-------------------------------------------------
                 ||                                                 
    =============++=================================================
     Net:        || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
  13. simonmichael commented on Sep 18, 2021

    @simonmichael
    Member

    It seems this was one of the casualties in 1.19, ie a regression.
    So @Flimm, thank you and please claim your https://hledger.org/regressionbounty !

    account assets:foobar:reserve    ; type: Asset
    account assets:foobar:current    ; type: Asset
    account assets:receivable:anon   ; type: Asset
    
    2018-12-06
        assets:foobar:current              GBP -63.49
        expenses:example
    
    2019-01-04
        assets:foobar:current              GBP -63.49
        expenses:example
    
    $ hledger-1.18 -f c.j bs --begin=2019-01-01 -D --tree --no-elide
    
    Balance Sheet 2019-01-01..2019-01-04
    
                 || 2019-01-01  2019-01-02  2019-01-03   2019-01-04 
    =============++=================================================
     Assets      ||                                                 
    -------------++-------------------------------------------------
     assets      || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
       foobar    || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
         current || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    -------------++-------------------------------------------------
                 || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    =============++=================================================
     Liabilities ||                                                 
    -------------++-------------------------------------------------
    -------------++-------------------------------------------------
                 ||                                                 
    =============++=================================================
     Net:        || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    
    $ hledger-1.19 -f c.j bs --begin=2019-01-01 -D --tree --no-elide
    
    Balance Sheet 2019-01-01..2019-01-04
    
                 ||         2019-01-01          2019-01-02          2019-01-03           2019-01-04 
    =============++=================================================================================
     Assets      ||                                                                                 
    -------------++---------------------------------------------------------------------------------
     assets      ||                  0                   0                   0   GBP -63.49 
       foobar    ||                  0                   0                   0   GBP -63.49 
         current || GBP -63.49  GBP -63.49  GBP -63.49  GBP -126.98 
    -------------++---------------------------------------------------------------------------------
                 ||                  0                   0                   0   GBP -63.49 
    =============++=================================================================================
     Liabilities ||                                                                                 
    -------------++---------------------------------------------------------------------------------
    -------------++---------------------------------------------------------------------------------
                 ||                                                                                 
    =============++=================================================================================
     Net:        ||                  0                   0                   0   GBP -63.49 
  14. simonmichael commented on Sep 18, 2021

    @simonmichael
    Member

    For each major release, our usual policy is to make bugfix releases until it is free of major bugs. In 1.19.1 (2020/09/07), we fixed most of them, but we missed this one. Do we need to backport this fix to 1.19, 1.20, 1.21, and 1.22 ? I think, and hope, this one is obscure enough that we can just fix master and move on.

  15. added
    regressionA backwards step, indicating a weakness in our QA. We don't like these.
    on Sep 18, 2021
  16. changed the title [-]Combining --tree with --begin for balancesheet causes miscalculation[/-] [+]balancesheet --tree --no-elide --begin with certain account directives can show wrong balances (1.19-1.22)[/+] on Sep 18, 2021
  17. Xitian9 commented on Sep 18, 2021

    @Xitian9
    Collaborator

    I think we're pretty safe here. The combination of factors that trigger it are unlikely.

  18. added a commit that references this issue on Nov 25, 2021
  19. changed the title [-]balancesheet --tree --no-elide --begin with certain account directives can show wrong balances (1.19-1.22)[/-] [+]1.19-1.22: balancesheet --tree --no-elide --begin with certain account directives can show wrong balances[/+] on Dec 16, 2023
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

    A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.balancesheetbountyThar's some kind o' loot on offer..regressionA backwards step, indicating a weakness in our QA. We don't like these.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions