Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 5e4a8c6

Browse files
committed
Add build-tool agnostic error message
1 parent ab761e7 commit 5e4a8c6

File tree

1 file changed

+61
-36
lines changed

1 file changed

+61
-36
lines changed

hie-plugin-api/Haskell/Ide/Engine/Cradle.hs

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,20 @@ findCabalHelperEntryPoint fp = do
238238
supported (Ex ProjLocV1Dir {}) _ cabalInstalled = cabalInstalled
239239
supported (Ex ProjLocV1CabalFile {}) _ cabalInstalled = cabalInstalled
240240

241-
isStackProject (Ex ProjLocStackYaml {}) = True
242-
isStackProject _ = False
241+
isStackProject :: Ex ProjLoc -> Bool
242+
isStackProject (Ex ProjLocStackYaml {}) = True
243+
isStackProject _ = False
243244

244-
isCabalV2FileProject (Ex ProjLocV2File {}) = True
245-
isCabalV2FileProject _ = False
245+
isCabalV2FileProject :: Ex ProjLoc -> Bool
246+
isCabalV2FileProject (Ex ProjLocV2File {}) = True
247+
isCabalV2FileProject _ = False
246248

247-
isCabalProject (Ex ProjLocV1CabalFile {}) = True
248-
isCabalProject (Ex ProjLocV1Dir {}) = True
249-
isCabalProject (Ex ProjLocV2File {}) = True
250-
isCabalProject (Ex ProjLocV2Dir {}) = True
251-
isCabalProject _ = False
249+
isCabalProject :: Ex ProjLoc -> Bool
250+
isCabalProject (Ex ProjLocV1CabalFile {}) = True
251+
isCabalProject (Ex ProjLocV1Dir {}) = True
252+
isCabalProject (Ex ProjLocV2File {}) = True
253+
isCabalProject (Ex ProjLocV2Dir {}) = True
254+
isCabalProject _ = False
252255

253256
{- | Given a FilePath, find the cradle the FilePath belongs to.
254257
@@ -476,6 +479,7 @@ cabalHelperCradle file = do
476479
CradleAction { actionName =
477480
"Cabal-Helper-" ++ actionNameSuffix
478481
, runCradle = \_ fp -> cabalHelperAction
482+
(Ex proj)
479483
env
480484
realPackage
481485
normalisedPackageLocation
@@ -497,24 +501,27 @@ cabalHelperCradle file = do
497501
else arg
498502
else arg
499503

500-
-- | cradle Action to query for the ComponentOptions that are needed
504+
-- | Cradle Action to query for the ComponentOptions that are needed
501505
-- to load the given FilePath.
502506
-- This Function is not supposed to throw any exceptions and use
503507
-- 'CradleLoadResult' to indicate errors.
504-
cabalHelperAction :: QueryEnv v -- ^ Query Env created by 'mkQueryEnv'
508+
cabalHelperAction :: Ex ProjLoc -- ^ Project location, can be used
509+
-- to present error build-tool
510+
-- agnostic error messages.
511+
-> QueryEnv v -- ^ Query Env created by 'mkQueryEnv'
505512
-- with the appropriate 'distdir'
506513
-> Package v -- ^ Package this cradle is part for.
507514
-> FilePath -- ^ Root directory of the cradle
508515
-- this action belongs to.
509516
-> FilePath -- ^ FilePath to load, expected to be an absolute path.
510517
-> IO (CradleLoadResult ComponentOptions)
511-
cabalHelperAction env package root fp = do
518+
cabalHelperAction proj env package root fp = do
512519
-- Get all unit infos the given FilePath may belong to
513520
let units = pUnits package
514521
-- make the FilePath to load relative to the root of the cradle.
515522
let relativeFp = makeRelative root fp
516523
debugm $ "Relative Module FilePath: " ++ relativeFp
517-
getComponent env (toList units) relativeFp
524+
getComponent proj env (toList units) relativeFp
518525
>>= \case
519526
Right comp -> do
520527
let fs' = getFlags comp
@@ -542,8 +549,8 @@ cabalHelperCradle file = do
542549
-- The given FilePath must be relative to the Root of the project
543550
-- the given units belong to.
544551
getComponent
545-
:: forall pt. QueryEnv pt -> [Unit pt] -> FilePath -> IO (Either [String] ChComponentInfo)
546-
getComponent env unitCandidates fp = getComponent' [] [] unitCandidates >>=
552+
:: forall pt. Ex ProjLoc -> QueryEnv pt -> [Unit pt] -> FilePath -> IO (Either [String] ChComponentInfo)
553+
getComponent proj env unitCandidates fp = getComponent' [] [] unitCandidates >>=
547554
\case
548555
(tried, failed, Nothing) -> return (Left $ buildErrorMsg tried failed)
549556
(_, _, Just comp) -> return (Right comp)
@@ -570,33 +577,51 @@ getComponent env unitCandidates fp = getComponent' [] [] unitCandidates >>=
570577

571578
buildErrorMsg :: [UnitInfo] -> [(Unit pt, IOException)] -> [String]
572579
buildErrorMsg triedUnits failedUnits =
573-
[ "Could not obtain flags for: \"" ++ fp ++ "\"."
580+
concat
581+
[ [ "Could not obtain flags for: \"" ++ fp ++ "\"."
574582
, ""
575583
]
576-
++ concat
577-
[
578-
[ "This Module was not part of any component we are aware of."
584+
, concat
585+
[ concat
586+
[ [ "This Module was not part of any component we are aware of."
579587
, ""
580588
]
581-
++ concatMap ppShowUnitInfo triedUnits
582-
++ [ ""
583-
, ""
584-
, "To expose a module, refer to:"
585-
, "https://www.haskell.org/cabal/users-guide/developing-packages.html"
586-
, ""
587-
]
588-
| not (null triedUnits)
589-
]
590-
++ concat
591-
[
592-
[ "We could not build all components."
593-
, "If one of these components exposes this Module, make sure they compile."
594-
, "You can try to invoke the commands yourself."
595-
, "The following commands failed:"
589+
, concatMap ppShowUnitInfo triedUnits
590+
, [ ""
591+
, ""
596592
]
597-
++ concatMap (ppShowIOException . snd) failedUnits
598-
| not (null failedUnits)
593+
, if isStackProject proj
594+
then stackSpecificInstructions
595+
else cabalSpecificInstructions
596+
]
597+
| not (null triedUnits)
598+
]
599+
, concat
600+
[
601+
[ "We could not build all components."
602+
, "If one of these components exposes this Module, make sure they compile."
603+
, "You can try to invoke the commands yourself."
604+
, "The following commands failed:"
599605
]
606+
++ concatMap (ppShowIOException . snd) failedUnits
607+
| not (null failedUnits)
608+
]
609+
]
610+
611+
stackSpecificInstructions :: [String]
612+
stackSpecificInstructions =
613+
[ "To expose a module, refer to:"
614+
, "https://docs.haskellstack.org/en/stable/GUIDE/"
615+
, "If you are using `package.yaml` then you don't have manually expose modules."
616+
, "Maybe you didn't set the source directories for your project correctly."
617+
]
618+
619+
cabalSpecificInstructions :: [String]
620+
cabalSpecificInstructions =
621+
[ "To expose a module, refer to:"
622+
, "https://www.haskell.org/cabal/users-guide/developing-packages.html"
623+
, ""
624+
]
600625

601626
ppShowUnitInfo :: UnitInfo -> [String]
602627
ppShowUnitInfo u =

0 commit comments

Comments
 (0)