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

Commit 42c9742

Browse files
committed
Add more sophisticated error messages
1 parent 8657dd4 commit 42c9742

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import Distribution.Helper (Package, projectPackages, pUnits,
1313
unChModuleName, Ex(..), ProjLoc(..),
1414
QueryEnv, mkQueryEnv, runQuery,
1515
Unit, unitInfo, uiComponents,
16-
ChEntrypoint(..), uComponentName)
16+
ChEntrypoint(..), uComponentName,
17+
UnitInfo(..))
1718
import Distribution.Helper.Discover (findProjects, getDefaultDistDir)
1819
import Data.Char (toLower)
1920
import Data.Function ((&))
20-
import Data.List (isPrefixOf, isInfixOf, sortOn, find, intercalate)
21+
import Data.List (isPrefixOf, isInfixOf, sortOn, find)
2122
import qualified Data.List.NonEmpty as NonEmpty
2223
import Data.List.NonEmpty (NonEmpty)
23-
import qualified Data.Map as M
24+
import qualified Data.Map as Map
2425
import Data.Maybe (listToMaybe, mapMaybe, isJust)
2526
import Data.Ord (Down(..))
2627
import Data.String (IsString(..))
@@ -146,7 +147,7 @@ getProjectGhcLibDir :: Cradle -> IO (Maybe FilePath)
146147
getProjectGhcLibDir crdl =
147148
execProjectGhc crdl ["--print-libdir"] >>= \case
148149
Nothing -> do
149-
logm "Could not obtain the libdir."
150+
errorm "Could not obtain the libdir."
150151
return Nothing
151152
mlibdir -> return mlibdir
152153

@@ -544,7 +545,7 @@ getComponent env unitCandidates fp = getComponent' [] [] unitCandidates >>=
544545
(tried, failed, Nothing) -> return (Left $ buildErrorMsg tried failed)
545546
(_, _, Just comp) -> return (Right comp)
546547
where
547-
getComponent' :: [Unit pt] -> [Unit pt] -> [Unit pt] -> IO ([Unit pt], [Unit pt], Maybe ChComponentInfo)
548+
getComponent' :: [UnitInfo] -> [(Unit pt, IOException)] -> [Unit pt] -> IO ([UnitInfo], [(Unit pt, IOException)], Maybe ChComponentInfo)
548549
getComponent' triedUnits failedUnits [] = return (triedUnits, failedUnits, Nothing)
549550
getComponent' triedUnits failedUnits (unit : units) =
550551
try (runQuery (unitInfo unit) env) >>= \case
@@ -556,15 +557,15 @@ getComponent env unitCandidates fp = getComponent' [] [] unitCandidates >>=
556557
++ fp
557558
++ "\" in the unit: "
558559
++ show unit
559-
getComponent' triedUnits (unit:failedUnits) units
560+
getComponent' triedUnits ((unit, e):failedUnits) units
560561
Right ui -> do
561-
let components = M.elems (uiComponents ui)
562+
let components = Map.elems (uiComponents ui)
562563
debugm $ "Unit Info: " ++ show ui
563564
case find (fp `partOfComponent`) components of
564-
Nothing -> getComponent' (unit:triedUnits) failedUnits units
565+
Nothing -> getComponent' (ui:triedUnits) failedUnits units
565566
comp -> return (triedUnits, failedUnits, comp)
566567

567-
buildErrorMsg :: [Unit pt] -> [Unit pt] -> [String]
568+
buildErrorMsg :: [UnitInfo] -> [(Unit pt, IOException)] -> [String]
568569
buildErrorMsg triedUnits failedUnits =
569570
[ "Could not obtain flags for: \"" ++ fp ++ "\"."
570571
, ""
@@ -573,20 +574,41 @@ getComponent env unitCandidates fp = getComponent' [] [] unitCandidates >>=
573574
[
574575
[ "This Module was not part of any component we are aware of."
575576
, ""
576-
, "If you dont know how to expose a module, take a look at: "
577+
, "If you dont know how to expose a module, take a look at:"
577578
, "https://www.haskell.org/cabal/users-guide/developing-packages.html"
578579
, ""
579580
]
581+
++ concatMap ppShowUnitInfo triedUnits
580582
| not (null triedUnits)
581583
]
582584
++ concat
583585
[
584586
[ "We could not build all components."
585587
, "If one of these components exposes this Module, make sure they compile."
588+
, "You can try to invoke the commands yourself."
589+
, "The following commands failed:"
586590
]
591+
++ concatMap (ppShowIOException . snd) failedUnits
587592
| not (null failedUnits)
588593
]
589594

595+
ppShowUnitInfo :: UnitInfo -> [String]
596+
ppShowUnitInfo u =
597+
u
598+
& uiComponents
599+
& Map.toList
600+
& map
601+
(\(name, info) ->
602+
"Component: " ++ show name ++ " with source directory: " ++ show (ciSourceDirs info)
603+
)
604+
605+
606+
ppShowIOException :: IOException -> [String]
607+
ppShowIOException e =
608+
[ ""
609+
, show e
610+
]
611+
590612
-- | Check whether the given FilePath is part of the Component.
591613
-- A FilePath is part of the Component if and only if:
592614
--

0 commit comments

Comments
 (0)