Skip to content

Commit 88863c1

Browse files
committed
Add check for upper bound on any package
fixes #8291 presumably this will make it nag at anyone for forgetting to add upper bounds to their packages. add changelog (presumably) wait what? move toDependencyVersionsMap to utils section add nicer error message simplify checking logic, add more comments only emit missing upper bounds if bigger then one don't add bound to internal libraries filter out self from the dependency map I think this is an external library so it needs an upper bound now? add test for multilib fix test suite by ignoring the warning ... probably not the best approach change link to pvp instead of parsonsmatt better wording on missing upper bound error remove spurious parenthesis change map creation from monad to list comprehension use foldmap to get rid of maybe, fix compile error rewrite from do notation to list comprehension fix test suite failing fix compile error factor out double filter call in a && expression
1 parent e1f5d8b commit 88863c1

File tree

7 files changed

+67
-32
lines changed

7 files changed

+67
-32
lines changed

Cabal-tests/tests/CheckTests.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ checkTests = testGroup "regressions"
4545
, checkTest "assoc-cpp-options.cabal"
4646
, checkTest "public-multilib-1.cabal"
4747
, checkTest "public-multilib-2.cabal"
48+
, checkTest "public-multilib-3.cabal"
4849
, checkTest "issue-6288-a.cabal"
4950
, checkTest "issue-6288-b.cabal"
5051
, checkTest "issue-6288-c.cabal"

Cabal-tests/tests/ParserTests/regressions/public-multilib-2.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ library
99
default-language: Haskell2010
1010
build-depends:
1111
, base ^>=4.14
12-
, somelib:internal
12+
, somelib:internal ^>=1.0
1313

1414
exposed-modules: Foo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cabal-version: 3.0
2+
name: public-multilib3
3+
version: 0
4+
synopsis: public-multilibs
5+
category: Tests
6+
license: MIT
7+
8+
library
9+
default-language: Haskell2010
10+
build-depends:
11+
, base ^>=4.14
12+
, somelib
13+
14+
exposed-modules: Foo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
No 'maintainer' field.
2+
No 'description' field.
3+
These packages miss upper bounds 'somelib' please add them using `cabal gen-bounds` for suggestions. For more information see: https://pvp.haskell.org/

Cabal/src/Distribution/PackageDescription/Check.hs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ data CheckExplanation =
227227
| UnknownArch [String]
228228
| UnknownCompiler [String]
229229
| BaseNoUpperBounds
230+
| MissingUpperBounds [PackageName]
230231
| SuspiciousFlagName [String]
231232
| DeclaredUsedFlags (Set FlagName) (Set FlagName)
232233
| NonASCIICustomField [String]
@@ -669,6 +670,12 @@ ppExplanation (UnknownArch unknownArches) =
669670
"Unknown architecture name " ++ commaSep (map quote unknownArches)
670671
ppExplanation (UnknownCompiler unknownImpls) =
671672
"Unknown compiler name " ++ commaSep (map quote unknownImpls)
673+
ppExplanation (MissingUpperBounds names) =
674+
"These packages miss upper bounds '"
675+
++ (intercalate "','" (unPackageName <$> names)) ++ "'"
676+
++ " please add them using `cabal gen-bounds` for suggestions."
677+
++ " For more information see: "
678+
++ " https://pvp.haskell.org/"
672679
ppExplanation BaseNoUpperBounds =
673680
"The dependency 'build-depends: base' does not specify an upper "
674681
++ "bound on the version number. Each major release of the 'base' "
@@ -1814,29 +1821,20 @@ checkCabalVersion pkg =
18141821
--
18151822
checkPackageVersions :: GenericPackageDescription -> [PackageCheck]
18161823
checkPackageVersions pkg =
1817-
catMaybes [
1818-
1819-
-- Check that the version of base is bounded above.
1820-
-- For example this bans "build-depends: base >= 3".
1821-
-- It should probably be "build-depends: base >= 3 && < 4"
1822-
-- which is the same as "build-depends: base == 3.*"
1823-
check (not (hasUpperBound baseDependency)) $
1824-
PackageDistInexcusable BaseNoUpperBounds
1825-
1826-
]
1824+
if length others > 0
1825+
then
1826+
PackageDistSuspiciousWarn (MissingUpperBounds others) : baseErrors
1827+
else
1828+
baseErrors
18271829
where
1828-
baseDependency = case typicalPkg pkg of
1829-
Right (pkg', _) | not (null baseDeps) ->
1830-
foldr intersectVersionRanges anyVersion baseDeps
1831-
where
1832-
baseDeps =
1833-
[ vr | Dependency pname vr _ <- allBuildDepends pkg'
1834-
, pname == mkPackageName "base" ]
1835-
1836-
-- Just in case finalizePD fails for any reason,
1837-
-- or if the package doesn't depend on the base package at all,
1838-
-- then we will just skip the check, since hasUpperBound noVersion = True
1839-
_ -> noVersion
1830+
baseErrors = PackageDistInexcusable BaseNoUpperBounds <$ bases
1831+
deps = toDependencyVersionsMap allBuildDepends pkg
1832+
-- base gets special treatment (it's more critical)
1833+
(bases, others) = partition (("base" ==) . unPackageName) $
1834+
[ name
1835+
| (name, vr) <- Map.toList deps
1836+
, not (hasUpperBound vr)
1837+
]
18401838

18411839
checkConditionals :: GenericPackageDescription -> [PackageCheck]
18421840
checkConditionals pkg =
@@ -2410,14 +2408,7 @@ checkSetupVersions pkg =
24102408
]
24112409
where
24122410
criticalPkgs = ["Cabal", "base"]
2413-
deps = case typicalPkg pkg of
2414-
Right (pkgs', _) ->
2415-
Map.fromListWith intersectVersionRanges
2416-
[ (pname, vr)
2417-
| sbi <- maybeToList $ setupBuildInfo pkgs'
2418-
, Dependency pname vr _ <- setupDepends sbi
2419-
]
2420-
_ -> Map.empty
2411+
deps = toDependencyVersionsMap (foldMap setupDepends . setupBuildInfo) pkg
24212412
emitError nm =
24222413
PackageDistInexcusable (UpperBoundSetup nm)
24232414

@@ -2456,6 +2447,24 @@ checkDuplicateModules pkg =
24562447
-- * Utils
24572448
-- ------------------------------------------------------------
24582449

2450+
toDependencyVersionsMap :: (PackageDescription -> [Dependency]) -> GenericPackageDescription -> Map PackageName VersionRange
2451+
toDependencyVersionsMap selectDependencies pkg = case typicalPkg pkg of
2452+
Right (pkgs', _) ->
2453+
let
2454+
self :: PackageName
2455+
self = pkgName $ package pkgs'
2456+
in
2457+
Map.fromListWith intersectVersionRanges $
2458+
[ (pname, vr)
2459+
| Dependency pname vr _ <- selectDependencies pkgs'
2460+
, pname /= self
2461+
]
2462+
-- Just in case finalizePD fails for any reason,
2463+
-- or if the package doesn't depend on the base package at all,
2464+
-- no deps is no checks.
2465+
_ -> Map.empty
2466+
2467+
24592468
quote :: String -> String
24602469
quote s = "'" ++ s ++ "'"
24612470

cabal-install/tests/UnitTests/Distribution/Solver/Modular/DSL.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ exAvSrcPkg ex =
478478
-- solver allows unknown extensions/languages when the compiler
479479
-- supports them.
480480
let checks = C.checkPackage (srcpkgDescription package) Nothing
481-
in filter (not . isUnknownLangExt) checks
481+
in filter (\x -> not (isMissingUpperBound x) && not (isUnknownLangExt x)) checks
482482
in if null pkgCheckErrors
483483
then package
484484
else error $ "invalid GenericPackageDescription for package "
@@ -671,6 +671,10 @@ exAvSrcPkg ex =
671671
C.UnknownExtensions {} -> True
672672
C.UnknownLanguages {} -> True
673673
_ -> False
674+
isMissingUpperBound :: C.PackageCheck -> Bool
675+
isMissingUpperBound pc = case C.explanation pc of
676+
C.MissingUpperBounds {} -> True
677+
_ -> False
674678

675679

676680
mkSimpleVersion :: ExamplePkgVersion -> C.Version

changelog.d/pr-8339

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
synopsis: Add check for upper bound on any dependency in cabal check
2+
report, list, init, fetch, info, upload, get.
3+
prs: #8339
4+
issues: #8291

0 commit comments

Comments
 (0)