Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ghc exceptions #65

Merged
merged 6 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions hie-plugin-api/Haskell/Ide/Engine/Ghc.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE LambdaCase #-}
-- | This module provides the interface to GHC, mainly for loading
Expand Down Expand Up @@ -35,7 +34,6 @@ import Haskell.Ide.Engine.PluginUtils

import DynFlags
import GHC
import IOEnv as G
import qualified HscTypes
import Outputable (renderWithStyle)
import Language.Haskell.LSP.Types ( NormalizedUri(..), toNormalizedUri )
Expand All @@ -49,7 +47,6 @@ import Outputable hiding ((<>))
-- to do with BIOS
import qualified HIE.Bios.Ghc.Api as BIOS (withDynFlags, setDeferTypeErrors)
import qualified HIE.Bios.Ghc.Load as BIOS
import qualified HIE.Bios.Flags as BIOS (CradleError)

import System.Directory

Expand Down Expand Up @@ -187,28 +184,6 @@ logDiag rfm eref dref df reason sev spn style msg = do
modifyIORef' eref (msgTxt:)
return ()


errorHandlers :: (String -> m a) -> (HscTypes.SourceError -> m a) -> [ErrorHandler m a]
errorHandlers ghcErrRes renderSourceError = handlers
where
-- ghc throws GhcException, SourceError, GhcApiError and
-- IOEnvFailure. hie-bios throws CradleError.
handlers =
[ ErrorHandler $ \(ex :: IOEnvFailure) ->
ghcErrRes (show ex)
, ErrorHandler $ \(ex :: HscTypes.GhcApiError) ->
ghcErrRes (show ex)
, ErrorHandler $ \(ex :: HscTypes.SourceError) ->
renderSourceError ex
, ErrorHandler $ \(ex :: IOError) ->
ghcErrRes (show ex)
, ErrorHandler $ \(ex :: BIOS.CradleError) ->
ghcErrRes (show ex)
, ErrorHandler $ \(ex :: GhcException) ->
ghcErrRes (showGhcException ex "")
]


-- | Load a module from a filepath into the cache, first check the cache
-- to see if it's already there.
setTypecheckedModule :: Uri -> IdeGhcM (IdeResult (Diagnostics, AdditionalErrs))
Expand Down
28 changes: 28 additions & 0 deletions hie-plugin-api/Haskell/Ide/Engine/GhcUtils.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Haskell.Ide.Engine.GhcUtils where

import qualified Language.Haskell.LSP.Core as Core

import qualified HscMain as G
import Module
import HscTypes
import GHC
import IOEnv as G
import qualified Data.Text as T

import qualified HIE.Bios.Flags as BIOS (CradleError)

import Haskell.Ide.Engine.PluginUtils (ErrorHandler(..))

-- Convert progress continuation to a messager
toMessager :: (Core.Progress -> IO ()) -> G.Messager
toMessager k _hsc_env (nk, n) _rc_reason ms =
Expand All @@ -32,3 +39,24 @@ toMessager hsc_env mod_index recomp mod_summary =
(recompileRequired recomp) mod_summary)
++ reason
-}

-- Handlers for each type of error that ghc can throw
errorHandlers :: (String -> m a) -> (HscTypes.SourceError -> m a) -> [ErrorHandler m a]
errorHandlers onGhcError onSourceError = handlers
where
-- ghc throws GhcException, SourceError, GhcApiError and
-- IOEnvFailure. hie-bios throws CradleError.
handlers =
[ ErrorHandler $ \(ex :: IOEnvFailure) ->
onGhcError (show ex)
, ErrorHandler $ \(ex :: GhcApiError) ->
onGhcError (show ex)
, ErrorHandler $ \(ex :: SourceError) ->
onSourceError ex
, ErrorHandler $ \(ex :: IOError) ->
onGhcError (show ex)
, ErrorHandler $ \(ex :: BIOS.CradleError) ->
onGhcError (show ex)
, ErrorHandler $ \(ex :: GhcException) ->
onGhcError (showGhcException ex "")
]
17 changes: 13 additions & 4 deletions hie-plugin-api/Haskell/Ide/Engine/ModuleCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,20 @@ loadCradle iniDynFlags (NewCradle fp) def action = do
-- while 'f' is still valid.
liftIO (GHC.newHscEnv iniDynFlags) >>= GHC.setSession
liftIO $ setCurrentDirectory (BIOS.cradleRootDir cradle)
init_res <- gcatches (Right <$> init_session)
[ErrorHandler (\(ex :: GHC.GhcException)
-> return $ Left (GHC.showGhcException ex ""))]

let onGhcError = return . Left
let onSourceError srcErr = do
logm $ "Source error on cradle initialisation: " ++ show srcErr
return $ Right ()
-- We continue setting the cradle in case the file has source errors
-- cause they will be reported to user by diagnostics
init_res <- gcatches
(Right <$> init_session)
(errorHandlers onGhcError onSourceError)

case init_res of
Left err -> do
logm $ "GhcException on cradle initialisation: " ++ show err
logm $ "Ghc error on cradle initialisation: " ++ show err
return $ IdeResultFail $ IdeError
{ ideCode = OtherError
, ideMessage = Text.pack $ show err
Expand All @@ -198,6 +206,7 @@ loadCradle iniDynFlags (NewCradle fp) def action = do
-- `.hi` files will be saved.
Right () -> do
setCurrentCradle cradle
logm $ "Cradle set succesfully"
IdeResultOk <$> action

-- | Sets the current cradle for caching.
Expand Down
8 changes: 4 additions & 4 deletions src/Haskell/Ide/Engine/Scheduler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ ghcDispatcher env@DispatcherEnv { docVersionTVar } errorHandler callbackHandler

let
runner :: a -> IdeGhcM a -> IdeGhcM (IdeResult a)
runner d act = case context of
Nothing -> runActionWithContext iniDynFlags Nothing d act
runner a act = case context of
Nothing -> runActionWithContext iniDynFlags Nothing a act
Just uri -> case uriToFilePath uri of
Just fp -> runActionWithContext iniDynFlags (Just fp) d act
Just fp -> runActionWithContext iniDynFlags (Just fp) a act
Nothing -> do
debugm
"ghcDispatcher:Got malformed uri, running action with default context"
runActionWithContext iniDynFlags Nothing d act
runActionWithContext iniDynFlags Nothing a act

let
runWithCallback = do
Expand Down