forked from haskell/cabal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck.hs
1113 lines (1027 loc) · 38.5 KB
/
Check.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- |
-- Module : Distribution.PackageDescription.Check
-- Copyright : Lennart Kolmodin 2008, Francesco Ariis 2022
-- License : BSD3
--
-- Maintainer : cabal-devel@haskell.org
-- Portability : portable
--
-- This has code for checking for various problems in packages. There is one
-- set of checks that just looks at a 'PackageDescription' in isolation and
-- another set of checks that also looks at files in the package. Some of the
-- checks are basic sanity checks, others are portability standards that we'd
-- like to encourage. There is a 'PackageCheck' type that distinguishes the
-- different kinds of checks so we can see which ones are appropriate to report
-- in different situations. This code gets used when configuring a package when
-- we consider only basic problems. The higher standard is used when
-- preparing a source tarball and by Hackage when uploading new packages. The
-- reason for this is that we want to hold packages that are expected to be
-- distributed to a higher standard than packages that are only ever expected
-- to be used on the author's own environment.
module Distribution.PackageDescription.Check
( -- * Package Checking
CheckExplanation (..)
, CheckExplanationID
, CheckExplanationIDString
, PackageCheck (..)
, checkPackage
, checkConfiguredPackage
, wrapParseWarning
, ppPackageCheck
, ppCheckExplanationId
, isHackageDistError
, filterPackageChecksById
, filterPackageChecksByIdString
-- ** Checking package contents
, checkPackageFiles
, checkPackageFilesGPD
, checkPackageContent
, CheckPackageContentOps (..)
) where
import Distribution.Compat.Prelude
import Prelude ()
import Data.List (group)
import Distribution.CabalSpecVersion
import Distribution.Compat.Lens
import Distribution.Compiler
import Distribution.License
import Distribution.Package
import Distribution.PackageDescription
import Distribution.PackageDescription.Check.Common
import Distribution.PackageDescription.Check.Conditional
import Distribution.PackageDescription.Check.Monad
import Distribution.PackageDescription.Check.Paths
import Distribution.PackageDescription.Check.Target
import Distribution.PackageDescription.Check.Warning
import Distribution.Parsec.Warning (PWarning)
import Distribution.Pretty (prettyShow)
import Distribution.Simple.Glob
( Glob
, GlobResult (..)
, globMatches
, parseFileGlob
, runDirFileGlob
)
import Distribution.Simple.Utils hiding (findPackageDesc, notice)
import Distribution.Utils.Generic (isAscii)
import Distribution.Utils.Path
import Distribution.Verbosity
import Distribution.Version
import System.FilePath (splitExtension, takeFileName)
import qualified Data.ByteString.Lazy as BS
import qualified Distribution.SPDX as SPDX
import qualified System.Directory as System
import qualified System.Directory (getDirectoryContents)
import qualified System.FilePath.Windows as FilePath.Windows (isValid)
import qualified Data.Set as Set
import qualified Distribution.Utils.ShortText as ShortText
import qualified Distribution.Types.GenericPackageDescription.Lens as L
import Control.Monad
-- $setup
-- >>> import Control.Arrow ((&&&))
-- ☞ N.B.
--
-- Part of the tools/scaffold used to perform check is found in
-- Distribution.PackageDescription.Check.Types. Summary of that module (for
-- how we use it here):
-- 1. we work inside a 'CheckM m a' monad (where `m` is an abstraction to
-- run non-pure checks);
-- 2. 'checkP', 'checkPre' functions perform checks (respectively pure and
-- non-pure);
-- 3. 'PackageCheck' and 'CheckExplanation' are types for warning severity
-- and description.
-- ------------------------------------------------------------
-- Checking interface
-- ------------------------------------------------------------
-- | 'checkPackagePrim' is the most general way to invoke package checks.
-- We pass to it two interfaces (one to check contents of packages, the
-- other to inspect working tree for orphan files) and before that a
-- Boolean to indicate whether we want pure checks or not. Based on these
-- parameters, some checks will be performed, some omitted.
-- Generality over @m@ means we could do non pure checks in monads other
-- than IO (e.g. a virtual filesystem, like a zip file, a VCS filesystem,
-- etc).
checkPackagePrim
:: Monad m
=> Bool -- Perform pure checks?
-> Maybe (CheckPackageContentOps m) -- Package content interface.
-> Maybe (CheckPreDistributionOps m) -- Predist checks interface.
-> GenericPackageDescription -- GPD to check.
-> m [PackageCheck]
checkPackagePrim b mco mpdo gpd = do
let cm = checkGenericPackageDescription gpd
ci = CheckInterface b mco mpdo
ctx = pristineCheckCtx ci gpd
execCheckM cm ctx
-- | Check for common mistakes and problems in package descriptions.
--
-- This is the standard collection of checks covering all aspects except
-- for checks that require looking at files within the package. For those
-- see 'checkPackageFiles'.
checkPackage :: GenericPackageDescription -> [PackageCheck]
checkPackage gpd = runIdentity $ checkPackagePrim True Nothing Nothing gpd
-- | This function is an oddity due to the historical
-- GenericPackageDescription/PackageDescription split. It is only maintained
-- not to break interface, use `checkPackage` if possible.
checkConfiguredPackage :: PackageDescription -> [PackageCheck]
checkConfiguredPackage pd = checkPackage (pd2gpd pd)
-- | Sanity check things that requires looking at files in the package.
-- This is a generalised version of 'checkPackageFiles' that can work in any
-- monad for which you can provide 'CheckPackageContentOps' operations.
--
-- The point of this extra generality is to allow doing checks in some virtual
-- file system, for example a tarball in memory.
checkPackageContent
:: Monad m
=> CheckPackageContentOps m
-> GenericPackageDescription
-> m [PackageCheck]
checkPackageContent pops gpd = checkPackagePrim False (Just pops) Nothing gpd
-- | Sanity checks that require IO. 'checkPackageFiles' looks at the files
-- in the package and expects to find the package unpacked at the given
-- filepath.
checkPackageFilesGPD
:: Verbosity -- Glob warn message verbosity.
-> GenericPackageDescription
-> FilePath -- Package root.
-> IO [PackageCheck]
checkPackageFilesGPD verbosity gpd root =
checkPackagePrim False (Just checkFilesIO) (Just checkPreIO) gpd
where
checkFilesIO =
CheckPackageContentOps
{ doesFileExist = System.doesFileExist . relative
, doesDirectoryExist = System.doesDirectoryExist . relative
, getDirectoryContents = System.Directory.getDirectoryContents . relative
, getFileContents = BS.readFile . relative
}
checkPreIO =
CheckPreDistributionOps
{ runDirFileGlobM = \fp g -> runDirFileGlob verbosity (Just . specVersion $ packageDescription gpd) (root </> fp) g
, getDirectoryContentsM = System.Directory.getDirectoryContents . relative
}
relative :: FilePath -> FilePath
relative path = root </> path
-- | Same as 'checkPackageFilesGPD', but working with 'PackageDescription'.
--
-- This function is included for legacy reasons, use 'checkPackageFilesGPD'
-- if you are working with 'GenericPackageDescription'.
checkPackageFiles
:: Verbosity -- Glob warn message verbosity.
-> PackageDescription
-> FilePath -- Package root.
-> IO [PackageCheck]
checkPackageFiles verbosity pd oot =
checkPackageFilesGPD verbosity (pd2gpd pd) oot
-- ------------------------------------------------------------
-- Package description
-- ------------------------------------------------------------
-- Here lies the meat of the module. Starting from 'GenericPackageDescription',
-- we walk the data while doing a number of checks.
--
-- Where applicable we do a full pattern match (if the data changes, code will
-- break: a gentle reminder to add more checks).
-- Pattern matching variables convention: matching accessor + underscore.
-- This way it is easier to see which one we are missing if we run into
-- an “GPD should have 20 arguments but has been given only 19” error.
-- | 'GenericPackageDescription' checks. Remember that for historical quirks
-- in the cabal codebase we have both `GenericPackageDescription` and
-- `PackageDescription` and that PD is both a *field* of GPD and a concept
-- of its own (i.e. a fully realised GPD).
-- In this case we are checking (correctly) GPD, so for target info/checks
-- you should walk condLibrary_ etc. and *not* the (empty) target info in
-- PD. See 'pd2gpd' for a convenient hack when you only have
-- 'PackageDescription'.
checkGenericPackageDescription
:: Monad m
=> GenericPackageDescription
-> CheckM m ()
checkGenericPackageDescription
gpd@( GenericPackageDescription
packageDescription_
_gpdScannedVersion_
genPackageFlags_
condLibrary_
condSubLibraries_
condForeignLibs_
condExecutables_
condTestSuites_
condBenchmarks_
) =
do
-- § Description and names.
checkPackageDescription packageDescription_
-- Targets should be present...
let condAllLibraries =
maybeToList condLibrary_
++ (map snd condSubLibraries_)
checkP
( and
[ null condExecutables_
, null condTestSuites_
, null condBenchmarks_
, null condAllLibraries
, null condForeignLibs_
]
)
(PackageBuildImpossible NoTarget)
-- ... and have unique names (names are not under conditional, it is
-- appropriate to check here.
(nsubs, nexes, ntests, nbenchs) <-
asksCM
( ( \n ->
( pnSubLibs n
, pnExecs n
, pnTests n
, pnBenchs n
)
)
. ccNames
)
let names = concat [nsubs, nexes, ntests, nbenchs]
dupes = dups names
checkP
(not . null $ dups names)
(PackageBuildImpossible $ DuplicateSections dupes)
-- PackageDescription checks.
checkPackageDescription packageDescription_
-- Flag names.
mapM_ checkFlagName genPackageFlags_
-- § Feature checks.
checkSpecVer
CabalSpecV2_0
(not . null $ condSubLibraries_)
(PackageDistInexcusable CVMultiLib)
checkSpecVer
CabalSpecV1_8
(not . null $ condTestSuites_)
(PackageDistInexcusable CVTestSuite)
-- § Conditional targets
-- Extract dependencies from libraries, to be passed along for
-- PVP checks purposes.
pName <-
asksCM
( packageNameToUnqualComponentName
. pkgName
. pnPackageId
. ccNames
)
let ads =
maybe [] ((: []) . extractAssocDeps pName) condLibrary_
++ map (uncurry extractAssocDeps) condSubLibraries_
case condLibrary_ of
Just cl ->
checkCondTarget
genPackageFlags_
(checkLibrary False ads)
(const id)
(mempty, cl)
Nothing -> return ()
mapM_
( checkCondTarget
genPackageFlags_
(checkLibrary False ads)
(\u l -> l{libName = maybeToLibraryName (Just u)})
)
condSubLibraries_
mapM_
( checkCondTarget
genPackageFlags_
checkForeignLib
(const id)
)
condForeignLibs_
mapM_
( checkCondTarget
genPackageFlags_
(checkExecutable ads)
(const id)
)
condExecutables_
mapM_
( checkCondTarget
genPackageFlags_
(checkTestSuite ads)
(\u l -> l{testName = u})
)
condTestSuites_
mapM_
( checkCondTarget
genPackageFlags_
(checkBenchmark ads)
(\u l -> l{benchmarkName = u})
)
condBenchmarks_
-- For unused flags it is clearer and more convenient to fold the
-- data rather than walk it, an exception to the rule.
checkP
(decFlags /= usedFlags)
(PackageDistSuspicious $ DeclaredUsedFlags decFlags usedFlags)
-- Duplicate modules.
mapM_ tellP (checkDuplicateModules gpd)
where
-- todo is this caught at parse time?
checkFlagName :: Monad m => PackageFlag -> CheckM m ()
checkFlagName pf =
let fn = unFlagName . flagName $ pf
invalidFlagName ('-' : _) = True -- starts with dash
invalidFlagName cs = any (not . isAscii) cs -- non ASCII
in checkP
(invalidFlagName fn)
(PackageDistInexcusable $ SuspiciousFlagName [fn])
decFlags :: Set.Set FlagName
decFlags = toSetOf (L.genPackageFlags . traverse . L.flagName) gpd
usedFlags :: Set.Set FlagName
usedFlags =
mconcat
[ toSetOf (L.condLibrary . traverse . traverseCondTreeV . L._PackageFlag) gpd
, toSetOf (L.condSubLibraries . traverse . _2 . traverseCondTreeV . L._PackageFlag) gpd
, toSetOf (L.condForeignLibs . traverse . _2 . traverseCondTreeV . L._PackageFlag) gpd
, toSetOf (L.condExecutables . traverse . _2 . traverseCondTreeV . L._PackageFlag) gpd
, toSetOf (L.condTestSuites . traverse . _2 . traverseCondTreeV . L._PackageFlag) gpd
, toSetOf (L.condBenchmarks . traverse . _2 . traverseCondTreeV . L._PackageFlag) gpd
]
checkPackageDescription :: Monad m => PackageDescription -> CheckM m ()
checkPackageDescription
pkg@( PackageDescription
specVersion_
package_
licenseRaw_
licenseFiles_
_copyright_
maintainer_
_author_
_stability_
testedWith_
_homepage_
_pkgUrl_
_bugReports_
sourceRepos_
synopsis_
description_
category_
customFieldsPD_
buildTypeRaw_
setupBuildInfo_
_library_
_subLibraries_
_executables_
_foreignLibs_
_testSuites_
_benchmarks_
dataFiles_
dataDir_
extraSrcFiles_
extraTmpFiles_
extraDocFiles_
extraFiles_
) = do
-- § Sanity checks.
checkPackageId package_
-- TODO `name` is caught at parse level, remove this test.
let pn = packageName package_
checkP
(null . unPackageName $ pn)
(PackageBuildImpossible NoNameField)
-- TODO `version` is caught at parse level, remove this test.
checkP
(nullVersion == packageVersion package_)
(PackageBuildImpossible NoVersionField)
-- But it is OK for executables to have the same name.
nsubs <- asksCM (pnSubLibs . ccNames)
checkP
(any (== prettyShow pn) (prettyShow <$> nsubs))
(PackageBuildImpossible $ IllegalLibraryName pn)
-- § Fields check.
checkNull
category_
(PackageDistSuspicious MissingFieldCategory)
checkNull
maintainer_
(PackageDistSuspicious MissingFieldMaintainer)
checkP
(ShortText.null synopsis_ && not (ShortText.null description_))
(PackageDistSuspicious MissingFieldSynopsis)
checkP
(ShortText.null description_ && not (ShortText.null synopsis_))
(PackageDistSuspicious MissingFieldDescription)
checkP
(all ShortText.null [synopsis_, description_])
(PackageDistInexcusable MissingFieldSynOrDesc)
checkP
(ShortText.length synopsis_ > 80)
(PackageDistSuspicious SynopsisTooLong)
checkP
( not (ShortText.null description_)
&& ShortText.length description_ <= ShortText.length synopsis_
)
(PackageDistSuspicious ShortDesc)
-- § Paths.
mapM_ (checkPath False "extra-source-files" PathKindGlob . getSymbolicPath) extraSrcFiles_
mapM_ (checkPath False "extra-tmp-files" PathKindFile . getSymbolicPath) extraTmpFiles_
mapM_ (checkPath False "extra-doc-files" PathKindGlob . getSymbolicPath) extraDocFiles_
mapM_ (checkPath False "extra-files" PathKindGlob . getSymbolicPath) extraFiles_
mapM_ (checkPath False "data-files" PathKindGlob . getSymbolicPath) dataFiles_
let rawDataDir = getSymbolicPath dataDir_
checkPath True "data-dir" PathKindDirectory rawDataDir
let licPaths = map getSymbolicPath licenseFiles_
mapM_ (checkPath False "license-file" PathKindFile) licPaths
mapM_ checkLicFileExist licenseFiles_
-- § Globs.
dataGlobs <- mapM (checkGlob "data-files" . getSymbolicPath) dataFiles_
extraSrcGlobs <- mapM (checkGlob "extra-source-files" . getSymbolicPath) extraSrcFiles_
docGlobs <- mapM (checkGlob "extra-doc-files" . getSymbolicPath) extraDocFiles_
extraGlobs <- mapM (checkGlob "extra-files" . getSymbolicPath) extraFiles_
-- We collect globs to feed them to checkMissingDocs.
-- § Missing documentation.
checkMissingDocs
(catMaybes dataGlobs)
(catMaybes extraSrcGlobs)
(catMaybes docGlobs)
(catMaybes extraGlobs)
-- § Datafield checks.
checkSetupBuildInfo setupBuildInfo_
mapM_ checkTestedWith testedWith_
either
checkNewLicense
(checkOldLicense $ null licenseFiles_)
licenseRaw_
checkSourceRepos sourceRepos_
mapM_ checkCustomField customFieldsPD_
-- Feature checks.
checkSpecVer
CabalSpecV1_18
(not . null $ extraDocFiles_)
(PackageDistInexcusable CVExtraDocFiles)
checkSpecVer
CabalSpecV1_6
(not . null $ sourceRepos_)
(PackageDistInexcusable CVSourceRepository)
checkP
( specVersion_ >= CabalSpecV1_24
&& isNothing setupBuildInfo_
&& buildTypeRaw_ == Just Custom
)
(PackageBuildWarning CVCustomSetup)
checkSpecVer
CabalSpecV1_24
( isNothing setupBuildInfo_
&& buildTypeRaw_ == Just Custom
)
(PackageDistSuspiciousWarn CVExpliticDepsCustomSetup)
checkP
(isNothing buildTypeRaw_ && specVersion_ < CabalSpecV2_2)
(PackageBuildWarning NoBuildType)
checkP
(isJust setupBuildInfo_ && buildType pkg `notElem` [Custom, Hooks])
(PackageBuildWarning NoCustomSetup)
-- Contents.
checkConfigureExists (buildType pkg)
checkSetupExists (buildType pkg)
checkCabalFile (packageName pkg)
mapM_ (checkGlobFile specVersion_ "." "extra-source-files" . getSymbolicPath) extraSrcFiles_
mapM_ (checkGlobFile specVersion_ "." "extra-doc-files" . getSymbolicPath) extraDocFiles_
mapM_ (checkGlobFile specVersion_ "." "extra-files" . getSymbolicPath) extraFiles_
mapM_ (checkGlobFile specVersion_ rawDataDir "data-files" . getSymbolicPath) dataFiles_
where
checkNull
:: Monad m
=> ShortText.ShortText
-> PackageCheck
-> CheckM m ()
checkNull st c = checkP (ShortText.null st) c
checkTestedWith
:: Monad m
=> (CompilerFlavor, VersionRange)
-> CheckM m ()
checkTestedWith (OtherCompiler n, _) =
tellP (PackageBuildWarning $ UnknownCompilers [n])
checkTestedWith (compiler, versionRange) =
checkVersionRange compiler versionRange
checkVersionRange
:: Monad m
=> CompilerFlavor
-> VersionRange
-> CheckM m ()
checkVersionRange cmp vr =
when
(isNoVersion vr)
( let dep =
[ Dependency
(mkPackageName (prettyShow cmp))
vr
mainLibSet
]
in tellP (PackageDistInexcusable (InvalidTestWith dep))
)
checkSetupBuildInfo :: Monad m => Maybe SetupBuildInfo -> CheckM m ()
checkSetupBuildInfo Nothing = return ()
checkSetupBuildInfo (Just (SetupBuildInfo ds _)) = do
let uqs = map mkUnqualComponentName ["base", "Cabal"]
(is, rs) <- partitionDeps [] uqs ds
let ick = PackageDistInexcusable . UpperBoundSetup
rck =
PackageDistSuspiciousWarn
. MissingUpperBounds CETSetup
leuck =
PackageDistSuspiciousWarn
. LEUpperBounds CETSetup
tzuck =
PackageDistSuspiciousWarn
. TrailingZeroUpperBounds CETSetup
gtlck =
PackageDistSuspiciousWarn
. GTLowerBounds CETSetup
checkPVP (checkDependencyVersionRange $ not . hasUpperBound) ick is
checkPVPs (checkDependencyVersionRange $ not . hasUpperBound) rck rs
checkPVPs (checkDependencyVersionRange hasLEUpperBound) leuck ds
checkPVPs (checkDependencyVersionRange hasTrailingZeroUpperBound) tzuck ds
checkPVPs (checkDependencyVersionRange hasGTLowerBound) gtlck ds
checkPackageId :: Monad m => PackageIdentifier -> CheckM m ()
checkPackageId (PackageIdentifier pkgName_ _pkgVersion_) = do
checkP
(not . FilePath.Windows.isValid . prettyShow $ pkgName_)
(PackageDistInexcusable $ InvalidNameWin pkgName_)
checkP (isPrefixOf "z-" . prettyShow $ pkgName_) $
(PackageDistInexcusable ZPrefix)
checkNewLicense :: Monad m => SPDX.License -> CheckM m ()
checkNewLicense lic = do
checkP
(lic == SPDX.NONE)
(PackageDistInexcusable NONELicense)
checkOldLicense
:: Monad m
=> Bool -- Flag: no license file?
-> License
-> CheckM m ()
checkOldLicense nullLicFiles lic = do
checkP
(lic == UnspecifiedLicense)
(PackageDistInexcusable NoLicense)
checkP
(lic == AllRightsReserved)
(PackageDistSuspicious AllRightsReservedLicense)
checkSpecVer
CabalSpecV1_4
(lic `notElem` compatLicenses)
(PackageDistInexcusable (LicenseMessParse lic))
checkP
(lic == BSD4)
(PackageDistSuspicious UncommonBSD4)
case lic of
UnknownLicense l ->
tellP (PackageBuildWarning (UnrecognisedLicense l))
_ -> return ()
checkP
( lic
`notElem` [ AllRightsReserved
, UnspecifiedLicense
, PublicDomain
]
&&
-- AllRightsReserved and PublicDomain are not strictly
-- licenses so don't need license files.
nullLicFiles
)
$ (PackageDistSuspicious NoLicenseFile)
case unknownLicenseVersion lic of
Just knownVersions ->
tellP
(PackageDistSuspicious $ UnknownLicenseVersion lic knownVersions)
_ -> return ()
where
compatLicenses =
[ GPL Nothing
, LGPL Nothing
, AGPL Nothing
, BSD3
, BSD4
, PublicDomain
, AllRightsReserved
, UnspecifiedLicense
, OtherLicense
]
unknownLicenseVersion (GPL (Just v))
| v `notElem` knownVersions = Just knownVersions
where
knownVersions = [v' | GPL (Just v') <- knownLicenses]
unknownLicenseVersion (LGPL (Just v))
| v `notElem` knownVersions = Just knownVersions
where
knownVersions = [v' | LGPL (Just v') <- knownLicenses]
unknownLicenseVersion (AGPL (Just v))
| v `notElem` knownVersions = Just knownVersions
where
knownVersions = [v' | AGPL (Just v') <- knownLicenses]
unknownLicenseVersion (Apache (Just v))
| v `notElem` knownVersions = Just knownVersions
where
knownVersions = [v' | Apache (Just v') <- knownLicenses]
unknownLicenseVersion _ = Nothing
checkSourceRepos :: Monad m => [SourceRepo] -> CheckM m ()
checkSourceRepos rs = do
mapM_ repoCheck rs
checkMissingVcsInfo rs
where
-- Single repository checks.
repoCheck :: Monad m => SourceRepo -> CheckM m ()
repoCheck
( SourceRepo
repoKind_
repoType_
repoLocation_
repoModule_
_repoBranch_
repoTag_
repoSubdir_
) = do
case repoKind_ of
RepoKindUnknown kind ->
tellP
(PackageDistInexcusable $ UnrecognisedSourceRepo kind)
_ -> return ()
checkP
(isNothing repoType_)
(PackageDistInexcusable MissingType)
checkP
(isNothing repoLocation_)
(PackageDistInexcusable MissingLocation)
checkGitProtocol repoLocation_
checkP
( repoType_ == Just (KnownRepoType CVS)
&& isNothing repoModule_
)
(PackageDistInexcusable MissingModule)
checkP
(repoKind_ == RepoThis && isNothing repoTag_)
(PackageDistInexcusable MissingTag)
checkP
(any isAbsoluteOnAnyPlatform repoSubdir_)
(PackageDistInexcusable SubdirRelPath)
case join . fmap isGoodRelativeDirectoryPath $ repoSubdir_ of
Just err ->
tellP
(PackageDistInexcusable $ SubdirGoodRelPath err)
Nothing -> return ()
checkMissingVcsInfo :: Monad m => [SourceRepo] -> CheckM m ()
checkMissingVcsInfo rs =
let rdirs = concatMap repoTypeDirname knownRepoTypes
in checkPkg
( \ops -> do
us <- or <$> traverse (doesDirectoryExist ops) rdirs
return (null rs && us)
)
(PackageDistSuspicious MissingSourceControl)
where
repoTypeDirname :: KnownRepoType -> [FilePath]
repoTypeDirname Darcs = ["_darcs"]
repoTypeDirname Git = [".git"]
repoTypeDirname SVN = [".svn"]
repoTypeDirname CVS = ["CVS"]
repoTypeDirname Mercurial = [".hg"]
repoTypeDirname GnuArch = [".arch-params"]
repoTypeDirname Bazaar = [".bzr"]
repoTypeDirname Monotone = ["_MTN"]
repoTypeDirname Pijul = [".pijul"]
-- git:// lacks TLS or other encryption, see
-- https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols#_the_cons_4
checkGitProtocol
:: Monad m
=> Maybe String -- Repository location
-> CheckM m ()
checkGitProtocol mloc =
checkP
(fmap (isPrefixOf "git://") mloc == Just True)
(PackageBuildWarning GitProtocol)
-- ------------------------------------------------------------
-- Package and distribution checks
-- ------------------------------------------------------------
-- | Find a package description file in the given directory. Looks for
-- @.cabal@ files. Like 'Distribution.Simple.Utils.findPackageDesc',
-- but generalized over monads.
findPackageDesc :: Monad m => CheckPackageContentOps m -> m [FilePath]
findPackageDesc ops = do
let dir = "."
files <- getDirectoryContents ops dir
-- to make sure we do not mistake a ~/.cabal/ dir for a <name>.cabal
-- file we filter to exclude dirs and null base file names:
cabalFiles <-
filterM
(doesFileExist ops)
[ dir </> file
| file <- files
, let (name, ext) = splitExtension file
, not (null name) && ext == ".cabal"
]
return cabalFiles
checkCabalFile :: Monad m => PackageName -> CheckM m ()
checkCabalFile pn = do
-- liftInt is a bit more messy than stricter interface, but since
-- each of the following check is exclusive, we can simplify the
-- condition flow.
liftInt
ciPackageOps
( \ops -> do
-- 1. Get .cabal files.
ds <- findPackageDesc ops
case ds of
[] -> return [PackageBuildImpossible NoDesc]
-- No .cabal file.
[d] -> do
bc <- bomf ops d
return (catMaybes [bc, noMatch d])
-- BOM + no matching .cabal checks.
_ -> return [PackageBuildImpossible $ MultiDesc ds]
)
where
-- Multiple .cabal files.
bomf
:: Monad m
=> CheckPackageContentOps m
-> FilePath
-> m (Maybe PackageCheck)
bomf wops wfp = do
b <- BS.isPrefixOf bomUtf8 <$> getFileContents wops wfp
if b
then (return . Just) (PackageDistInexcusable $ BOMStart wfp)
else return Nothing
bomUtf8 :: BS.ByteString
bomUtf8 = BS.pack [0xef, 0xbb, 0xbf] -- U+FEFF encoded as UTF8
noMatch :: FilePath -> Maybe PackageCheck
noMatch wd =
let expd = unPackageName pn <.> "cabal"
in if takeFileName wd /= expd
then Just (PackageDistInexcusable $ NotPackageName wd expd)
else Nothing
checkLicFileExist
:: Monad m
=> RelativePath Pkg File
-> CheckM m ()
checkLicFileExist sp = do
let fp = getSymbolicPath sp
checkPkg
(\ops -> not <$> doesFileExist ops fp)
(PackageBuildWarning $ UnknownFile "license-file" sp)
checkConfigureExists :: Monad m => BuildType -> CheckM m ()
checkConfigureExists Configure =
checkPkg
(\ops -> not <$> doesFileExist ops "configure")
(PackageBuildWarning MissingConfigureScript)
checkConfigureExists _ = return ()
checkSetupExists :: Monad m => BuildType -> CheckM m ()
checkSetupExists Simple = return ()
checkSetupExists _ =
checkPkg
( \ops -> do
ba <- doesFileExist ops "Setup.hs"
bb <- doesFileExist ops "Setup.lhs"
return (not $ ba || bb)
)
(PackageDistInexcusable MissingSetupFile)
-- The following functions are similar to 'CheckPackageContentOps m' ones,
-- but, as they inspect the files included in the package, but are primarily
-- looking for files in the working tree that may have been missed or other
-- similar problems that can only be detected pre-distribution.
--
-- Because Hackage necessarily checks the uploaded tarball, it is too late to
-- check these on the server; these checks only make sense in the development
-- and package-creation environment.
-- This most likely means we need to use IO, but a dictionary
-- 'CheckPreDistributionOps m' is provided in case in the future such
-- information can come from somewhere else (e.g. VCS filesystem).
--
-- Note: this really shouldn't return any 'Inexcusable' warnings,
-- because that will make us say that Hackage would reject the package.
-- But, because Hackage doesn't yet run these tests, that will be a lie!
checkGlobFile
:: Monad m
=> CabalSpecVersion
-> FilePath -- Glob pattern.
-> FilePath -- Folder to check.
-> CabalField -- .cabal field we are checking.
-> CheckM m ()
checkGlobFile cv ddir title fp = do
let adjDdir = if null ddir then "." else ddir
dir
| title == "data-files" = adjDdir
| otherwise = "."
case parseFileGlob cv fp of
-- We just skip over parse errors here; they're reported elsewhere.
Left _ -> return ()
Right parsedGlob -> do
liftInt ciPreDistOps $ \po -> do
rs <- runDirFileGlobM po dir parsedGlob
return $ checkGlobResult title fp rs
-- | Checks for matchless globs and too strict matching (<2.4 spec).
checkGlobResult
:: CabalField -- .cabal field we are checking
-> FilePath -- Glob pattern (to show the user
-- which pattern is the offending
-- one).
-> [GlobResult FilePath] -- List of glob results.
-> [PackageCheck]
checkGlobResult title fp rs = dirCheck ++ catMaybes (map getWarning rs)
where
dirCheck
| all (not . withoutNoMatchesWarning) rs =
[PackageDistSuspiciousWarn $ GlobNoMatch title fp]
| otherwise = []
-- If there's a missing directory in play, since globs in Cabal packages
-- don't (currently) support disjunction, that will always mean there are
-- no matches. The no matches error in this case is strictly less
-- informative than the missing directory error.
withoutNoMatchesWarning (GlobMatch _) = True
withoutNoMatchesWarning (GlobWarnMultiDot _) = False
withoutNoMatchesWarning (GlobMissingDirectory _) = True
withoutNoMatchesWarning (GlobMatchesDirectory _) = True
getWarning :: GlobResult FilePath -> Maybe PackageCheck
getWarning (GlobMatch _) = Nothing
-- Before Cabal 2.4, the extensions of globs had to match the file
-- exactly. This has been relaxed in 2.4 to allow matching only the
-- suffix. This warning detects when pre-2.4 package descriptions
-- are omitting files purely because of the stricter check.
getWarning (GlobWarnMultiDot file) =
Just $ PackageDistSuspiciousWarn (GlobExactMatch title fp file)
getWarning (GlobMissingDirectory dir) =
Just $ PackageDistSuspiciousWarn (GlobNoDir title fp dir)
-- GlobMatchesDirectory is handled elsewhere if relevant;
-- we can discard it here.
getWarning (GlobMatchesDirectory _) = Nothing
-- ------------------------------------------------------------
-- Other exports
-- ------------------------------------------------------------
-- | Wraps `ParseWarning` into `PackageCheck`.
wrapParseWarning :: FilePath -> PWarning -> PackageCheck
wrapParseWarning fp pw = PackageDistSuspicious (ParseWarning fp pw)
-- TODO: as Jul 2022 there is no severity indication attached PWarnType.
-- Once that is added, we can output something more appropriate
-- than PackageDistSuspicious for every parse warning.
-- (see: Cabal-syntax/src/Distribution/Parsec/Warning.hs)
-- ------------------------------------------------------------
-- Ancillaries
-- ------------------------------------------------------------
-- Gets a list of dependencies from a Library target to pass to PVP related
-- functions. We are not doing checks here: this is not imprecise, as the
-- library itself *will* be checked for PVP errors.
-- Same for branch merging,
-- each of those branch will be checked one by one.
extractAssocDeps
:: UnqualComponentName -- Name of the target library
-> CondTree ConfVar [Dependency] Library
-> AssocDep
extractAssocDeps n ct =
let a = ignoreConditions ct
in -- Merging is fine here, remember the specific
-- library dependencies will be checked branch
-- by branch.
(n, snd a)
-- | August 2022: this function is an oddity due to the historical
-- GenericPackageDescription/PackageDescription split (check
-- Distribution.Types.PackageDescription for a description of the relationship
-- between GPD and PD.
-- It is only maintained not to break interface, should be deprecated in the
-- future in favour of `checkPackage` when PD and GPD are refactored sensibly.
pd2gpd :: PackageDescription -> GenericPackageDescription
pd2gpd pd = gpd
where
gpd :: GenericPackageDescription
gpd =
emptyGenericPackageDescription
{ packageDescription = pd
, condLibrary = fmap t2c (library pd)
, condSubLibraries = map (t2cName ln id) (subLibraries pd)
, condForeignLibs =
map
(t2cName foreignLibName id)
(foreignLibs pd)
, condExecutables =
map
(t2cName exeName id)
(executables pd)
, condTestSuites =
map
(t2cName testName remTest)
(testSuites pd)
, condBenchmarks =
map
(t2cName benchmarkName remBench)
(benchmarks pd)
}
-- From target to simple, unconditional CondTree.
t2c :: a -> CondTree ConfVar [Dependency] a
t2c a = CondNode a [] []
-- From named target to unconditional CondTree. Notice we have
-- a function to extract the name *and* a function to modify
-- the target. This is needed for 'initTargetAnnotation' to work
-- properly and to contain all the quirks inside 'pd2gpd'.
t2cName
:: (a -> UnqualComponentName)
-> (a -> a)
-> a
-> (UnqualComponentName, CondTree ConfVar [Dependency] a)
t2cName nf mf a = (nf a, t2c . mf $ a)
ln :: Library -> UnqualComponentName
ln wl = case libName wl of
(LSubLibName u) -> u