Skip to content

Commit 1beff3f

Browse files
committed
Enforce utf8
1 parent b22f772 commit 1beff3f

File tree

1 file changed

+9
-4
lines changed
  • plugins/hls-eval-plugin/src/Ide/Plugin/Eval

1 file changed

+9
-4
lines changed

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Data.String (IsString)
1212
import qualified Data.Text as T
1313
import Development.IDE.Types.Location (Position (..), Range (..))
1414
import GHC (ExecOptions, ExecResult (..),
15-
execStmt)
15+
execOptions, execStmt)
1616
import GHC.LanguageExtensions.Type (Extension (..))
1717
import GhcMonad (Ghc, liftIO, modifySession)
1818
import HscTypes
@@ -24,7 +24,7 @@ import Ide.Plugin.Eval.Types (Language (Plain), Loc,
2424
import InteractiveEval (getContext, parseImportDecl,
2525
runDecls, setContext)
2626
import Language.LSP.Types.Lens (line, start)
27-
import System.IO.Extra (newTempFile, readFile')
27+
import System.IO.Extra (newTempFile, readFileUTF8')
2828

2929
-- | Return the ranges of the expression and result parts of the given test
3030
testRanges :: Test -> (Range, Range)
@@ -95,8 +95,13 @@ evalExtensions =
9595
evalSetup :: Ghc ()
9696
evalSetup = do
9797
preludeAsP <- parseImportDecl "import qualified Prelude as P"
98+
encodingAsP <- parseImportDecl "import GHC.IO.Encoding as P"
9899
context <- getContext
99-
setContext (IIDecl preludeAsP : context)
100+
setContext (IIDecl encodingAsP : IIDecl preludeAsP : context)
101+
execStmt "setLocaleEncoding utf8" execOptions >>= \case
102+
ExecComplete (Left err) _ -> error $ "failed to set encoding in eval setup: " <> show err
103+
ExecComplete (Right _) _ -> pure ()
104+
ExecBreak{} -> error "breakpoints are not supported"
100105

101106
-- | A wrapper of 'InteractiveEval.execStmt', capturing the execution result
102107
myExecStmt :: String -> ExecOptions -> Ghc (Either String (Maybe String))
@@ -106,7 +111,7 @@ myExecStmt stmt opts = do
106111
modifySession $ \hsc -> hsc {hsc_IC = setInteractivePrintName (hsc_IC hsc) evalPrint}
107112
result <- execStmt stmt opts >>= \case
108113
ExecComplete (Left err) _ -> pure $ Left $ show err
109-
ExecComplete (Right _) _ -> liftIO $ Right . (\x -> if null x then Nothing else Just x ) <$> readFile' temp
114+
ExecComplete (Right _) _ -> liftIO $ Right . (\x -> if null x then Nothing else Just x) <$> readFileUTF8' temp
110115
ExecBreak{} -> pure $ Right $ Just "breakpoints are not supported"
111116
liftIO purge
112117
pure result

0 commit comments

Comments
 (0)