Skip to content

Commit 3a96032

Browse files
authored
Merge pull request #3723 from grayjay/issue-3436
Consistently use the Cabal version picked by the dependency solver.
2 parents dd6cf0d + a94764e commit 3a96032

File tree

15 files changed

+157
-19
lines changed

15 files changed

+157
-19
lines changed

cabal-install/Distribution/Client/SetupWrapper.hs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ import Control.Applicative ( (<$>), (<*>) )
106106
import Data.Monoid ( mempty )
107107
#endif
108108
import Control.Monad ( when, unless )
109-
import Data.List ( foldl1' )
109+
import Data.List ( find, foldl1' )
110110
import Data.Maybe ( fromMaybe, isJust )
111111
import Data.Char ( isSpace )
112112
import Distribution.Client.Compat.ExecutablePath ( getExecutablePath )
@@ -389,26 +389,45 @@ externalSetupMethod verbosity options pkg bt mkargs = do
389389
Nothing -> getInstalledPackages verbosity
390390
comp (usePackageDB options') conf
391391

392-
cabalLibVersionToUse :: IO (Version, (Maybe ComponentId)
392+
-- Choose the version of Cabal to use if the setup script has a dependency on
393+
-- Cabal, and possibly update the setup script options. The version also
394+
-- determines how to filter the flags to Setup.
395+
--
396+
-- We first check whether the dependency solver has specified a Cabal version.
397+
-- If it has, we use the solver's version without looking at the installed
398+
-- package index (See issue #3436). Otherwise, we pick the Cabal version by
399+
-- checking 'useCabalSpecVersion', then the saved version, and finally the
400+
-- versions available in the index.
401+
--
402+
-- The version chosen here must match the one used in 'compileSetupExecutable'
403+
-- (See issue #3433).
404+
cabalLibVersionToUse :: IO (Version, Maybe ComponentId
393405
,SetupScriptOptions)
394406
cabalLibVersionToUse =
395-
case useCabalSpecVersion options of
396-
Just version -> do
407+
case find (hasCabal . snd) (useDependencies options) of
408+
Just (unitId, pkgId) -> do
409+
let version = pkgVersion pkgId
397410
updateSetupScript version bt
398-
writeFile setupVersionFile (show version ++ "\n")
399-
return (version, Nothing, options)
400-
Nothing -> do
401-
savedVer <- savedVersion
402-
case savedVer of
403-
Just version | version `withinRange` useCabalVersion options
404-
-> do updateSetupScript version bt
405-
-- Does the previously compiled setup executable still exist
406-
-- and is it up-to date?
407-
useExisting <- canUseExistingSetup version
408-
if useExisting
409-
then return (version, Nothing, options)
410-
else installedVersion
411-
_ -> installedVersion
411+
writeSetupVersionFile version
412+
return (version, Just unitId, options)
413+
Nothing ->
414+
case useCabalSpecVersion options of
415+
Just version -> do
416+
updateSetupScript version bt
417+
writeSetupVersionFile version
418+
return (version, Nothing, options)
419+
Nothing -> do
420+
savedVer <- savedVersion
421+
case savedVer of
422+
Just version | version `withinRange` useCabalVersion options
423+
-> do updateSetupScript version bt
424+
-- Does the previously compiled setup executable still exist
425+
-- and is it up-to date?
426+
useExisting <- canUseExistingSetup version
427+
if useExisting
428+
then return (version, Nothing, options)
429+
else installedVersion
430+
_ -> installedVersion
412431
where
413432
-- This check duplicates the checks in 'getCachedSetupExecutable' /
414433
-- 'compileSetupExecutable'. Unfortunately, we have to perform it twice
@@ -424,13 +443,20 @@ externalSetupMethod verbosity options pkg bt mkargs = do
424443
(&&) <$> setupProgFile `existsAndIsMoreRecentThan` setupHs
425444
<*> setupProgFile `existsAndIsMoreRecentThan` setupVersionFile
426445

446+
writeSetupVersionFile :: Version -> IO ()
447+
writeSetupVersionFile version =
448+
writeFile setupVersionFile (show version ++ "\n")
449+
450+
hasCabal (PackageIdentifier (PackageName "Cabal") _) = True
451+
hasCabal _ = False
452+
427453
installedVersion :: IO (Version, Maybe InstalledPackageId
428454
,SetupScriptOptions)
429455
installedVersion = do
430456
(comp, conf, options') <- configureCompiler options
431457
(version, mipkgid, options'') <- installedCabalVersion options' comp conf
432458
updateSetupScript version bt
433-
writeFile setupVersionFile (show version ++ "\n")
459+
writeSetupVersionFile version
434460
return (version, mipkgid, options'')
435461

436462
savedVersion :: IO (Maybe Version)

cabal-install/cabal-install.cabal

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ Extra-Source-Files:
2323
-- Do NOT edit this section manually; instead, run the script.
2424
-- BEGIN gen-extra-source-files
2525
tests/IntegrationTests/common.sh
26+
tests/IntegrationTests/custom-setup/Cabal-99998/Cabal.cabal
27+
tests/IntegrationTests/custom-setup/Cabal-99998/CabalMessage.hs
28+
tests/IntegrationTests/custom-setup/Cabal-99999/Cabal.cabal
29+
tests/IntegrationTests/custom-setup/Cabal-99999/CabalMessage.hs
30+
tests/IntegrationTests/custom-setup/custom-setup-without-cabal-defaultMain/Setup.hs
31+
tests/IntegrationTests/custom-setup/custom-setup-without-cabal-defaultMain/custom-setup-without-cabal-defaultMain.cabal
32+
tests/IntegrationTests/custom-setup/custom-setup-without-cabal/Setup.hs
33+
tests/IntegrationTests/custom-setup/custom-setup-without-cabal/custom-setup-without-cabal.cabal
34+
tests/IntegrationTests/custom-setup/custom-setup/Setup.hs
35+
tests/IntegrationTests/custom-setup/custom-setup/custom-setup.cabal
36+
tests/IntegrationTests/custom-setup/custom_setup_without_Cabal_doesnt_allow_Cabal_import.sh
37+
tests/IntegrationTests/custom-setup/custom_setup_without_Cabal_doesnt_require_Cabal.sh
38+
tests/IntegrationTests/custom-setup/installs_Cabal_as_setup_dep.sh
2639
tests/IntegrationTests/custom/custom_dep.sh
2740
tests/IntegrationTests/custom/custom_dep/client/B.hs
2841
tests/IntegrationTests/custom/custom_dep/client/Setup.hs
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: Cabal
2+
version: 99998
3+
build-type: Simple
4+
cabal-version: >= 1.2
5+
6+
library
7+
build-depends: base
8+
exposed-modules: CabalMessage
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module CabalMessage where
2+
3+
message = "This is Cabal-99998"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: Cabal
2+
version: 99999
3+
build-type: Simple
4+
cabal-version: >= 1.2
5+
6+
library
7+
build-depends: base
8+
exposed-modules: CabalMessage
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module CabalMessage where
2+
3+
message = "This is Cabal-99999"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Distribution.Simple
2+
3+
main = defaultMain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: custom-setup-without-cabal-defaultMain
2+
version: 1.0
3+
build-type: Custom
4+
cabal-version: >= 1.2
5+
6+
custom-setup
7+
setup-depends: base
8+
9+
library
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import System.Exit
2+
import System.IO
3+
4+
main = hPutStrLn stderr "My custom Setup" >> exitFailure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: custom-setup-without-cabal
2+
version: 1.0
3+
build-type: Custom
4+
cabal-version: >= 99999
5+
6+
custom-setup
7+
setup-depends: base
8+
9+
library
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import CabalMessage (message)
2+
import System.Exit
3+
import System.IO
4+
5+
main = hPutStrLn stderr message >> exitFailure
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: custom-setup
2+
version: 1.0
3+
build-type: Custom
4+
cabal-version: >= 99999
5+
6+
custom-setup
7+
setup-depends: base, Cabal >= 99999
8+
9+
library
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
. ./common.sh
2+
cd custom-setup-without-cabal-defaultMain
3+
4+
# This package has explicit setup dependencies that do not include Cabal.
5+
# Compilation should fail because Setup.hs imports Distribution.Simple.
6+
! cabal new-build custom-setup-without-cabal-defaultMain > output 2>&1
7+
cat output
8+
grep -q "\(Could not find module\|Failed to load interface for\).*Distribution\\.Simple" output \
9+
|| die "Should not have been able to import Cabal"
10+
11+
grep -q "It is a member of the hidden package .*Cabal-" output \
12+
|| die "Cabal should be available"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
. ./common.sh
2+
cd custom-setup-without-cabal
3+
4+
# This package has explicit setup dependencies that do not include Cabal.
5+
# new-build should try to build it, even though the cabal-version cannot be
6+
# satisfied by an installed version of Cabal (cabal-version: >= 99999). However,
7+
# configure should fail because Setup.hs just prints an error message and exits.
8+
! cabal new-build custom-setup-without-cabal > output 2>&1
9+
cat output
10+
grep -q "My custom Setup" output \
11+
|| die "Expected output from custom Setup"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Regression test for issue #3436
2+
3+
. ./common.sh
4+
cabal sandbox init
5+
cabal install ./Cabal-99998
6+
cabal sandbox add-source Cabal-99999
7+
8+
# Install custom-setup, which has a setup dependency on Cabal-99999.
9+
# cabal should build the setup script with Cabal-99999, but then
10+
# configure should fail because Setup just prints an error message
11+
# imported from Cabal and exits.
12+
! cabal install custom-setup/ > output 2>&1
13+
14+
cat output
15+
grep -q "This is Cabal-99999" output || die "Expected output from Cabal-99999"

0 commit comments

Comments
 (0)