Skip to content

Coverage does not work for packages that use internal libraries or library components #6440

Open

Description

@newhoggy

Describe the bug
Coverage does not work for packages that use internal libraries or library components. This is true even if package is only a build dependency, making it very easy encounter this issue on any reasonably sized project.

To Reproduce
Steps to reproduce the behavior:

$ git clone [email protected]:haskell-works/hw-ip.git
$ cd hw-ip
$ cabal v2-test --enable-coverage
Error:
    Internal libraries only supported with per-component builds.
    Per-component builds were disabled because program coverage is enabled
    In the package 'hw-ip-2.4.0.1'

Please use version-prefixed commands (e.g. v2-build or v1-build) to avoid ambiguity.

Expected behavior
I expect coverage to just work for any typical package that has been published to hackage.

System information

$ cabal --version
cabal-install version 3.0.0.0
compiled using version 3.0.0.0 of the Cabal library
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.5
$ uname -a
Darwin INTLKyMac.local 18.7.0 Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64

Additional context
None

Activity

  1. symbiont-sam-halliday commented on Mar 23, 2020

    @symbiont-sam-halliday

    I also observe this with cabal-install 3.0.0.0 and ghc-8.3.3.

    It is unfortunate, because the equivalent command works in stack on the same project.

    If I do --enable-coverage --disable-library-coverage I don't see any files after running cabal test.

  2. symbiont-sam-halliday commented on Mar 26, 2020

    @symbiont-sam-halliday

    Workaround: #5213 (comment)

    tl;dr specifically disable coverage for libraries that use internal libraries, e.g.

    package *
      coverage: True
      library-coverage: True
    
    package dns
      coverage: False
      library-coverage: False
    

    (note that this doesn't work if we don't use package *)

  3. ivanperez-keera commented on May 7, 2022

    @ivanperez-keera
    Contributor

    I believe this may be the reason behind copilot-core's tests being reported as broken on hackage.

    I ran the build in a docker image using:

    cabal test all --enable-coverage --disable-optimization copilot**/
    

    and was met with:

    Resolving dependencies...
    Error:
        Internal libraries only supported with per-component builds.
        Per-component builds were disabled because program coverage is enabled
        In the package 'attoparsec-0.14.4'
    
  4. peterbecich commented on May 15, 2022

    @peterbecich
    Member

    I see this with Cabal 3.6.2.0 and Pandoc:

    After cloning https://github.com/jgm/pandoc.git,

    % cabal test --enable-coverage
    Resolving dependencies...
    Error:
        Internal libraries only supported with per-component builds.
        Per-component builds were disabled because program coverage is enabled
        In the package 'attoparsec-0.14.4'
    

    The workaround #6440 (comment) does not appear to have an effect:

    packages: *
    tests: True
    flags: +embed_data_files
    constraints: aeson >= 2.0.1.0
    
    -- coverage: True
    -- library-coverage: False
    
    package attoparsec
      coverage: False
      library-coverage: False
    

    I've tried a couple variations on this cabal.project


    I have also installed the latest version of Cabal (3.7.0.0) from this repository and tried that.


    This does succeed:

    cabal test --enable-coverage --disable-library-coverage
    

    However, there are no .mix or .tix files created.

  5. RyanGlScott commented on May 16, 2022

    @RyanGlScott
    Member

    I think you're missing a key part of the workaround in #6440 (comment), which is the package * stanza:

    package *
      coverage: True
      library-coverage: True
    
    package attoparsec
      coverage: False
      library-coverage: False
    

    If I add that and run cabal test (without specifying the --enable-coverage) flag, then I'm able to observe .mix and .tix files after running the pandoc test suite.

  6. RyanGlScott commented on May 16, 2022

    @RyanGlScott
    Member

    In fact, I don't think the workaround even requires disabling coverage for packages with internal libraries. I was also able to get coverage working using cabal test and this simpler configuration:

    package *
      coverage: True
      library-coverage: True
    
  7. Mikolaj commented on May 16, 2022

    @Mikolaj
    Member

    Probably --enable-coverage probably overrides anything in the project file, which would explain the observations.

  8. jneira commented on May 16, 2022

    @jneira
    Member

    --enable-coverage would override the options set in the config files for local packages, being equivalent to top level, out of any package stanza, coverage: true
    in theory of course

  9. peterbecich commented on May 21, 2022

    @peterbecich
    Member

    #6440 (comment) @RyanGlScott you are correct, it works with Cabal 3.6.2.0 and

    package *
      coverage: True
      library-coverage: True
    

    and cabal test.

    I see the .mix and .tix files

    Thanks all

  10. added
    re: internal library
    on Feb 4, 2023
  11. added
    can-workaround
    and removed
    can-workaround
    on Nov 10, 2023
  12. added a commit that references this issue on Nov 22, 2023
    3690ba1
  13. 43 remaining items

  14. added 4 commits that reference this issue on Dec 11, 2023
    7998ca2
    3e5e73d
    ad373ac
    9779d8f
  15. added a commit that references this issue on Dec 18, 2023
    d6e3804
  16. peterbecich commented on Feb 18, 2024

    @peterbecich
    Member

    Can this issue be re-opened? I believe this work-around is still necessary: #6440 (comment)

  17. dpwiz commented on Feb 27, 2024

    @dpwiz

    Yep, it is. Came here right after hitting the attoparsec error.

  18. ivanperez-keera commented on Sep 16, 2024

    @ivanperez-keera
    Contributor

    The attoparsec error is the reason why copilot-language is not running the tests on hackage, by the way: https://hackage.haskell.org/package/copilot-language-4.0/reports/1/test

  19. gbaz commented on Jul 13, 2025

    @gbaz
    Collaborator

    This would be good to fix. It looks like threre's some draft work by @erikd and @alt-romes linked above? What's the status on that?

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

    Cabal: code-coveragere: internal librarytype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions