Skip to content

Commit

Permalink
Add explicit failure modes
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Nov 13, 2021
1 parent 5b4258c commit 2a55374
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cabal-install/src/Distribution/Client/CmdErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ renderTargetProblem verb _ (TargetProblemNoSuchComponent pkgid cname) =
++ "for the project plan, which would suggest an inconsistency "
++ "between readTargetSelectors and resolveTargets."

renderTargetProblem verb _ (TargetProblemNotSinglePackage []) =
"Internal error when trying to " ++ verb ++ ". Found no package."
renderTargetProblem verb _ (TargetProblemNotSinglePackage pkgids) =
"Internal error when trying to " ++ verb ++ ". Found multiple packages:\n"
++ unlines (map prettyShow pkgids)


------------------------------------------------------------
-- Renderering error messages for TargetProblemNoneEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,7 @@ resolveTargets selectPackageTargets selectComponentTarget
= Left (TargetProblemNoSuchPackage pkgid)

checkTarget (TargetPackage _ pkgids _)
= error ("TODO: add support for multiple packages in a directory. Got\n"
++ unlines (map prettyShow pkgids))
= Left (TargetProblemNotSinglePackage pkgids)
-- For the moment this error cannot happen here, because it gets
-- detected when the package config is being constructed. This case
-- will need handling properly when we do add support.
Expand Down
1 change: 1 addition & 0 deletions cabal-install/src/Distribution/Client/TargetProblem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data TargetProblem a
-- directly then of course it can.
| TargetProblemNoSuchPackage PackageId
| TargetProblemNoSuchComponent PackageId ComponentName
| TargetProblemNotSinglePackage [PackageId]

-- | A custom target problem
| CustomTargetProblem a
Expand Down
35 changes: 29 additions & 6 deletions cabal-install/src/Distribution/Client/TargetSelector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,11 @@ resolveTargetSelectors (KnownTargets{knownPackagesAll = []}) [] _ =
resolveTargetSelectors (KnownTargets{knownPackagesPrimary = []}) [] _ =
([TargetSelectorNoTargetsInCwd], [])

resolveTargetSelectors (KnownTargets{knownPackagesPrimary}) [] _ =
([], [TargetPackage TargetImplicitCwd pkgids Nothing])
resolveTargetSelectors (KnownTargets{knownPackagesPrimary}) [] _
| [pkgid] <- pkgids =
([], [TargetPackage TargetImplicitCwd [pkgid] Nothing])
| otherwise =
([TargetSelectorNotSinglePackage pkgids], [])
where
pkgids = [ pinfoId | KnownPackage{pinfoId} <- knownPackagesPrimary ]

Expand Down Expand Up @@ -593,6 +596,7 @@ data TargetSelectorProblem
| TargetSelectorUnrecognised String
-- ^ Syntax error when trying to parse a target string.
| TargetSelectorNoCurrentPackage TargetString
| TargetSelectorNotSinglePackage [PackageId]
| TargetSelectorNoTargetsInCwd
| TargetSelectorNoTargetsInProject
deriving (Show, Eq)
Expand Down Expand Up @@ -805,7 +809,29 @@ reportTargetSelectorProblems verbosity problems = do
case [ () | TargetSelectorNoTargetsInProject <- problems ] of
[] -> return ()
_:_ ->
die' verbosity $
die' verbosity noPackageErrorMessage

case [ pkgids | TargetSelectorNotSinglePackage pkgids <- problems ] of
[] -> return ()
mpkgids ->
let
allpkgids = concat mpkgids
in
case allpkgids of
[] ->
die' verbosity noPackageErrorMessage
pkgids ->
die' verbosity $
"Multiple packages have been found:\n "
++ unlines (map prettyShow pkgids)


where


fail "reportTargetSelectorProblems: internal error"
where
noPackageErrorMessage =
"There is no <pkgname>.cabal package file or cabal.project file. "
++ "To build packages locally you need at minimum a <pkgname>.cabal "
++ "file. You can use 'cabal init' to create one.\n"
Expand All @@ -815,9 +841,6 @@ reportTargetSelectorProblems verbosity problems = do
++ "packages in your project and all other build configuration. "
++ "See the Cabal user guide for full details."

fail "reportTargetSelectorProblems: internal error"


----------------------------------
-- Syntax type
--
Expand Down

0 comments on commit 2a55374

Please sign in to comment.