Skip to content

Commit 747af13

Browse files
SuganyaAKgbazmergify[bot]
authored
Remaining Cabal Package Exceptions (#9191)
* Remaining Cabal Package Exceptions * Accepting new error output with error code * Removing commented codes and adding constructor "RawSystemStdOut" in `Utils.hs` * Included a catch condition for VErboseException CabalException * Including "GetProgramInvocationException" with a `catch` condition for CabalException. * Rolling back "GetProgramInvocationException" --------- Co-authored-by: gbaz <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent fbd9642 commit 747af13

File tree

11 files changed

+206
-135
lines changed

11 files changed

+206
-135
lines changed

Cabal/src/Distribution/Simple/Errors.hs

Lines changed: 151 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Distribution.Types.BenchmarkType
3232
import Distribution.Types.LibraryName
3333
import Distribution.Types.PkgconfigVersion
3434
import Distribution.Types.TestType
35+
import Distribution.Types.VersionRange.Internal ()
3536
import Distribution.Version
3637
import Text.PrettyPrint
3738

@@ -144,6 +145,32 @@ data CabalException
144145
| CheckPackageProblems [String]
145146
| LibDirDepsPrefixNotRelative FilePath FilePath
146147
| CombinedConstraints Doc
148+
| CantParseGHCOutput
149+
| IncompatibleWithCabal String String
150+
| Couldn'tFindTestProgram FilePath
151+
| TestCoverageSupport
152+
| Couldn'tFindTestProgLibV09 FilePath
153+
| TestCoverageSupportLibV09
154+
| RawSystemStdout String
155+
| FindFileCwd FilePath
156+
| FindFileEx FilePath
157+
| FindModuleFileEx ModuleName [String] [FilePath]
158+
| MultipleFilesWithExtension String
159+
| NoDesc
160+
| MultiDesc [String]
161+
| RelocRegistrationInfo
162+
| CreatePackageDB
163+
| WithHcPkg String
164+
| RegisMultiplePkgNotSupported
165+
| RegisteringNotImplemented
166+
| NoTestSuitesEnabled
167+
| TestNameDisabled String
168+
| NoSuchTest String
169+
| ConfigureProgram String FilePath
170+
| RequireProgram String
171+
| NoProgramFound String VersionRange
172+
| BadVersionDb String Version VersionRange FilePath
173+
| UnknownVersionDb String VersionRange FilePath
147174
deriving (Show, Typeable)
148175

149176
exceptionCode :: CabalException -> Int
@@ -249,6 +276,37 @@ exceptionCode e = case e of
249276
CheckPackageProblems{} -> 5559
250277
LibDirDepsPrefixNotRelative{} -> 6667
251278
CombinedConstraints{} -> 5000
279+
CantParseGHCOutput{} -> 1980
280+
IncompatibleWithCabal{} -> 8123
281+
Couldn'tFindTestProgram{} -> 5678
282+
TestCoverageSupport{} -> 7890
283+
Couldn'tFindTestProgLibV09{} -> 9012
284+
TestCoverageSupportLibV09{} -> 1076
285+
RawSystemStdout{} -> 3098
286+
FindFileCwd{} -> 4765
287+
FindFileEx{} -> 2115
288+
FindModuleFileEx{} -> 6663
289+
MultipleFilesWithExtension{} -> 3333
290+
NoDesc{} -> 7654
291+
MultiDesc{} -> 5554
292+
RelocRegistrationInfo{} -> 4343
293+
CreatePackageDB{} -> 6787
294+
WithHcPkg{} -> 9876
295+
RegisMultiplePkgNotSupported{} -> 7632
296+
RegisteringNotImplemented{} -> 5411
297+
NoTestSuitesEnabled{} -> 9061
298+
TestNameDisabled{} -> 8210
299+
NoSuchTest{} -> 8000
300+
ConfigureProgram{} -> 5490
301+
RequireProgram{} -> 6666
302+
NoProgramFound{} -> 7620
303+
BadVersionDb{} -> 8038
304+
UnknownVersionDb{} -> 1008
305+
306+
versionRequirement :: VersionRange -> String
307+
versionRequirement range
308+
| isAnyVersion range = ""
309+
| otherwise = " version " ++ prettyShow range
252310

253311
exceptionMessage :: CabalException -> String
254312
exceptionMessage e = case e of
@@ -538,17 +596,17 @@ exceptionMessage e = case e of
538596
HowToFindInstalledPackages flv ->
539597
"don't know how to find the installed packages for "
540598
++ prettyShow flv
541-
PkgConfigNotFound pkg versionRequirement ->
599+
PkgConfigNotFound pkg versionReq ->
542600
"The pkg-config package '"
543601
++ pkg
544602
++ "'"
545-
++ versionRequirement
603+
++ versionReq
546604
++ " is required but it could not be found."
547-
BadVersion pkg versionRequirement v ->
605+
BadVersion pkg versionReq v ->
548606
"The pkg-config package '"
549607
++ pkg
550608
++ "'"
551-
++ versionRequirement
609+
++ versionReq
552610
++ " is required but the version installed on the"
553611
++ " system is version "
554612
++ prettyShow v
@@ -645,3 +703,92 @@ exceptionMessage e = case e of
645703
text "The following package dependencies were requested"
646704
$+$ nest 4 dispDepend
647705
$+$ text "however the given installed package instance does not exist."
706+
CantParseGHCOutput -> "Can't parse --info output of GHC"
707+
IncompatibleWithCabal compilerName packagePathEnvVar ->
708+
"Use of "
709+
++ compilerName
710+
++ "'s environment variable "
711+
++ packagePathEnvVar
712+
++ " is incompatible with Cabal. Use the "
713+
++ "flag --package-db to specify a package database (it can be "
714+
++ "used multiple times)."
715+
Couldn'tFindTestProgram cmd ->
716+
"Could not find test program \""
717+
++ cmd
718+
++ "\". Did you build the package first?"
719+
TestCoverageSupport -> "Test coverage is only supported for packages with a library component."
720+
Couldn'tFindTestProgLibV09 cmd ->
721+
"Could not find test program \""
722+
++ cmd
723+
++ "\". Did you build the package first?"
724+
TestCoverageSupportLibV09 -> "Test coverage is only supported for packages with a library component."
725+
RawSystemStdout errors -> errors
726+
FindFileCwd fileName -> fileName ++ " doesn't exist"
727+
FindFileEx fileName -> fileName ++ " doesn't exist"
728+
FindModuleFileEx mod_name extensions searchPath ->
729+
"Could not find module: "
730+
++ prettyShow mod_name
731+
++ " with any suffix: "
732+
++ show extensions
733+
++ " in the search path: "
734+
++ show searchPath
735+
MultipleFilesWithExtension buildInfoExt -> "Multiple files with extension " ++ buildInfoExt
736+
NoDesc ->
737+
"No cabal file found.\n"
738+
++ "Please create a package description file <pkgname>.cabal"
739+
MultiDesc l ->
740+
"Multiple cabal files found.\n"
741+
++ "Please use only one of: "
742+
++ intercalate ", " l
743+
RelocRegistrationInfo ->
744+
"Distribution.Simple.Register.relocRegistrationInfo: \
745+
\not implemented for this compiler"
746+
CreatePackageDB ->
747+
"Distribution.Simple.Register.createPackageDB: "
748+
++ "not implemented for this compiler"
749+
WithHcPkg name ->
750+
"Distribution.Simple.Register."
751+
++ name
752+
++ ":\
753+
\not implemented for this compiler"
754+
RegisMultiplePkgNotSupported -> "Registering multiple package instances is not yet supported for this compiler"
755+
RegisteringNotImplemented -> "Registering is not implemented for this compiler"
756+
NoTestSuitesEnabled ->
757+
"No test suites enabled. Did you remember to configure with "
758+
++ "\'--enable-tests\'?"
759+
TestNameDisabled tName ->
760+
"Package configured with test suite "
761+
++ tName
762+
++ " disabled."
763+
NoSuchTest tName -> "no such test: " ++ tName
764+
ConfigureProgram name path ->
765+
"Cannot find the program '"
766+
++ name
767+
++ "'. User-specified path '"
768+
++ path
769+
++ "' does not refer to an executable and "
770+
++ "the program is not on the system path."
771+
RequireProgram progName -> "The program '" ++ progName ++ "' is required but it could not be found."
772+
NoProgramFound progName versionRange ->
773+
"The program '"
774+
++ progName
775+
++ "'"
776+
++ versionRequirement versionRange
777+
++ " is required but it could not be found."
778+
BadVersionDb progName version range locationPath ->
779+
"The program '"
780+
++ progName
781+
++ "'"
782+
++ versionRequirement range
783+
++ " is required but the version found at "
784+
++ locationPath
785+
++ " is version "
786+
++ prettyShow version
787+
UnknownVersionDb progName versionRange locationPath ->
788+
"The program '"
789+
++ progName
790+
++ "'"
791+
++ versionRequirement versionRange
792+
++ " is required but the version of "
793+
++ locationPath
794+
++ " could not be determined."

Cabal/src/Distribution/Simple/GHC/Internal.hs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ module Distribution.Simple.GHC.Internal
5151
import Distribution.Compat.Prelude
5252
import Prelude ()
5353

54+
import Data.Bool (bool)
55+
import qualified Data.ByteString.Lazy.Char8 as BS
56+
import qualified Data.Map as Map
57+
import qualified Data.Set as Set
5458
import Distribution.Backpack
5559
import Distribution.Compat.Stack
5660
import qualified Distribution.InstalledPackageInfo as IPI
@@ -61,6 +65,7 @@ import Distribution.Parsec (simpleParsec)
6165
import Distribution.Pretty (prettyShow)
6266
import Distribution.Simple.BuildPaths
6367
import Distribution.Simple.Compiler
68+
import Distribution.Simple.Errors
6469
import Distribution.Simple.Flag (Flag (NoFlag), maybeToFlag, toFlag)
6570
import Distribution.Simple.GHC.ImplInfo
6671
import Distribution.Simple.LocalBuildInfo
@@ -69,6 +74,7 @@ import Distribution.Simple.Program.GHC
6974
import Distribution.Simple.Setup.Common (extraCompilationArtifacts)
7075
import Distribution.Simple.Utils
7176
import Distribution.System
77+
import Distribution.Types.ComponentId (ComponentId)
7278
import Distribution.Types.ComponentLocalBuildInfo
7379
import Distribution.Types.LocalBuildInfo
7480
import Distribution.Types.TargetInfo
@@ -78,12 +84,6 @@ import Distribution.Utils.Path
7884
import Distribution.Verbosity
7985
import Distribution.Version (Version)
8086
import Language.Haskell.Extension
81-
82-
import Data.Bool (bool)
83-
import qualified Data.ByteString.Lazy.Char8 as BS
84-
import qualified Data.Map as Map
85-
import qualified Data.Set as Set
86-
import Distribution.Types.ComponentId (ComponentId)
8787
import System.Directory (getDirectoryContents, getTemporaryDirectory)
8888
import System.Environment (getEnv)
8989
import System.FilePath
@@ -285,7 +285,7 @@ getGhcInfo verbosity _implInfo ghcProg = do
285285
| all isSpace ss ->
286286
return i
287287
_ ->
288-
die' verbosity "Can't parse --info output of GHC"
288+
dieWithException verbosity CantParseGHCOutput
289289

290290
getExtensions
291291
:: Verbosity
@@ -753,15 +753,7 @@ checkPackageDbEnvVar verbosity compilerName packagePathEnvVar = do
753753
(Just `fmap` getEnv name)
754754
`catchIO` const (return Nothing)
755755
abort =
756-
die' verbosity $
757-
"Use of "
758-
++ compilerName
759-
++ "'s environment variable "
760-
++ packagePathEnvVar
761-
++ " is incompatible with Cabal. Use the "
762-
++ "flag --package-db to specify a package database (it can be "
763-
++ "used multiple times)."
764-
756+
dieWithException verbosity $ IncompatibleWithCabal compilerName packagePathEnvVar
765757
_ = callStack -- TODO: output stack when erroring
766758

767759
profDetailLevelFlag :: Bool -> ProfDetailLevel -> Flag GhcProfAuto

Cabal/src/Distribution/Simple/Haddock.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ getHaddockProg verbosity programDb comp args quickJumpFlag = do
182182
-- various sanity checks
183183
when (hoogle && version < mkVersion [2, 2]) $
184184
dieWithException verbosity NoSupportForHoogle
185-
-- "Haddock 2.0 and 2.1 do not support the --hoogle flag."
186185

187186
when (fromFlag argQuickJump && version < mkVersion [2, 19]) $ do
188187
let msg = "Haddock prior to 2.19 does not support the --quickjump flag."
@@ -1116,7 +1115,7 @@ hscolour'
11161115
-> HscolourFlags
11171116
-> IO ()
11181117
hscolour' onNoHsColour haddockTarget pkg_descr lbi suffixes flags =
1119-
either onNoHsColour (\(hscolourProg, _, _) -> go hscolourProg)
1118+
either (\excep -> onNoHsColour $ exceptionMessage excep) (\(hscolourProg, _, _) -> go hscolourProg)
11201119
=<< lookupProgramVersion
11211120
verbosity
11221121
hscolourProgram

Cabal/src/Distribution/Simple/Program/Db.hs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ module Distribution.Simple.Program.Db
6363
import Distribution.Compat.Prelude
6464
import Prelude ()
6565

66-
import Distribution.Pretty
6766
import Distribution.Simple.Program.Builtin
6867
import Distribution.Simple.Program.Find
6968
import Distribution.Simple.Program.Types
@@ -75,6 +74,7 @@ import Distribution.Version
7574
import Data.Tuple (swap)
7675

7776
import qualified Data.Map as Map
77+
import Distribution.Simple.Errors
7878

7979
-- ------------------------------------------------------------
8080

@@ -348,16 +348,8 @@ configureProgram verbosity prog progdb = do
348348
else
349349
findProgramOnSearchPath verbosity (progSearchPath progdb) path
350350
>>= maybe
351-
(die' verbosity notFound)
351+
(dieWithException verbosity $ ConfigureProgram name path)
352352
(return . Just . swap . fmap UserSpecified . swap)
353-
where
354-
notFound =
355-
"Cannot find the program '"
356-
++ name
357-
++ "'. User-specified path '"
358-
++ path
359-
++ "' does not refer to an executable and "
360-
++ "the program is not on the system path."
361353
case maybeLocation of
362354
Nothing -> return progdb
363355
Just (location, triedLocations) -> do
@@ -437,10 +429,8 @@ requireProgram
437429
requireProgram verbosity prog progdb = do
438430
mres <- needProgram verbosity prog progdb
439431
case mres of
440-
Nothing -> die' verbosity notFound
432+
Nothing -> dieWithException verbosity $ RequireProgram (programName prog)
441433
Just res -> return res
442-
where
443-
notFound = "The program '" ++ programName prog ++ "' is required but it could not be found."
444434

445435
-- | Check that a program is configured and available to be run.
446436
--
@@ -477,51 +467,24 @@ lookupProgramVersion
477467
-> Program
478468
-> VersionRange
479469
-> ProgramDb
480-
-> IO (Either String (ConfiguredProgram, Version, ProgramDb))
470+
-> IO (Either CabalException (ConfiguredProgram, Version, ProgramDb))
481471
lookupProgramVersion verbosity prog range programDb = do
482472
-- If it's not already been configured, try to configure it now
483473
programDb' <- case lookupProgram prog programDb of
484474
Nothing -> configureProgram verbosity prog programDb
485475
Just _ -> return programDb
486476

487477
case lookupProgram prog programDb' of
488-
Nothing -> return $! Left notFound
478+
Nothing -> return $! Left $ NoProgramFound (programName prog) range
489479
Just configuredProg@ConfiguredProgram{programLocation = location} ->
490480
case programVersion configuredProg of
491481
Just version
492482
| withinRange version range ->
493483
return $! Right (configuredProg, version, programDb')
494484
| otherwise ->
495-
return $! Left (badVersion version location)
485+
return $! Left $ BadVersionDb (programName prog) version range (locationPath location)
496486
Nothing ->
497-
return $! Left (unknownVersion location)
498-
where
499-
notFound =
500-
"The program '"
501-
++ programName prog
502-
++ "'"
503-
++ versionRequirement
504-
++ " is required but it could not be found."
505-
badVersion v l =
506-
"The program '"
507-
++ programName prog
508-
++ "'"
509-
++ versionRequirement
510-
++ " is required but the version found at "
511-
++ locationPath l
512-
++ " is version "
513-
++ prettyShow v
514-
unknownVersion l =
515-
"The program '"
516-
++ programName prog
517-
++ "'"
518-
++ versionRequirement
519-
++ " is required but the version of "
520-
++ locationPath l
521-
++ " could not be determined."
522-
versionRequirement
523-
| isAnyVersion range = ""
524-
| otherwise = " version " ++ prettyShow range
487+
return $! Left $ UnknownVersionDb (programName prog) range (locationPath location)
525488

526489
-- | Like 'lookupProgramVersion', but raises an exception in case of error
527490
-- instead of returning 'Left errMsg'.
@@ -533,5 +496,5 @@ requireProgramVersion
533496
-> IO (ConfiguredProgram, Version, ProgramDb)
534497
requireProgramVersion verbosity prog range programDb =
535498
join $
536-
either (die' verbosity) return
499+
either (dieWithException verbosity) return
537500
`fmap` lookupProgramVersion verbosity prog range programDb

0 commit comments

Comments
 (0)