From c3b8cee3b3ff1d914f34ec896ade0716cf213ef6 Mon Sep 17 00:00:00 2001 From: Rodrigo Mesquita Date: Wed, 7 Aug 2024 17:28:42 +0100 Subject: [PATCH] hpc: Don't cover non-dependency libraries We were passing the flag --coverage-for for every library in the package when building the testsuites. However, if a particular testsuite doesn't depend on one of the packages passed with --coverage-for we would crash. Since we look for the unit ids of all packages given in --coverage-for in the package database (to find hpc artifacts), if the testsuite builds before the library this lookup would fail. The fix is easy: don't pass --coverage-for= to if doesn't depend on . If is an indirect dependency of it'll no longer be covered by HPC, but this is a weird behaviour that I doubt anyone relies on. Fixes #10046 --- Cabal/src/Distribution/Simple/Configure.hs | 4 ++- .../Distribution/Client/ProjectPlanning.hs | 18 +++++++----- .../PackageTests/Regression/T10046/aa.cabal | 28 +++++++++++++++++++ .../Regression/T10046/app/Main.hs | 1 + .../Regression/T10046/cabal.project | 1 + .../Regression/T10046/cabal.test.hs | 6 ++++ .../Regression/T10046/src/MyLib.hs | 11 ++++++++ .../Regression/T10046/test/Main.hs | 1 + .../Regression/T10046/testbb/Main.hs | 1 + changelog.d/issue-10046 | 4 +++ 10 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/aa.cabal create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/app/Main.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/cabal.project create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/cabal.test.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/src/MyLib.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/test/Main.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T10046/testbb/Main.hs create mode 100644 changelog.d/issue-10046 diff --git a/Cabal/src/Distribution/Simple/Configure.hs b/Cabal/src/Distribution/Simple/Configure.hs index 6e98ee6c25f..04929bad030 100644 --- a/Cabal/src/Distribution/Simple/Configure.hs +++ b/Cabal/src/Distribution/Simple/Configure.hs @@ -1280,7 +1280,9 @@ configureComponents extraCoverageUnitIds = case enabled of -- Whole package configure, add package libs ComponentRequestedSpec{} -> mapMaybe mbCompUnitId buildComponents - -- Component configure, no need to do anything + -- Component configure, no need to do anything since + -- extra-coverage-for will be passed for all other components that + -- should be covered. OneComponentRequestedSpec{} -> [] mbCompUnitId LibComponentLocalBuildInfo{componentUnitId} = Just componentUnitId mbCompUnitId _ = Nothing diff --git a/cabal-install/src/Distribution/Client/ProjectPlanning.hs b/cabal-install/src/Distribution/Client/ProjectPlanning.hs index 23fed2c5bd1..4efa5f313b1 100644 --- a/cabal-install/src/Distribution/Client/ProjectPlanning.hs +++ b/cabal-install/src/Distribution/Client/ProjectPlanning.hs @@ -4058,7 +4058,7 @@ setupHsConfigureFlags Just _ -> error "non-library dependency" Nothing -> LMainLibName - configCoverageFor = determineCoverageFor elabPkgSourceId plan + configCoverageFor = determineCoverageFor elab plan setupHsConfigureArgs :: ElaboratedConfiguredPackage @@ -4469,13 +4469,14 @@ inplaceBinRoot layout config package = -- The list of non-pre-existing libraries without module holes, i.e. the -- main library and sub-libraries components of all the local packages in --- the project that do not require instantiations or are instantiations. +-- the project that are dependencies of the components being built and that do +-- not require instantiations or are instantiations. determineCoverageFor - :: PackageId - -- ^ The 'PackageId' of the package or component being configured + :: ElaboratedConfiguredPackage + -- ^ The package or component being configured -> ElaboratedInstallPlan -> Flag [UnitId] -determineCoverageFor configuredPkgSourceId plan = +determineCoverageFor configuredPkg plan = Flag $ mapMaybe ( \case @@ -4488,15 +4489,18 @@ determineCoverageFor configuredPkgSourceId plan = $ Graph.toList $ InstallPlan.toGraph plan where - shouldCoverPkg elab@ElaboratedConfiguredPackage{elabModuleShape, elabPkgSourceId, elabLocalToProject} = + libDeps = elabLibDependencies configuredPkg + shouldCoverPkg elab@ElaboratedConfiguredPackage{elabModuleShape, elabPkgSourceId = pkgSID, elabLocalToProject} = elabLocalToProject && not (isIndefiniteOrInstantiation elabModuleShape) -- TODO(#9493): We can only cover libraries in the same package -- as the testsuite - && configuredPkgSourceId == elabPkgSourceId + && elabPkgSourceId configuredPkg == pkgSID -- Libraries only! We don't cover testsuite modules, so we never need -- the paths to their mix dirs. Furthermore, we do not install testsuites... && maybe False (\case CLibName{} -> True; CNotLibName{} -> False) (elabComponentName elab) + -- We only want coverage for libraries which are dependencies of the given one + && pkgSID `elem` map (confSrcId . fst) libDeps isIndefiniteOrInstantiation :: ModuleShape -> Bool isIndefiniteOrInstantiation = not . Set.null . modShapeRequires diff --git a/cabal-testsuite/PackageTests/Regression/T10046/aa.cabal b/cabal-testsuite/PackageTests/Regression/T10046/aa.cabal new file mode 100644 index 00000000000..4acc7154baa --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/aa.cabal @@ -0,0 +1,28 @@ +cabal-version: 3.0 +name: aa +version: 0.1.0.0 +license: NONE +build-type: Simple +extra-doc-files: CHANGELOG.md + +library + exposed-modules: MyLib + build-depends: base, template-haskell + hs-source-dirs: src + default-language: Haskell2010 + +test-suite bb-test + default-language: Haskell2010 + type: exitcode-stdio-1.0 + hs-source-dirs: testbb + main-is: Main.hs + build-depends: + base, aa + +test-suite aa-test + default-language: Haskell2010 + type: exitcode-stdio-1.0 + hs-source-dirs: test + main-is: Main.hs + build-depends: + base diff --git a/cabal-testsuite/PackageTests/Regression/T10046/app/Main.hs b/cabal-testsuite/PackageTests/Regression/T10046/app/Main.hs new file mode 100644 index 00000000000..76a9bdb5d48 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/app/Main.hs @@ -0,0 +1 @@ +main = pure () diff --git a/cabal-testsuite/PackageTests/Regression/T10046/cabal.project b/cabal-testsuite/PackageTests/Regression/T10046/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/Regression/T10046/cabal.test.hs b/cabal-testsuite/PackageTests/Regression/T10046/cabal.test.hs new file mode 100644 index 00000000000..9b857e2f09a --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/cabal.test.hs @@ -0,0 +1,6 @@ +import Test.Cabal.Prelude + +-- T10046 +main = cabalTest $ recordMode DoNotRecord $ do + out <- cabal' "test" ["all"] + assertOutputDoesNotContain "Failed to find the installed unit 'aa-0.1.0.0-inplace'" out diff --git a/cabal-testsuite/PackageTests/Regression/T10046/src/MyLib.hs b/cabal-testsuite/PackageTests/Regression/T10046/src/MyLib.hs new file mode 100644 index 00000000000..ca3816aaab9 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/src/MyLib.hs @@ -0,0 +1,11 @@ +{-# LANGUAGE TemplateHaskell #-} +module MyLib where + +import Control.Concurrent +import Language.Haskell.TH + +-- Must take longer to compile than the testsuite +$(do runIO $ + threadDelay (5*1000*1000) -- 5s + [d| data X |] + ) diff --git a/cabal-testsuite/PackageTests/Regression/T10046/test/Main.hs b/cabal-testsuite/PackageTests/Regression/T10046/test/Main.hs new file mode 100644 index 00000000000..76a9bdb5d48 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/test/Main.hs @@ -0,0 +1 @@ +main = pure () diff --git a/cabal-testsuite/PackageTests/Regression/T10046/testbb/Main.hs b/cabal-testsuite/PackageTests/Regression/T10046/testbb/Main.hs new file mode 100644 index 00000000000..76a9bdb5d48 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T10046/testbb/Main.hs @@ -0,0 +1 @@ +main = pure () diff --git a/changelog.d/issue-10046 b/changelog.d/issue-10046 new file mode 100644 index 00000000000..668d077271e --- /dev/null +++ b/changelog.d/issue-10046 @@ -0,0 +1,4 @@ +synopsis: Bug fix - Don't pass --coverage-for for non-dependency libs of testsuite +packages: cabal-install +issues: #10046 +prs: #10250