From 1c55df463d4fe3a7af37d4a6fd0a65f83bacf5e2 Mon Sep 17 00:00:00 2001 From: Matthew Pickering Date: Tue, 14 Nov 2023 11:05:04 +0000 Subject: [PATCH] Fix assertion failure when combining build-tool-depends and --enable-documentation The `setDocumentation` function was modifying the elaborated package after the hash was computed. This led to the assertion failing as the computed hash was different to what was computed in the initial install plan. Therefore in order to fix this we either needed to: 1. Set elabBuildHaddocks = False at the point where the hash is initially computed. 2. Verify that elabBuildHaddocks = True will not lead to unexpected results. The latter has been implemented. The elabBuildHaddocks option is only consulted in `hasValidHaddockTargets`, at which point documentation building the executable component is disabled because elabHaddockExecutables is False. In the added test we ensure this by checking that we didn't build documentation for the executable which is built because of build-tool-depends. Fixes #6006 #8313 --- .../Distribution/Client/ProjectPlanning.hs | 34 +++++-------------- .../PackageTests/HaddockBuildDepends/a.cabal | 10 ++++++ .../HaddockBuildDepends/cabal.out | 27 +++++++++++++++ .../HaddockBuildDepends/cabal.project | 1 + .../HaddockBuildDepends/cabal.test.hs | 22 ++++++++++++ .../HaddockBuildDepends/repo/exe-1/Main.hs | 3 ++ .../HaddockBuildDepends/repo/exe-1/exe.cabal | 12 +++++++ .../HaddockBuildDepends/repo/lib-1/Lib.hs | 1 + .../HaddockBuildDepends/repo/lib-1/lib.cabal | 13 +++++++ .../HaddockBuildDepends/src/MyLib.hs | 4 +++ 10 files changed, 101 insertions(+), 26 deletions(-) create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/a.cabal create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.out create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.project create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.test.hs create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/Main.hs create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/exe.cabal create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/Lib.hs create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/lib.cabal create mode 100644 cabal-testsuite/PackageTests/HaddockBuildDepends/src/MyLib.hs diff --git a/cabal-install/src/Distribution/Client/ProjectPlanning.hs b/cabal-install/src/Distribution/Client/ProjectPlanning.hs index 3cb0d8033e8..5cb04eaf56b 100644 --- a/cabal-install/src/Distribution/Client/ProjectPlanning.hs +++ b/cabal-install/src/Distribution/Client/ProjectPlanning.hs @@ -258,7 +258,7 @@ sanityCheckElaboratedConfiguredPackage -> a -> a sanityCheckElaboratedConfiguredPackage - _sharedConfig + sharedConfig elab@ElaboratedConfiguredPackage{..} = ( case elabPkgOrComp of ElabPackage pkg -> sanityCheckElaboratedPackage elab pkg @@ -273,10 +273,12 @@ sanityCheckElaboratedConfiguredPackage -- 'installedPackageId' we assigned is consistent with -- the 'hashedInstalledPackageId' we would compute from -- the elaborated configured package - -- . assert (isInplaceBuildStyle elabBuildStyle || - -- elabComponentId == hashedInstalledPackageId - -- (packageHashInputs sharedConfig elab)) - + . assert + ( isInplaceBuildStyle elabBuildStyle + || elabComponentId + == hashedInstalledPackageId + (packageHashInputs sharedConfig elab) + ) -- the stanzas explicitly disabled should not be available . assert ( optStanzaSetNull $ @@ -3293,9 +3295,7 @@ pruneInstallPlanPass1 pkgs prune :: ElaboratedConfiguredPackage -> PrunedPackage prune elab = PrunedPackage elab' (pruneOptionalDependencies elab') where - elab' = - setDocumentation $ - addOptionalStanzas elab + elab' = addOptionalStanzas elab graph = Graph.fromDistinctList pkgs' @@ -3444,24 +3444,6 @@ pruneInstallPlanPass1 pkgs <> optionalStanzasWithDepsAvailable availablePkgs elab pkg addOptionalStanzas elab = elab - setDocumentation :: ElaboratedConfiguredPackage -> ElaboratedConfiguredPackage - setDocumentation elab@ElaboratedConfiguredPackage{elabPkgOrComp = ElabComponent comp} = - elab - { elabBuildHaddocks = - elabBuildHaddocks elab && documentationEnabled (compSolverName comp) elab - } - where - documentationEnabled c = - case c of - CD.ComponentLib -> const True - CD.ComponentSubLib _ -> elabHaddockInternal - CD.ComponentFLib _ -> elabHaddockForeignLibs - CD.ComponentExe _ -> elabHaddockExecutables - CD.ComponentTest _ -> elabHaddockTestSuites - CD.ComponentBench _ -> elabHaddockBenchmarks - CD.ComponentSetup -> const False - setDocumentation elab = elab - -- Calculate package dependencies but cut out those needed only by -- optional stanzas that we've determined we will not enable. -- These pruned deps are not persisted in this pass since they're based on diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/a.cabal b/cabal-testsuite/PackageTests/HaddockBuildDepends/a.cabal new file mode 100644 index 00000000000..552ef337ea6 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/a.cabal @@ -0,0 +1,10 @@ +name: a +version: 0.1.0.0 +build-type: Simple +cabal-version: >= 1.10 + +library + exposed-modules: MyLib + build-depends: base, lib + hs-source-dirs: src + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.out b/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.out new file mode 100644 index 00000000000..bb6754c14a6 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.out @@ -0,0 +1,27 @@ +# cabal v2-update +Downloading the latest package list from test-local-repo +# cabal build +Resolving dependencies... +Build profile: -w ghc- -O1 +In order, the following will be built: + - exe-1 (exe:exe) (requires build) + - lib-1 (lib) (requires build) + - a-0.1.0.0 (lib) (first run) +Configuring executable 'exe' for exe-1... +Preprocessing executable 'exe' for exe-1... +Building executable 'exe' for exe-1... +Installing executable exe in +Warning: The directory /cabal.dist/home/.cabal/store/ghc-/incoming/new-/cabal.dist/home/.cabal/store/ghc-/-/bin is not in the system search path. +Configuring library for lib-1... +Preprocessing library for lib-1... +Building library for lib-1... +Preprocessing library for lib-1... +Running Haddock on library for lib-1... +Documentation created: dist/doc/html/lib/ +Installing library in +Configuring library for a-0.1.0.0... +Preprocessing library for a-0.1.0.0... +Building library for a-0.1.0.0... +Preprocessing library for a-0.1.0.0... +Running Haddock on library for a-0.1.0.0... +Documentation created: /cabal.dist/work/dist/build//ghc-/a-0.1.0.0/doc/html/a/ diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.project b/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.test.hs b/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.test.hs new file mode 100644 index 00000000000..28821c5e858 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/cabal.test.hs @@ -0,0 +1,22 @@ +import Test.Cabal.Prelude + + +main = cabalTest . withRepo "repo" $ do + cabal "build" ["--enable-documentation"] + + env <- getTestEnv + let storeDir = testCabalDir env "store" + + -- Check properties of executable component + libDir <- liftIO $ findDependencyInStore storeDir "exe" + -- Documentation is enabled.. + assertFileDoesContain (libDir "cabal-hash.txt") "documentation: True" + -- But not built + shouldDirectoryNotExist ( libDir "share" "doc" ) + + -- Check properties of library + libDir <- liftIO $ findDependencyInStore storeDir "lib" + -- Documentation is enabled.. + assertFileDoesContain (libDir "cabal-hash.txt") "documentation: True" + -- and has been built + shouldDirectoryExist ( libDir "share" "doc" ) diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/Main.hs b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/Main.hs new file mode 100644 index 00000000000..de106fe48f9 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/Main.hs @@ -0,0 +1,3 @@ +module Main where + +main = return () diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/exe.cabal b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/exe.cabal new file mode 100644 index 00000000000..a5c1b537d7b --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/exe-1/exe.cabal @@ -0,0 +1,12 @@ +name: exe +version: 1 +license: BSD3 +author: Edward Z. Yang +maintainer: ezyang@cs.stanford.edu +build-type: Simple +cabal-version: 2.0 + +executable exe + build-depends: base + main-is: Main.hs + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/Lib.hs b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/Lib.hs new file mode 100644 index 00000000000..6d85a26fe10 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/Lib.hs @@ -0,0 +1 @@ +module Lib where diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/lib.cabal b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/lib.cabal new file mode 100644 index 00000000000..526338f344f --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/repo/lib-1/lib.cabal @@ -0,0 +1,13 @@ +name: lib +version: 1 +license: BSD3 +author: Edward Z. Yang +maintainer: ezyang@cs.stanford.edu +build-type: Simple +cabal-version: 2.0 + +library + build-depends: base + build-tool-depends: exe:exe + exposed-modules: Lib + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/HaddockBuildDepends/src/MyLib.hs b/cabal-testsuite/PackageTests/HaddockBuildDepends/src/MyLib.hs new file mode 100644 index 00000000000..bbaef0a6d64 --- /dev/null +++ b/cabal-testsuite/PackageTests/HaddockBuildDepends/src/MyLib.hs @@ -0,0 +1,4 @@ +module MyLib (someFunc) where + +someFunc :: IO () +someFunc = return ()