Skip to content

Commit a7e06a1

Browse files
committed
Setting current directory
1 parent 7af8f19 commit a7e06a1

4 files changed

Lines changed: 15 additions & 12 deletions

File tree

src/Client.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Network (PortID(UnixSocket))
1717
import System.Exit (exitFailure, exitWith)
1818
import System.IO (Handle, hClose, hFlush, hGetLine, hPutStrLn, stderr)
1919
import System.IO.Error (isDoesNotExistError)
20+
import System.Directory (getCurrentDirectory)
2021

2122
import Daemonize (daemonize)
2223
import Server (createListenSocket, startServer)
@@ -42,7 +43,8 @@ serverCommand sock cmd ghcOpts = do
4243
r <- tryJust (guard . isDoesNotExistError) (connect sock)
4344
case r of
4445
Right h -> do
45-
hPutStrLn h $ show (SrvCommand cmd ghcOpts)
46+
cwd <- getCurrentDirectory
47+
hPutStrLn h $ show (SrvCommand cwd cmd ghcOpts)
4648
hFlush h
4749
startClientReadLoop h
4850
Left _ -> do

src/CommandLoop.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Data.IORef
99
import Data.List (find)
1010
import MonadUtils (MonadIO, liftIO)
1111
import System.Exit (ExitCode(ExitFailure, ExitSuccess))
12+
import System.Directory (setCurrentDirectory)
1213
import qualified ErrUtils
1314
import qualified Exception (ExceptionMonad)
1415
import qualified GHC
@@ -18,7 +19,7 @@ import qualified Outputable
1819
import Types (ClientDirective(..), Command(..))
1920
import Info (getIdentifierInfo, getType)
2021

21-
type CommandObj = (Command, [String])
22+
type CommandObj = (FilePath, (Command, [String]))
2223

2324
type 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 ()
4849
startCommandLoop 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 ())

src/Server.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ clientSend currentClient clientDirective = do
5757
ignoreEPipe = handleJust (guard . isEPipe) (const $ return ())
5858
isEPipe = (==ResourceVanished) . ioeGetErrorType
5959

60-
getNextCommand :: IORef (Maybe Handle) -> Socket -> IO (Maybe (Command, [String]))
60+
getNextCommand :: IORef (Maybe Handle) -> Socket -> IO (Maybe (FilePath, (Command, [String])))
6161
getNextCommand currentClient sock = do
6262
checkCurrent <- readIORef currentClient
6363
case checkCurrent of
@@ -72,8 +72,8 @@ getNextCommand currentClient sock = do
7272
clientSend currentClient $ ClientUnexpectedError $
7373
"The client sent an invalid message to the server: " ++ show msg
7474
getNextCommand currentClient sock
75-
Just (SrvCommand cmd ghcOpts) -> do
76-
return $ Just (cmd, ghcOpts)
75+
Just (SrvCommand cwd cmd ghcOpts) -> do
76+
return $ Just (cwd, (cmd, ghcOpts))
7777
Just SrvStatus -> do
7878
mapM_ (clientSend currentClient) $
7979
[ ClientStdout "Server is running."

src/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Types
77
import System.Exit (ExitCode)
88

99
data ServerDirective
10-
= SrvCommand Command [String]
10+
= SrvCommand FilePath Command [String]
1111
| SrvStatus
1212
| SrvExit
1313
deriving (Read, Show)

0 commit comments

Comments
 (0)