Skip to content

Commit 19d9738

Browse files
authored
Merge pull request #4058 from dcoutts/issue3932
Refine the fix for requiring Cabal > 1.20 for Setup.hs Fixes #4196
2 parents 6a79d66 + c74696c commit 19d9738

File tree

6 files changed

+60
-39
lines changed

6 files changed

+60
-39
lines changed

cabal-install/Distribution/Client/Dependency.hs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module Distribution.Client.Dependency (
5656
removeLowerBounds,
5757
removeUpperBounds,
5858
addDefaultSetupDependencies,
59+
addSetupCabalMinVersionConstraint,
5960
) where
6061

6162
import Distribution.Solver.Modular
@@ -84,9 +85,9 @@ import Distribution.PackageDescription.Configuration
8485
import Distribution.Client.PackageUtils
8586
( externalBuildDepends )
8687
import Distribution.Version
87-
( mkVersion, VersionRange, anyVersion, thisVersion, orLaterVersion
88-
, withinRange, simplifyVersionRange
89-
, removeLowerBound, removeUpperBound )
88+
( Version, mkVersion
89+
, VersionRange, anyVersion, thisVersion, orLaterVersion, withinRange
90+
, simplifyVersionRange, removeLowerBound, removeUpperBound )
9091
import Distribution.Compiler
9192
( CompilerInfo(..) )
9293
import Distribution.System
@@ -466,6 +467,22 @@ addDefaultSetupDependencies defaultSetupDeps params =
466467
gpkgdesc = packageDescription srcpkg
467468
pkgdesc = PD.packageDescription gpkgdesc
468469

470+
-- | There If a package has a custom setup then we need to add a setup-depends
471+
-- on Cabal. For now it's easier to add this unconditionally. Once
472+
-- qualified constraints land we can turn this into a custom setup
473+
-- only constraint.
474+
--
475+
addSetupCabalMinVersionConstraint :: Version
476+
-> DepResolverParams -> DepResolverParams
477+
addSetupCabalMinVersionConstraint minVersion =
478+
addConstraints
479+
[ LabeledPackageConstraint
480+
(PackageConstraintVersion cabalPkgname (orLaterVersion minVersion))
481+
ConstraintSetupCabalMinVersion
482+
]
483+
where
484+
cabalPkgname = mkPackageName "Cabal"
485+
469486

470487
upgradeDependencies :: DepResolverParams -> DepResolverParams
471488
upgradeDependencies = setPreferenceDefault PreferAllLatest

cabal-install/Distribution/Client/ProjectPlanning.hs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -952,23 +952,25 @@ planPackages comp platform solver SolverSettings{..}
952952
. PD.packageDescription
953953
. packageDescription)
954954

955+
. addSetupCabalMinVersionConstraint (mkVersion [1,20])
956+
-- While we can talk to older Cabal versions (we need to be able to
957+
-- do so for custom Setup scripts that require older Cabal lib
958+
-- versions), we have problems talking to some older versions that
959+
-- don't support certain features.
960+
--
961+
-- For example, Cabal-1.16 and older do not know about build targets.
962+
-- Even worse, 1.18 and older only supported the --constraint flag
963+
-- with source package ids, not --dependency with installed package
964+
-- ids. That is bad because we cannot reliably select the right
965+
-- dependencies in the presence of multiple instances (i.e. the
966+
-- store). See issue #3932. So we require Cabal 1.20 as a minimum.
967+
955968
. addPreferences
956969
-- preferences from the config file or command line
957970
[ PackageVersionPreference name ver
958971
| Dependency name ver <- solverSettingPreferences ]
959972

960973
. addConstraints
961-
962-
-- If a package has a custom setup then we need to add a setup-depends
963-
-- on Cabal. For now it's easier to add this unconditionally. Once
964-
-- qualified constraints land we can turn this into a custom setup
965-
-- only constraint.
966-
--
967-
-- TODO: use a qualified constraint
968-
[ LabeledPackageConstraint (PackageConstraintVersion cabalPkgname
969-
(orLaterVersion (mkVersion [1,20])))
970-
ConstraintNewBuildCustomSetupLowerBoundCabal
971-
] . addConstraints
972974
-- version constraints from the config file or command line
973975
[ LabeledPackageConstraint (userToPackageConstraint pc) src
974976
| (pc, src) <- solverSettingConstraints ]
@@ -2397,6 +2399,10 @@ packageSetupScriptStyle pkg
23972399
-- we still need to distinguish the case of explicit and implict setup deps.
23982400
-- See 'rememberImplicitSetupDeps'.
23992401
--
2402+
-- Note in addition to adding default setup deps, we also use
2403+
-- 'addSetupCabalMinVersionConstraint' (in 'planPackages') to require
2404+
-- @Cabal >= 1.20@ for Setup scripts.
2405+
--
24002406
defaultSetupDeps :: Compiler -> Platform
24012407
-> PD.PackageDescription
24022408
-> Maybe [Dependency]
@@ -2415,35 +2421,19 @@ defaultSetupDeps compiler platform pkg =
24152421
where
24162422
-- The Cabal dep is slightly special:
24172423
-- * We omit the dep for the Cabal lib itself, since it bootstraps.
2418-
-- * We constrain it to be >= 1.18 < 2
2424+
-- * We constrain it to be < 1.25
24192425
--
2420-
-- Note: cabalCompatMinVer only gets applied WHEN WE ARE ADDING a
2421-
-- default setup build info, i.e., when there is no custom-setup
2422-
-- stanza. If there is a custom-setup stanza, this codepath never gets
2423-
-- invoked (that's why there's an error case for
2424-
-- SetupCustomExplicitDeps).
2426+
-- Note: we also add a global constraint to require Cabal >= 1.20
2427+
-- for Setup scripts (see use addSetupCabalMinVersionConstraint).
24252428
--
2426-
-- One way we could solve this problem is by also modifying
2427-
-- custom-setup stanzas when they exist, but we're going to take a
2428-
-- different approach: add an extra constraint on Cabal globally to
2429-
-- make sure the solver respects it regardless of whether or not there
2430-
-- is an explicit setup build info or not. See planPackages.
2431-
cabalConstraint = orLaterVersion cabalCompatMinVer
2432-
`intersectVersionRanges`
2433-
orLaterVersion (PD.specVersion pkg)
2429+
cabalConstraint = orLaterVersion (PD.specVersion pkg)
24342430
`intersectVersionRanges`
24352431
earlierVersion cabalCompatMaxVer
24362432
-- The idea here is that at some point we will make significant
24372433
-- breaking changes to the Cabal API that Setup.hs scripts use.
24382434
-- So for old custom Setup scripts that do not specify explicit
24392435
-- constraints, we constrain them to use a compatible Cabal version.
24402436
cabalCompatMaxVer = mkVersion [1,25]
2441-
-- In principle we can talk to any old Cabal version, and we need to
2442-
-- be able to do that for custom Setup scripts that require older
2443-
-- Cabal lib versions. However in practice we have currently have
2444-
-- problems with Cabal-1.16. (1.16 does not know about build targets)
2445-
-- If this is fixed we can relax this constraint.
2446-
cabalCompatMinVer = mkVersion [1,18]
24472437

24482438
-- For other build types (like Simple) if we still need to compile an
24492439
-- external Setup.hs, it'll be one of the simple ones that only depends
@@ -2458,7 +2448,7 @@ defaultSetupDeps compiler platform pkg =
24582448
SetupNonCustomInternalLib -> Just []
24592449

24602450
-- This case gets ruled out by the caller, planPackages, see the note
2461-
-- above in the SetupCustomIplicitDeps case.
2451+
-- above in the SetupCustomImplicitDeps case.
24622452
SetupCustomExplicitDeps ->
24632453
error $ "defaultSetupDeps: called for a package with explicit "
24642454
++ "setup deps: " ++ display (packageId pkg)

cabal-install/Distribution/Solver/Modular/Package.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Distribution.Solver.Modular.Package
1212
, instI
1313
, makeIndependent
1414
, primaryPP
15+
, setupPP
1516
, showI
1617
, showPI
1718
, unPN
@@ -92,6 +93,14 @@ primaryPP (PackagePath _ns q) = go q
9293
go (Setup _) = False
9394
go (Exe _ _) = False
9495

96+
-- | Is the package a dependency of a setup script. This is used establish
97+
-- whether or not certain constraints should apply to this dependency
98+
-- (grep 'setupPP' to see the use sites).
99+
--
100+
setupPP :: PackagePath -> Bool
101+
setupPP (PackagePath _ns (Setup _)) = True
102+
setupPP (PackagePath _ns _) = False
103+
95104
-- | Create artificial parents for each of the package names, making
96105
-- them all independent.
97106
makeIndependent :: [PN] -> [QPN]

cabal-install/Distribution/Solver/Modular/Preference.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ processPackageConstraintP pp _ _ (LabeledPackageConstraint _ src) r
156156
| src == ConstraintSourceUserTarget && not (primaryPP pp) = r
157157
-- the constraints arising from targets, like "foo-1.0" only apply to
158158
-- the main packages in the solution, they don't constrain setup deps
159+
| src == ConstraintSetupCabalMinVersion && not (setupPP pp) = r
160+
-- the internal constraints on the Setup.hs CLI version don't apply to
161+
-- the main packages in the solution, they only constrain setup deps
159162

160163
processPackageConstraintP _ c i (LabeledPackageConstraint pc src) r = go i pc
161164
where

cabal-install/Distribution/Solver/Types/ConstraintSource.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ data ConstraintSource =
4646
-- | The source of the constraint is not specified.
4747
| ConstraintSourceUnknown
4848

49-
-- | Custom setup requires a minimum lower bound on Cabal
50-
| ConstraintNewBuildCustomSetupLowerBoundCabal
49+
-- | An internal constraint due to compatability issues with the Setup.hs
50+
-- command line interface requires a minimum lower bound on Cabal
51+
| ConstraintSetupCabalMinVersion
5152
deriving (Eq, Show, Generic)
5253

5354
instance Binary ConstraintSource
@@ -71,4 +72,5 @@ showConstraintSource ConstraintSourceFreeze = "cabal freeze"
7172
showConstraintSource ConstraintSourceConfigFlagOrTarget =
7273
"config file, command line flag, or user target"
7374
showConstraintSource ConstraintSourceUnknown = "unknown source"
74-
showConstraintSource ConstraintNewBuildCustomSetupLowerBoundCabal = "new-build's support of Custom Setup (issue #3932)"
75+
showConstraintSource ConstraintSetupCabalMinVersion =
76+
"minimum version of Cabal used by Setup.hs"

cabal-testsuite/PackageTests/Regression/T3932/cabal.test.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ main = cabalTest $
77
-- won't be reported.
88
withRepo "repo" $ do
99
fails (cabal' "new-build" []) >>=
10-
assertOutputContains "(issue #3932) requires >=1.20"
10+
assertOutputContains "Setup.hs requires >=1.20"

0 commit comments

Comments
 (0)