Skip to content

Commit 8730d5b

Browse files
authored
Merge pull request #11571 from tbidne/tbidne/11518-check
Add check for "dotlines" with cabal 3.0+
2 parents c5c2072 + 2621a7f commit 8730d5b

13 files changed

Lines changed: 237 additions & 3 deletions

File tree

Cabal-syntax/src/Distribution/FieldGrammar/Parsec.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module Distribution.FieldGrammar.Parsec
6666
, runFieldParser
6767
, runFieldParser'
6868
, fieldLinesToStream
69+
, freeTextIgnoreDotlineVers
6970
) where
7071

7172
import Distribution.Compat.Newtype
@@ -234,7 +235,7 @@ instance FieldGrammar Parsec ParsecFieldGrammar where
234235

235236
parseOne v (MkNamelessField pos fls)
236237
| null fls = pure Nothing
237-
| v >= CabalSpecV3_0 = pure (Just (fieldlinesToFreeText3 pos fls))
238+
| v >= freeTextIgnoreDotlineVers = pure (Just (fieldlinesToFreeText3 pos fls))
238239
| otherwise = pure (Just (fieldlinesToFreeText fls))
239240

240241
freeTextFieldDef fn _ = ParsecFG (Set.singleton fn) Set.empty parser
@@ -249,7 +250,7 @@ instance FieldGrammar Parsec ParsecFieldGrammar where
249250

250251
parseOne v (MkNamelessField pos fls)
251252
| null fls = pure ""
252-
| v >= CabalSpecV3_0 = pure (fieldlinesToFreeText3 pos fls)
253+
| v >= freeTextIgnoreDotlineVers = pure (fieldlinesToFreeText3 pos fls)
253254
| otherwise = pure (fieldlinesToFreeText fls)
254255

255256
-- freeTextFieldDefST = defaultFreeTextFieldDefST
@@ -267,7 +268,7 @@ instance FieldGrammar Parsec ParsecFieldGrammar where
267268
[] -> pure mempty
268269
[FieldLine _ bs] -> pure (ShortText.unsafeFromUTF8BS bs)
269270
_
270-
| v >= CabalSpecV3_0 -> pure (ShortText.toShortText $ fieldlinesToFreeText3 pos fls)
271+
| v >= freeTextIgnoreDotlineVers -> pure (ShortText.toShortText $ fieldlinesToFreeText3 pos fls)
271272
| otherwise -> pure (ShortText.toShortText $ fieldlinesToFreeText fls)
272273

273274
monoidalFieldAla fn _pack _extract = ParsecFG (Set.singleton fn) Set.empty parser
@@ -411,6 +412,11 @@ fieldlinesToFreeText fls = intercalate "\n" (map go fls)
411412
where
412413
s = trim (fromUTF8BS bs)
413414

415+
-- | Cabal version where we switch from the old free text parser that had
416+
-- special logic for "dotlines" to a new parser that has no such logic.
417+
freeTextIgnoreDotlineVers :: CabalSpecVersion
418+
freeTextIgnoreDotlineVers = CabalSpecV3_0
419+
414420
fieldlinesToFreeText3 :: Position -> [FieldLine Position] -> String
415421
fieldlinesToFreeText3 _ [] = ""
416422
fieldlinesToFreeText3 _ [FieldLine _ bs] = fromUTF8BS bs

Cabal/src/Distribution/PackageDescription/Check.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ import Distribution.Compat.Prelude
4747
import Prelude ()
4848

4949
import Data.List (group)
50+
import qualified Data.List as L
5051
import Distribution.CabalSpecVersion
5152
import Distribution.Compat.Lens
5253
import Distribution.Compiler
54+
import Distribution.FieldGrammar.Parsec (freeTextIgnoreDotlineVers)
5355
import Distribution.License
5456
import Distribution.ModuleName (toFilePath)
5557
import Distribution.Package
@@ -85,6 +87,7 @@ import qualified System.FilePath.Windows as FilePath.Windows (isValid)
8587

8688
import qualified Data.Set as Set
8789
import qualified Distribution.Utils.ShortText as ShortText
90+
import qualified Distribution.Utils.String as String
8891

8992
import qualified Distribution.Types.GenericPackageDescription.Lens as L
9093

@@ -464,6 +467,20 @@ checkPackageDescription
464467
)
465468
(PackageDistSuspicious ShortDesc)
466469

470+
-- § Freeform fields no longer include "dotlines". Generally taken from
471+
-- Distribution.PackageDescription.FieldGrammar fields that use some
472+
-- freeTextField parser.
473+
checkDotline "author" _author_
474+
checkDotline "bug-reports" _bugReports_
475+
checkDotline "category" category_
476+
checkDotline "copyright" _copyright_
477+
checkDotline "description" description_
478+
checkDotline "homepage" _homepage_
479+
checkDotline "maintainer" maintainer_
480+
checkDotline "package-url" _pkgUrl_
481+
checkDotline "stability" _stability_
482+
checkDotline "synopsis" synopsis_
483+
467484
-- § Paths.
468485
mapM_ (checkPath False "extra-source-files" PathKindGlob . getSymbolicPath) extraSrcFiles_
469486
mapM_ (checkPath False "extra-tmp-files" PathKindFile . getSymbolicPath) extraTmpFiles_
@@ -570,6 +587,19 @@ checkPackageDescription
570587
in tellP (PackageDistInexcusable (InvalidTestWith dep))
571588
)
572589

590+
-- | Issue a warning if the text contains any "dotlines".
591+
checkDotline :: Monad m => String -> ShortText.ShortText -> CheckM m ()
592+
checkDotline fieldName fieldVal = do
593+
checkSpecVerGte
594+
freeTextIgnoreDotlineVers
595+
(hasDotline fieldVal)
596+
(PackageDistSuspiciousWarn $ FreeTextDotline fieldName)
597+
where
598+
hasDotline =
599+
L.any ((== ".") . String.trim)
600+
. L.lines
601+
. ShortText.fromShortText
602+
573603
checkSetupBuildInfo :: Monad m => Maybe SetupBuildInfo -> CheckM m ()
574604
checkSetupBuildInfo Nothing = return ()
575605
checkSetupBuildInfo (Just (SetupBuildInfo ds _)) = do

Cabal/src/Distribution/PackageDescription/Check/Monad.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Distribution.PackageDescription.Check.Monad
3939
, liftInt
4040
, tellP
4141
, checkSpecVer
42+
, checkSpecVerGte
4243
) where
4344

4445
import Distribution.Compat.Prelude
@@ -369,3 +370,16 @@ checkSpecVer
369370
checkSpecVer vc cond c = do
370371
vp <- asksCM ccSpecVersion
371372
unless (vp >= vc) (checkP cond c)
373+
374+
-- | Like 'checkSpecVer', except performs the check when our
375+
-- spec version >= the param.
376+
checkSpecVerGte
377+
:: Monad m
378+
=> CabalSpecVersion -- Perform this check only if our
379+
-- spec version is >= than this.
380+
-> Bool -- Check condition.
381+
-> PackageCheck -- Check message.
382+
-> CheckM m ()
383+
checkSpecVerGte vc cond c = do
384+
vp <- asksCM ccSpecVersion
385+
when (vp >= vc) (checkP cond c)

Cabal/src/Distribution/PackageDescription/Check/Warning.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ data CheckExplanation
170170
| UnknownExtensions [String]
171171
| LanguagesAsExtension [String]
172172
| DeprecatedExtensions [(Extension, Maybe Extension)]
173+
| FreeTextDotline String
173174
| MissingFieldCategory
174175
| MissingFieldMaintainer
175176
| MissingFieldSynopsis
@@ -337,6 +338,7 @@ data CheckExplanationID
337338
| CIUnknownExtensions
338339
| CILanguagesAsExtension
339340
| CIDeprecatedExtensions
341+
| CIFreeTextDotline
340342
| CIMissingFieldCategory
341343
| CIMissingFieldMaintainer
342344
| CIMissingFieldSynopsis
@@ -483,6 +485,7 @@ checkExplanationId (UnknownLanguages{}) = CIUnknownLanguages
483485
checkExplanationId (UnknownExtensions{}) = CIUnknownExtensions
484486
checkExplanationId (LanguagesAsExtension{}) = CILanguagesAsExtension
485487
checkExplanationId (DeprecatedExtensions{}) = CIDeprecatedExtensions
488+
checkExplanationId (FreeTextDotline{}) = CIFreeTextDotline
486489
checkExplanationId (MissingFieldCategory{}) = CIMissingFieldCategory
487490
checkExplanationId (MissingFieldMaintainer{}) = CIMissingFieldMaintainer
488491
checkExplanationId (MissingFieldSynopsis{}) = CIMissingFieldSynopsis
@@ -636,6 +639,7 @@ ppCheckExplanationId CIUnknownLanguages = "unknown-languages"
636639
ppCheckExplanationId CIUnknownExtensions = "unknown-extension"
637640
ppCheckExplanationId CILanguagesAsExtension = "languages-as-extensions"
638641
ppCheckExplanationId CIDeprecatedExtensions = "deprecated-extensions"
642+
ppCheckExplanationId CIFreeTextDotline = "free-text-dotline"
639643
ppCheckExplanationId CIMissingFieldCategory = "no-category"
640644
ppCheckExplanationId CIMissingFieldMaintainer = "no-maintainer"
641645
ppCheckExplanationId CIMissingFieldSynopsis = "no-synopsis"
@@ -910,6 +914,11 @@ ppExplanation (DeprecatedExtensions ourDeprecatedExtensions) =
910914
++ "'."
911915
| (ext, Just replacement) <- ourDeprecatedExtensions
912916
]
917+
ppExplanation (FreeTextDotline field) =
918+
"Empty lines with a dot '.' in field '"
919+
++ field
920+
++ "' are unnecessary for creating newlines "
921+
++ "(and treated literally) in cabal 3.0+"
913922
ppExplanation MissingFieldCategory = "No 'category' field."
914923
ppExplanation MissingFieldMaintainer = "No 'maintainer' field."
915924
ppExplanation MissingFieldSynopsis = "No 'synopsis' field."
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# cabal v2-build
2+
Resolving dependencies...
3+
Build profile: -w ghc-<GHCVER> -O1
4+
In order, the following would be built:
5+
- main-0.1 (lib) (first run)
6+
7+
# cabal check
8+
These warnings may cause trouble when distributing the package:
9+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'author' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
10+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'bug-reports' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
11+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'category' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
12+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'copyright' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
13+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'description' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
14+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'homepage' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
15+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'maintainer' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
16+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'package-url' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
17+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'stability' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
18+
Warning: [free-text-dotline] Empty lines with a dot '.' in field 'synopsis' are unnecessary for creating newlines (and treated literally) in cabal 3.0+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
cabal "v2-build" ["--dry-run"]
5+
6+
cabal "check" []
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cabal-version: 3.0
2+
3+
name: main
4+
version: 0.1
5+
build-type: Simple
6+
category:
7+
Some
8+
.
9+
Category
10+
author:
11+
Joe
12+
.
13+
Author
14+
stability:
15+
Some
16+
.
17+
Stability
18+
homepage:
19+
Some
20+
.
21+
homepace
22+
package-url:
23+
Some
24+
.
25+
url
26+
maintainer:
27+
Joe
28+
.
29+
Maintainer
30+
copyright:
31+
Some
32+
.
33+
text
34+
bug-reports:
35+
Some
36+
.
37+
bugs
38+
synopsis:
39+
Test input
40+
.
41+
with
42+
.
43+
lines
44+
description:
45+
.
46+
Some . description
47+
.
48+
spanning .
49+
.
50+
multiple lines.
51+
.
52+
license: BSD-3-Clause
53+
54+
library
55+
exposed-modules: Lib
56+
build-depends: base <6
57+
default-language: Haskell2010
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Lib where
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# cabal v2-build
2+
Resolving dependencies...
3+
Build profile: -w ghc-<GHCVER> -O1
4+
In order, the following would be built:
5+
- main-0.1 (lib) (first run)
6+
7+
# cabal check
8+
No errors or warnings could be found in the package.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
cabal "v2-build" ["--dry-run"]
5+
6+
cabal "check" []

0 commit comments

Comments
 (0)