haskell / cabal

Official upstream development repository for Cabal and cabal-install
https://haskell.org/cabal
Other
1.62k stars 691 forks source link

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

Open newhoggy opened 4 years ago

newhoggy commented 4 years ago

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 git@github.com: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

symbiont-sam-halliday commented 4 years ago

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.

symbiont-sam-halliday commented 4 years ago

Workaround: https://github.com/haskell/cabal/issues/5213#issuecomment-586517129

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 *)

ivanperez-keera commented 2 years ago

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'
peterbecich commented 2 years ago

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 https://github.com/haskell/cabal/issues/6440#issuecomment-604409595 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.

RyanGlScott commented 2 years ago

I think you're missing a key part of the workaround in https://github.com/haskell/cabal/issues/6440#issuecomment-604409595, 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.

RyanGlScott commented 2 years ago

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
Mikolaj commented 2 years ago

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

jneira commented 2 years ago

--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

peterbecich commented 2 years ago

https://github.com/haskell/cabal/issues/6440#issuecomment-1127513329 @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

peterbecich commented 8 months ago

Can this issue be re-opened? I believe this work-around is still necessary: https://github.com/haskell/cabal/issues/6440#issuecomment-1133542171

dpwiz commented 8 months ago

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

ivanperez-keera commented 1 month ago

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