@@ -9,6 +9,7 @@ import Data.IORef
99import Data.List (find )
1010import MonadUtils (MonadIO , liftIO )
1111import System.Exit (ExitCode (ExitFailure , ExitSuccess ))
12+ import System.Directory (setCurrentDirectory )
1213import qualified ErrUtils
1314import qualified Exception (ExceptionMonad )
1415import qualified GHC
@@ -18,7 +19,7 @@ import qualified Outputable
1819import Types (ClientDirective (.. ), Command (.. ))
1920import Info (getIdentifierInfo , getType )
2021
21- type CommandObj = (Command , [String ])
22+ type CommandObj = (FilePath , ( Command , [String ]) )
2223
2324type ClientSend = ClientDirective -> IO ()
2425
@@ -44,22 +45,22 @@ withWarnings state warningsValue action = do
4445 setWarnings :: Bool -> IO ()
4546 setWarnings val = modifyIORef state $ \ s -> s { stateWarningsEnabled = val }
4647
47- startCommandLoop :: IORef State -> ClientSend -> IO (Maybe CommandObj ) -> [String ] -> Maybe Command -> IO ()
48+ startCommandLoop :: IORef State -> ClientSend -> IO (Maybe CommandObj ) -> [String ] -> Maybe ( FilePath , Command ) -> IO ()
4849startCommandLoop state clientSend getNextCommand initialGhcOpts mbInitial = do
4950 continue <- GHC. runGhc (Just GHC.Paths. libdir) $ do
5051 configOk <- GHC. gcatch (configSession state clientSend initialGhcOpts >> return True )
5152 handleConfigError
5253 if configOk
5354 then do
54- doMaybe mbInitial $ \ cmd - > sendErrors (runCommand state clientSend cmd)
55+ doMaybe mbInitial $ \ (cwd, cmd) -> liftIO (setCurrentDirectory cwd) > > sendErrors (runCommand state clientSend cmd)
5556 processNextCommand False
5657 else processNextCommand True
5758
5859 case continue of
5960 Nothing ->
6061 -- Exit
6162 return ()
62- Just (cmd, ghcOpts) -> startCommandLoop state clientSend getNextCommand ghcOpts (Just cmd)
63+ Just (cwd, ( cmd, ghcOpts)) -> startCommandLoop state clientSend getNextCommand ghcOpts (Just (cwd, cmd) )
6364 where
6465 processNextCommand :: Bool -> GHC. Ghc (Maybe CommandObj )
6566 processNextCommand forceReconfig = do
@@ -68,10 +69,10 @@ startCommandLoop state clientSend getNextCommand initialGhcOpts mbInitial = do
6869 Nothing ->
6970 -- Exit
7071 return Nothing
71- Just (cmd, ghcOpts) ->
72+ Just (cwd, ( cmd, ghcOpts) ) ->
7273 if forceReconfig || (ghcOpts /= initialGhcOpts)
73- then return (Just (cmd, ghcOpts))
74- else sendErrors (runCommand state clientSend cmd) >> processNextCommand False
74+ then return (Just (cwd, ( cmd, ghcOpts) ))
75+ else sendErrors (liftIO (setCurrentDirectory cwd) >> runCommand state clientSend cmd) >> processNextCommand False
7576
7677 sendErrors :: GHC. Ghc () -> GHC. Ghc ()
7778 sendErrors action = GHC. gcatch action (\ x -> handleConfigError x >> return () )
0 commit comments