-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathCmdErrorMessages.hs
525 lines (490 loc) · 20 KB
/
CmdErrorMessages.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
{-# LANGUAGE NamedFieldPuns #-}
-- | Utilities to help format error messages for the various CLI commands.
module Distribution.Client.CmdErrorMessages
( module Distribution.Client.CmdErrorMessages
, module Distribution.Client.TargetSelector
) where
import Distribution.Client.Compat.Prelude
import Prelude ()
import Distribution.Client.ProjectPlanning
( AvailableTarget (..)
, AvailableTargetStatus (..)
, CannotPruneDependencies (..)
, TargetRequested (..)
)
import Distribution.Client.TargetProblem
( TargetProblem (..)
, TargetProblem'
)
import Distribution.Client.TargetSelector
( ComponentKind (..)
, ComponentKindFilter
, SubComponentTarget (..)
, TargetSelector (..)
, componentKind
, showTargetSelector
)
import Distribution.Package
( PackageId
, PackageName
, packageId
, packageName
)
import Distribution.Simple.Utils
( dieWithException
)
import Distribution.Solver.Types.OptionalStanza
( OptionalStanza (..)
)
import Distribution.Types.ComponentName
( ComponentName (..)
, showComponentName
)
import Distribution.Types.LibraryName
( LibraryName (..)
)
import qualified Data.List.NonEmpty as NE
import Distribution.Client.Errors
-----------------------
-- Singular or plural
--
-- | A tag used in rendering messages to distinguish singular or plural.
data Plural = Singular | Plural
-- | Used to render a singular or plural version of something
--
-- > plural (listPlural theThings) "it is" "they are"
plural :: Plural -> a -> a -> a
plural Singular si _pl = si
plural Plural _si pl = pl
-- | Singular for singleton lists and plural otherwise.
listPlural :: [a] -> Plural
listPlural [_] = Singular
listPlural _ = Plural
--------------------
-- Rendering lists
--
-- | Render a list of things in the style @foo, bar and baz@
renderListCommaAnd :: [String] -> String
renderListCommaAnd [] = ""
renderListCommaAnd [x] = x
renderListCommaAnd [x, x'] = x ++ " and " ++ x'
renderListCommaAnd (x : xs) = x ++ ", " ++ renderListCommaAnd xs
renderListTabular :: [String] -> String
renderListTabular = ("\n" ++) . unlines . map ("| * " ++)
renderListPretty :: [String] -> String
renderListPretty xs =
if length xs > 5
then renderListTabular xs
else renderListCommaAnd xs
-- | Render a list of things in the style @blah blah; this that; and the other@
renderListSemiAnd :: [String] -> String
renderListSemiAnd [] = ""
renderListSemiAnd [x] = x
renderListSemiAnd [x, x'] = x ++ "; and " ++ x'
renderListSemiAnd (x : xs) = x ++ "; " ++ renderListSemiAnd xs
-- | When rendering lists of things it often reads better to group related
-- things, e.g. grouping components by package name
--
-- > renderListSemiAnd
-- > [ "the package " ++ prettyShow pkgname ++ " components "
-- > ++ renderListCommaAnd showComponentName components
-- > | (pkgname, components) <- sortGroupOn packageName allcomponents ]
sortGroupOn :: Ord b => (a -> b) -> [a] -> [(b, [a])]
sortGroupOn key =
map (\(x :| xs) -> (key x, x : xs))
. NE.groupBy ((==) `on` key)
. sortBy (compare `on` key)
----------------------------------------------------
-- Rendering for a few project and package types
--
renderTargetSelector :: TargetSelector -> String
renderTargetSelector (TargetPackage _ pkgids Nothing) =
"the "
++ plural (listPlural pkgids) "package" "packages"
++ " "
++ renderListCommaAnd (map prettyShow pkgids)
renderTargetSelector (TargetPackage _ pkgids (Just kfilter)) =
"the "
++ renderComponentKind Plural kfilter
++ " in the "
++ plural (listPlural pkgids) "package" "packages"
++ " "
++ renderListCommaAnd (map prettyShow pkgids)
renderTargetSelector (TargetPackageNamed pkgname Nothing) =
"the package " ++ prettyShow pkgname
renderTargetSelector (TargetPackageNamed pkgname (Just kfilter)) =
"the "
++ renderComponentKind Plural kfilter
++ " in the package "
++ prettyShow pkgname
renderTargetSelector (TargetAllPackages Nothing) =
"all the packages in the project"
renderTargetSelector (TargetAllPackages (Just kfilter)) =
"all the "
++ renderComponentKind Plural kfilter
++ " in the project"
renderTargetSelector (TargetComponent pkgid cname subtarget) =
renderSubComponentTarget subtarget
++ "the "
++ renderComponentName (packageName pkgid) cname
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname) subtarget) =
renderSubComponentTarget subtarget
++ "the component "
++ prettyShow ucname
++ " in the package "
++ prettyShow pkgname
renderTargetSelector (TargetComponentUnknown pkgname (Right cname) subtarget) =
renderSubComponentTarget subtarget
++ "the "
++ renderComponentName pkgname cname
renderSubComponentTarget :: SubComponentTarget -> String
renderSubComponentTarget WholeComponent = ""
renderSubComponentTarget (FileTarget filename) =
"the file " ++ filename ++ " in "
renderSubComponentTarget (ModuleTarget modname) =
"the module " ++ prettyShow modname ++ " in "
renderOptionalStanza :: Plural -> OptionalStanza -> String
renderOptionalStanza Singular TestStanzas = "test suite"
renderOptionalStanza Plural TestStanzas = "test suites"
renderOptionalStanza Singular BenchStanzas = "benchmark"
renderOptionalStanza Plural BenchStanzas = "benchmarks"
-- | The optional stanza type (test suite or benchmark), if it is one.
optionalStanza :: ComponentName -> Maybe OptionalStanza
optionalStanza (CTestName _) = Just TestStanzas
optionalStanza (CBenchName _) = Just BenchStanzas
optionalStanza _ = Nothing
-- | Does the 'TargetSelector' potentially refer to one package or many?
targetSelectorPluralPkgs :: TargetSelector -> Plural
targetSelectorPluralPkgs (TargetAllPackages _) = Plural
targetSelectorPluralPkgs (TargetPackage _ pids _) = listPlural pids
targetSelectorPluralPkgs (TargetPackageNamed _ _) = Singular
targetSelectorPluralPkgs TargetComponent{} = Singular
targetSelectorPluralPkgs TargetComponentUnknown{} = Singular
-- | Does the 'TargetSelector' refer to packages or to components?
targetSelectorRefersToPkgs :: TargetSelector -> Bool
targetSelectorRefersToPkgs (TargetAllPackages mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackage _ _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackageNamed _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs TargetComponent{} = False
targetSelectorRefersToPkgs TargetComponentUnknown{} = False
targetSelectorFilter :: TargetSelector -> Maybe ComponentKindFilter
targetSelectorFilter (TargetPackage _ _ mkfilter) = mkfilter
targetSelectorFilter (TargetPackageNamed _ mkfilter) = mkfilter
targetSelectorFilter (TargetAllPackages mkfilter) = mkfilter
targetSelectorFilter TargetComponent{} = Nothing
targetSelectorFilter TargetComponentUnknown{} = Nothing
renderComponentName :: PackageName -> ComponentName -> String
renderComponentName pkgname (CLibName LMainLibName) = "library " ++ prettyShow pkgname
renderComponentName _ (CLibName (LSubLibName name)) = "library " ++ prettyShow name
renderComponentName _ (CFLibName name) = "foreign library " ++ prettyShow name
renderComponentName _ (CExeName name) = "executable " ++ prettyShow name
renderComponentName _ (CTestName name) = "test suite " ++ prettyShow name
renderComponentName _ (CBenchName name) = "benchmark " ++ prettyShow name
renderComponentKind :: Plural -> ComponentKind -> String
renderComponentKind Singular ckind = case ckind of
LibKind -> "library" -- internal/sub libs?
FLibKind -> "foreign library"
ExeKind -> "executable"
TestKind -> "test suite"
BenchKind -> "benchmark"
renderComponentKind Plural ckind = case ckind of
LibKind -> "libraries" -- internal/sub libs?
FLibKind -> "foreign libraries"
ExeKind -> "executables"
TestKind -> "test suites"
BenchKind -> "benchmarks"
-------------------------------------------------------
-- Rendering error messages for TargetProblem
--
-- | Default implementation of 'reportTargetProblems' simply renders one problem per line.
reportTargetProblems :: Verbosity -> String -> [TargetProblem'] -> IO a
reportTargetProblems verbosity verb =
dieWithException verbosity . CmdErrorMessages . map (renderTargetProblem verb absurd)
-- | Default implementation of 'renderTargetProblem'.
renderTargetProblem
:: String
-- ^ verb
-> (a -> String)
-- ^ how to render custom problems
-> TargetProblem a
-> String
renderTargetProblem _verb f (CustomTargetProblem x) = f x
renderTargetProblem verb _ (TargetProblemNoneEnabled targetSelector targets) =
renderTargetProblemNoneEnabled verb targetSelector targets
renderTargetProblem verb _ (TargetProblemNoTargets targetSelector) =
renderTargetProblemNoTargets verb targetSelector
renderTargetProblem verb _ (TargetNotInProject pkgname) =
"Cannot "
++ verb
++ " the package "
++ prettyShow pkgname
++ ", it is not "
++ "in this project (either directly or indirectly). If you want to add it "
++ "to the project then edit the cabal.project file."
renderTargetProblem verb _ (TargetAvailableInIndex pkgname) =
"Cannot "
++ verb
++ " the package "
++ prettyShow pkgname
++ ", it is not "
++ "in this project (either directly or indirectly), but it is in the current "
++ "package index. If you want to add it to the project then edit the "
++ "cabal.project file."
renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname _) =
"Cannot "
++ verb
++ " the "
++ showComponentName cname
++ " because the "
++ "package "
++ prettyShow pkgid
++ " is not local to the project, and cabal "
++ "does not currently support building test suites or benchmarks of "
++ "non-local dependencies. To run test suites or benchmarks from "
++ "dependencies you can unpack the package locally and adjust the "
++ "cabal.project file to include that package directory."
renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname _) =
"Cannot "
++ verb
++ " the "
++ showComponentName cname
++ " because it is "
++ "marked as 'buildable: False' within the '"
++ prettyShow (packageName pkgid)
++ ".cabal' file (at least for the current configuration). If you believe it "
++ "should be buildable then check the .cabal file to see if the buildable "
++ "property is conditional on flags. Alternatively you may simply have to "
++ "edit the .cabal file to declare it as buildable and fix any resulting "
++ "build problems."
renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname _) =
"Cannot "
++ verb
++ " the "
++ showComponentName cname
++ " because "
++ "building "
++ compkinds
++ " has been explicitly disabled in the "
++ "configuration. You can adjust this configuration in the "
++ "cabal.project{.local} file either for all packages in the project or on "
++ "a per-package basis. Note that if you do not explicitly disable "
++ compkinds
++ " then the solver will merely try to make a plan with "
++ "them available, so you may wish to explicitly enable them which will "
++ "require the solver to find a plan with them available or to fail with an "
++ "explanation."
where
compkinds = renderComponentKind Plural (componentKind cname)
renderTargetProblem verb _ (TargetOptionalStanzaDisabledBySolver pkgid cname _) =
"Cannot "
++ verb
++ " the "
++ showComponentName cname
++ " because the "
++ "solver did not find a plan that included the "
++ compkinds
++ " for "
++ prettyShow pkgid
++ ". It is probably worth trying again with "
++ compkinds
++ " explicitly enabled in the configuration in the "
++ "cabal.project{.local} file. This will ask the solver to find a plan with "
++ "the "
++ compkinds
++ " available. It will either fail with an "
++ "explanation or find a different plan that uses different versions of some "
++ "other packages. Use the '--dry-run' flag to see package versions and "
++ "check that you are happy with the choices."
where
compkinds = renderComponentKind Plural (componentKind cname)
renderTargetProblem verb _ (TargetProblemUnknownComponent pkgname ecname) =
"Cannot "
++ verb
++ " the "
++ ( case ecname of
Left ucname -> "component " ++ prettyShow ucname
Right cname -> renderComponentName pkgname cname
)
++ " from the package "
++ prettyShow pkgname
++ ", because the package does not contain a "
++ ( case ecname of
Left _ -> "component"
Right cname -> renderComponentKind Singular (componentKind cname)
)
++ " with that name."
renderTargetProblem verb _ (TargetProblemNoSuchPackage pkgid) =
"Internal error when trying to "
++ verb
++ " the package "
++ prettyShow pkgid
++ ". The package is not in the set of available targets "
++ "for the project plan, which would suggest an inconsistency "
++ "between readTargetSelectors and resolveTargets."
renderTargetProblem verb _ (TargetProblemNoSuchComponent pkgid cname) =
"Internal error when trying to "
++ verb
++ " the "
++ showComponentName cname
++ " from the package "
++ prettyShow pkgid
++ ". The package,component pair is not in the set of available targets "
++ "for the project plan, which would suggest an inconsistency "
++ "between readTargetSelectors and resolveTargets."
------------------------------------------------------------
-- Rendering error messages for TargetProblemNoneEnabled
--
-- | Several commands have a @TargetProblemNoneEnabled@ problem constructor.
-- This renders an error message for those cases.
renderTargetProblemNoneEnabled
:: String
-> TargetSelector
-> [AvailableTarget ()]
-> String
renderTargetProblemNoneEnabled verb targetSelector targets =
"Cannot "
++ verb
++ " "
++ renderTargetSelector targetSelector
++ " because none of the components are available to build: "
++ renderListSemiAnd
[ case (status, mstanza) of
(TargetDisabledByUser, Just stanza) ->
renderListCommaAnd
[ "the " ++ showComponentName availableTargetComponentName
| AvailableTarget{availableTargetComponentName} <- targets'
]
++ plural (listPlural targets') " is " " are "
++ " not available because building "
++ renderOptionalStanza Plural stanza
++ " has been disabled in the configuration"
(TargetDisabledBySolver, Just stanza) ->
renderListCommaAnd
[ "the " ++ showComponentName availableTargetComponentName
| AvailableTarget{availableTargetComponentName} <- targets'
]
++ plural (listPlural targets') " is " " are "
++ "not available because the solver picked a plan that does not "
++ "include the "
++ renderOptionalStanza Plural stanza
++ ", perhaps because no such plan exists. To see the error message "
++ "explaining the problems with such plans, force the solver to "
++ "include the "
++ renderOptionalStanza Plural stanza
++ " for all "
++ "packages, by adding the line 'tests: True' to the "
++ "'cabal.project.local' file."
(TargetNotBuildable, _) ->
renderListCommaAnd
[ "the " ++ showComponentName availableTargetComponentName
| AvailableTarget{availableTargetComponentName} <- targets'
]
++ plural (listPlural targets') " is " " are all "
++ "marked as 'buildable: False'"
(TargetNotLocal, _) ->
renderListCommaAnd
[ "the " ++ showComponentName availableTargetComponentName
| AvailableTarget{availableTargetComponentName} <- targets'
]
++ " cannot be built because cabal does not currently support "
++ "building test suites or benchmarks of non-local dependencies"
(TargetBuildable () TargetNotRequestedByDefault, Just stanza) ->
renderListCommaAnd
[ "the " ++ showComponentName availableTargetComponentName
| AvailableTarget{availableTargetComponentName} <- targets'
]
++ " will not be built because "
++ renderOptionalStanza Plural stanza
++ " are not built by default in the current configuration (but you "
++ "can still build them specifically)" -- TODO: say how
_ ->
error $
"renderBuildTargetProblem: unexpected status "
++ show (status, mstanza)
| ((status, mstanza), targets') <- sortGroupOn groupingKey targets
]
where
groupingKey t =
( availableTargetStatus t
, case availableTargetStatus t of
TargetNotBuildable -> Nothing
TargetNotLocal -> Nothing
_ -> optionalStanza (availableTargetComponentName t)
)
------------------------------------------------------------
-- Rendering error messages for TargetProblemNoneEnabled
--
-- | Several commands have a @TargetProblemNoTargets@ problem constructor.
-- This renders an error message for those cases.
renderTargetProblemNoTargets :: String -> TargetSelector -> String
renderTargetProblemNoTargets verb targetSelector =
"Cannot "
++ verb
++ " "
++ renderTargetSelector targetSelector
++ " because "
++ reason targetSelector
++ ". "
++ "Check the .cabal "
++ plural
(targetSelectorPluralPkgs targetSelector)
"file for the package and make sure that it properly declares "
"files for the packages and make sure that they properly declare "
++ "the components that you expect."
where
reason (TargetPackage _ _ Nothing) =
"it does not contain any components at all"
reason (TargetPackage _ _ (Just kfilter)) =
"it does not contain any " ++ renderComponentKind Plural kfilter
reason (TargetPackageNamed _ Nothing) =
"it does not contain any components at all"
reason (TargetPackageNamed _ (Just kfilter)) =
"it does not contain any " ++ renderComponentKind Plural kfilter
reason (TargetAllPackages Nothing) =
"none of them contain any components at all"
reason (TargetAllPackages (Just kfilter)) =
"none of the packages contain any "
++ renderComponentKind Plural kfilter
reason ts@TargetComponent{} =
error $ "renderTargetProblemNoTargets: " ++ show ts
reason ts@TargetComponentUnknown{} =
error $ "renderTargetProblemNoTargets: " ++ show ts
-----------------------------------------------------------
-- Rendering error messages for CannotPruneDependencies
--
renderCannotPruneDependencies :: CannotPruneDependencies -> String
renderCannotPruneDependencies (CannotPruneDependencies brokenPackages) =
"Cannot select only the dependencies (as requested by the "
++ "'--only-dependencies' flag), "
++ ( case pkgids of
[pkgid] -> "the package " ++ prettyShow pkgid ++ " is "
_ ->
"the packages "
++ renderListCommaAnd (map prettyShow pkgids)
++ " are "
)
++ "required by a dependency of one of the other targets."
where
-- throw away the details and just list the deps that are needed
pkgids :: [PackageId]
pkgids = nub . map packageId . concatMap snd $ brokenPackages
{-
++ "Syntax:\n"
++ " - build [package]\n"
++ " - build [package:]component\n"
++ " - build [package:][component:]module\n"
++ " - build [package:][component:]file\n"
++ " where\n"
++ " package is a package name, package dir or .cabal file\n\n"
++ "Examples:\n"
++ " - build foo -- package name\n"
++ " - build tests -- component name\n"
++ " (name of library, executable, test-suite or benchmark)\n"
++ " - build Data.Foo -- module name\n"
++ " - build Data/Foo.hsc -- file name\n\n"
++ "An ambiguous target can be qualified by package, component\n"
++ "and/or component kind (lib|exe|test|bench|flib)\n"
++ " - build foo:tests -- component qualified by package\n"
++ " - build tests:Data.Foo -- module qualified by component\n"
++ " - build lib:foo -- component qualified by kind"
-}