Commit 5dd6a76f authored by Duncan Coutts's avatar Duncan Coutts
Browse files

Split TargetSelectorNoTargets case into two sub-cases

In resolveTargetSelectors we return [TargetSelectorProblem] which
includes a case TargetSelectorNoTargets. We split this into two cases:
TargetSelectorNoTargetsInCwd and TargetSelectorNoTargetsInProject.

The latter covers the important special case when there are no targets
in the project as a whole. The former covers the same remaining cases as
the original.

This is a useful special case since it corresponds to running commands
in a directory where there is no .cabal files and that is not underneath
a project. Add a hopefully useful error message for this case.

Also adjust the integration tests covering this case, which now split
into two separate cases.
parent fa47eec5
......@@ -424,8 +424,11 @@ resolveTargetSelectors :: [PackageInfo] -- any pkg in the cur dir
[TargetSelector PackageInfo])
-- default local dir target if there's no given target:
resolveTargetSelectors [] [] [] =
([TargetSelectorNoTargetsInProject], [])
resolveTargetSelectors [] _opinfo [] =
([TargetSelectorNoTargets], [])
([TargetSelectorNoTargetsInCwd], [])
resolveTargetSelectors ppinfo _opinfo [] =
([], [TargetPackage TargetImplicitCwd (head ppinfo) Nothing])
......@@ -443,14 +446,21 @@ resolveTargetSelector :: [PackageInfo] -> [PackageInfo]
resolveTargetSelector ppinfo opinfo targetStrStatus =
case findMatch (matcher targetStrStatus) of
Unambiguous _
| projectIsEmpty -> Left TargetSelectorNoTargetsInProject
Unambiguous (TargetPackage TargetImplicitCwd _ mkfilter)
| null ppinfo -> Left (TargetSelectorNoCurrentPackage targetStr)
| otherwise -> Right (TargetPackage TargetImplicitCwd
(head ppinfo) mkfilter)
--TODO: in future allow multiple packages in the same dir
Unambiguous target -> Right target
None errs -> Left (classifyMatchErrors errs)
Unambiguous target -> Right target
None errs
| projectIsEmpty -> Left TargetSelectorNoTargetsInProject
| otherwise -> Left (classifyMatchErrors errs)
Ambiguous exactMatch targets ->
case disambiguateTargetSelectors
matcher targetStrStatus exactMatch
......@@ -466,6 +476,8 @@ resolveTargetSelector ppinfo opinfo targetStrStatus =
targetStr = forgetFileStatus targetStrStatus
projectIsEmpty = null ppinfo && null opinfo
classifyMatchErrors errs
| not (null expected)
= let (things, got:_) = unzip expected in
......@@ -528,7 +540,8 @@ data TargetSelectorProblem
| TargetSelectorUnrecognised String
-- ^ Syntax error when trying to parse a target string.
| TargetSelectorNoCurrentPackage TargetString
| TargetSelectorNoTargets
| TargetSelectorNoTargetsInCwd
| TargetSelectorNoTargetsInProject
deriving (Show, Eq)
data QualLevel = QL1 | QL2 | QL3 | QLFull
......@@ -714,7 +727,7 @@ reportTargetSelectorProblems verbosity problems = do
--TODO: report a different error if there is a .cabal file but it's
-- not a member of the project
case [ () | TargetSelectorNoTargets <- problems ] of
case [ () | TargetSelectorNoTargetsInCwd <- problems ] of
[] -> return ()
_:_ ->
die' verbosity $
......@@ -723,6 +736,19 @@ reportTargetSelectorProblems verbosity problems = do
++ "project or specify packages or components by name or location. "
++ "See 'cabal build --help' for more details on target options."
case [ () | TargetSelectorNoTargetsInProject <- problems ] of
[] -> return ()
_:_ ->
die' verbosity $
"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"
++ "\n"
++ "For non-trivial projects you will also want a cabal.project "
++ "file in the root directory of your project. This file lists the "
++ "packages in your project and all other build configuration. "
++ "See the Cabal user guide for full details."
fail "reportTargetSelectorProblems: internal error"
where
......
......@@ -461,7 +461,7 @@ testTargetSelectorNoTargets :: Assertion
testTargetSelectorNoTargets = do
(_, _, _, localPackages, _) <- configureProject testdir config
Left errs <- readTargetSelectors localPackages []
errs @?= [TargetSelectorNoTargets]
errs @?= [TargetSelectorNoTargetsInCwd]
cleanProject testdir
where
testdir = "targets/complex"
......@@ -472,7 +472,7 @@ testTargetSelectorProjectEmpty :: Assertion
testTargetSelectorProjectEmpty = do
(_, _, _, localPackages, _) <- configureProject testdir config
Left errs <- readTargetSelectors localPackages []
errs @?= [TargetSelectorNoTargets]
errs @?= [TargetSelectorNoTargetsInProject]
cleanProject testdir
where
testdir = "targets/empty"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment