@@ -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 (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,12 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
788789 pkgopts <- getPkgOpts menv wc pkgs
789790 return (prefix ++ compilerExeName wc, pkgopts ++ args)
790791
792+ runWithPath :: Maybe FilePath -> RIO EnvConfig () -> RIO EnvConfig ()
793+ runWithPath path callback = case path of
794+ Nothing -> callback
795+ Just p | not (isValid p) -> throwIO $ InvalidPathForExec p
796+ Just p -> withUnliftIO $ \ ul -> D. withCurrentDirectory p $ unliftIO ul callback
797+
791798-- | Evaluate some haskell code inline.
792799evalCmd :: EvalOpts -> GlobalOpts -> IO ()
793800evalCmd EvalOpts {.. } go@ GlobalOpts {.. } = execCmd execOpts go
@@ -923,6 +930,7 @@ hpcReportCmd hropts go = withBuildConfig go $ generateHpcReportForTargets hropts
923930
924931data MainException = InvalidReExecVersion String String
925932 | UpgradeCabalUnusable
933+ | InvalidPathForExec FilePath
926934 deriving (Typeable )
927935instance Exception MainException
928936instance Show MainException where
@@ -934,3 +942,7 @@ instance Show MainException where
934942 , " ; found: "
935943 , actual]
936944 show UpgradeCabalUnusable = " --upgrade-cabal cannot be used when nix is activated"
945+ show (InvalidPathForExec path) = concat
946+ [ " Got an invalid --cwd argument for stack exec ("
947+ , path
948+ , " )" ]
0 commit comments