@@ -105,7 +105,7 @@ import qualified Stack.Upload as Upload
105105import qualified System.Directory as D
106106import System.Environment (getProgName , getArgs , withArgs )
107107import System.Exit
108- import System.FilePath (pathSeparator )
108+ import System.FilePath (isRelative , isValid , pathSeparator )
109109import System.IO (hIsTerminalDevice , stderr , stdin , stdout , hSetBuffering , BufferMode (.. ), hPutStrLn , hGetEncoding , hSetEncoding )
110110
111111-- | Change the character encoding of the given Handle to transliterate
@@ -767,7 +767,8 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
767767 (ExecRunGhc , args) ->
768768 getGhcCmd " run" menv eoPackages args
769769 munlockFile lk -- Unlock before transferring control away.
770- exec menv cmd args
770+
771+ runWithPath eoCwd $ exec menv cmd args
771772 where
772773 -- return the package-id of the first package in GHC_PACKAGE_PATH
773774 getPkgId menv wc name = do
@@ -788,6 +789,20 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
788789 pkgopts <- getPkgOpts menv wc pkgs
789790 return (prefix ++ compilerExeName wc, pkgopts ++ args)
790791
792+ runWithPath path callback = case path of
793+ Nothing -> callback
794+ Just p | not (isValid p) -> callback
795+ Just p ->
796+ if isRelative p
797+ then parseRelDir p >>= runInDirectory
798+ else parseAbsDir p >>= runInDirectory
799+ where
800+ runInDirectory :: (Path t Dir ) -> RIO EnvConfig ()
801+ runInDirectory directory =
802+ withUnliftIO $ \ unlift ->
803+ withCurrentDir directory $ unliftIO unlift callback
804+
805+
791806-- | Evaluate some haskell code inline.
792807evalCmd :: EvalOpts -> GlobalOpts -> IO ()
793808evalCmd EvalOpts {.. } go@ GlobalOpts {.. } = execCmd execOpts go
0 commit comments