Skip to content

Commit b9cd6e8

Browse files
committed
User friendly errors: only show if cabal, show prefixes
1 parent cff3812 commit b9cd6e8

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import Data.Hashable
4242
import qualified Data.HashMap.Strict as HM
4343
import Data.IORef
4444
import Data.List
45-
import Data.List.Extra (dropPrefix, replace)
45+
import Data.List.Extra (dropPrefix, split)
4646
import qualified Data.Map.Strict as Map
4747
import Data.Maybe
4848
import Data.Proxy
@@ -77,6 +77,7 @@ import Development.IDE.Types.Logger (Pretty (pretty),
7777
import Development.IDE.Types.Options
7878
import GHC.Check
7979
import qualified HIE.Bios as HieBios
80+
import qualified HIE.Bios.Cradle as HieBios
8081
import HIE.Bios.Environment hiding (getCacheDir)
8182
import HIE.Bios.Types hiding (Log)
8283
import qualified HIE.Bios.Types as HieBios
@@ -664,7 +665,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
664665
Left err -> do
665666
dep_info <- getDependencyInfo (maybeToList hieYaml)
666667
let ncfp = toNormalizedFilePath' cfp
667-
let res = (map (renderCradleError ncfp) err, Nothing)
668+
let res = (map (renderCradleError cradle ncfp) err, Nothing)
668669
void $ modifyVar' fileToFlags $
669670
Map.insertWith HM.union hieYaml (HM.singleton ncfp (res, dep_info))
670671
void $ modifyVar' filesMap $ HM.insert ncfp hieYaml
@@ -907,46 +908,75 @@ setCacheDirs recorder CacheDirs{..} dflags = do
907908
& maybe id setODir oCacheDir
908909

909910

910-
renderCradleError :: NormalizedFilePath -> CradleError -> FileDiagnostic
911-
renderCradleError nfp (CradleError _ _ec ms) =
912-
ideErrorWithSource (Just "cradle") (Just DiagnosticSeverity_Error) nfp $ T.unlines $ map T.pack $ fromMaybe ms fileMissingMessage
911+
renderCradleError :: Cradle a -> NormalizedFilePath -> CradleError -> FileDiagnostic
912+
renderCradleError cradle nfp (CradleError _ _ec ms) =
913+
ideErrorWithSource (Just "cradle") (Just DiagnosticSeverity_Error) nfp $ T.unlines $ map T.pack userFriendlyMessage
913914
where
914915

916+
userFriendlyMessage :: [String]
917+
userFriendlyMessage
918+
| HieBios.isCabalCradle cradle = fromMaybe ms fileMissingMessage
919+
| otherwise = ms
920+
915921
fileMissingMessage :: Maybe [String]
916922
fileMissingMessage =
917923
multiCradleErrMessage <$> parseMultiCradleErr ms
918924

919925
-- | Information included in Multi Cradle error messages
920926
data MultiCradleErr = MultiCradleErr
921-
{ multiCradlePwd :: FilePath
922-
, multiCradleFilePath :: FilePath
923-
-- , multiCradlePrefixes :: [Prefix]
927+
{ mcPwd :: FilePath
928+
, mcFilePath :: FilePath
929+
, mcPrefixes :: [(FilePath, String)]
924930
} deriving (Show)
925931

926932
-- | Attempt to parse a multi-cradle message
927933
parseMultiCradleErr :: [String] -> Maybe MultiCradleErr
928934
parseMultiCradleErr ms = do
929-
_ <- prefix "Multi Cradle"
930-
wd <- prefix "pwd"
931-
fp <- prefix "filepath"
932-
pure $ MultiCradleErr wd fp
935+
_ <- lineAfter "Multi Cradle: "
936+
wd <- lineAfter "pwd: "
937+
fp <- lineAfter "filepath: "
938+
ps <- prefixes
939+
pure $ MultiCradleErr wd fp ps
933940

934941
where
935-
-- Extract the string after any line starting with "prefix: "
936-
prefix :: String -> Maybe String
937-
prefix pre = listToMaybe $ mapMaybe (stripPrefix (pre ++ ": ")) ms
942+
lineAfter :: String -> Maybe String
943+
lineAfter pre = listToMaybe $ mapMaybe (stripPrefix pre) ms
944+
945+
prefixes :: Maybe [(FilePath, String)]
946+
prefixes = do
947+
pure $ mapMaybe tuple ms
948+
949+
tuple :: String -> Maybe (String, String)
950+
tuple line = do
951+
line' <- surround '(' line ')'
952+
[f, s] <- pure $ split (==',') line'
953+
pure (f, s)
954+
955+
-- extracts the string surrounded by required characters
956+
surround :: Char -> String -> Char -> Maybe String
957+
surround start s end = do
958+
guard (listToMaybe s == Just start)
959+
guard (listToMaybe (reverse s) == Just end)
960+
pure $ drop 1 $ take (length s - 1) s
961+
962+
963+
964+
965+
938966

939967
multiCradleErrMessage :: MultiCradleErr -> [String]
940968
multiCradleErrMessage e =
941969
[ "Loading the module '" <> moduleFileName <> "' failed. It seems that it is not listed in your .cabal file!"
942970
, "Perhaps you need to add `"<> moduleName <> "` to other-modules or exposed-modules" -- named 'example' in example.cabal."
943971
, "For more information, visit: https://cabal.readthedocs.io/en/3.4/developing-packages.html#modules-included-in-the-package"
944-
]
972+
, ""
973+
] <> map prefix (mcPrefixes e)
945974
where
946-
localFilePath f = dropWhile (==pathSeparator) $ dropPrefix (multiCradlePwd e) f
947-
moduleFileName = localFilePath $ multiCradleFilePath e
975+
localFilePath f = dropWhile (==pathSeparator) $ dropPrefix (mcPwd e) f
976+
moduleFileName = localFilePath $ mcFilePath e
948977
moduleName = intercalate "." $ map dropExtension $ dropWhile isSourceFolder $ splitDirectories moduleFileName
949978
isSourceFolder p = all isLower $ take 1 p
979+
prefix (f, r) = f <> " - " <> r
950980

951981

952982

0 commit comments

Comments
 (0)