Skip to content

Commit 986bca5

Browse files
authored
Merge pull request #3497 from khanage/3264-add-cwd-to-exec
Implement #3264 adding --cwd to exec
2 parents 154e547 + 6885470 commit 986bca5

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Stack/Options/ExecParser.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ execOptsExtraParser = eoPlainParser <|>
4343
<$> eoEnvSettingsParser
4444
<*> eoPackagesParser
4545
<*> eoRtsOptionsParser
46+
<*> eoCwdParser
4647
where
4748
eoEnvSettingsParser :: Parser EnvSettings
4849
eoEnvSettingsParser = EnvSettings
@@ -70,3 +71,11 @@ execOptsExtraParser = eoPlainParser <|>
7071
eoPlainParser = flag' ExecOptsPlain
7172
(long "plain" <>
7273
help "Use an unmodified environment (only useful with Docker)")
74+
75+
eoCwdParser :: Parser (Maybe FilePath)
76+
eoCwdParser = optional
77+
(strOption (long "cwd"
78+
<> help "Sets the working directory before executing"
79+
<> metavar "DIR"
80+
<> completer dirCompleter)
81+
)

src/Stack/Types/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ data ExecOptsExtra
430430
{ eoEnvSettings :: !EnvSettings
431431
, eoPackages :: ![String]
432432
, eoRtsOptions :: ![String]
433+
, eoCwd :: !(Maybe FilePath)
433434
}
434435
deriving (Show)
435436

src/main/Main.hs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ import qualified Stack.Upload as Upload
105105
import qualified System.Directory as D
106106
import System.Environment (getProgName, getArgs, withArgs)
107107
import System.Exit
108-
import System.FilePath (pathSeparator)
108+
import System.FilePath (isValid, pathSeparator)
109109
import 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.
792799
evalCmd :: EvalOpts -> GlobalOpts -> IO ()
793800
evalCmd EvalOpts {..} go@GlobalOpts {..} = execCmd execOpts go
@@ -923,6 +930,7 @@ hpcReportCmd hropts go = withBuildConfig go $ generateHpcReportForTargets hropts
923930

924931
data MainException = InvalidReExecVersion String String
925932
| UpgradeCabalUnusable
933+
| InvalidPathForExec FilePath
926934
deriving (Typeable)
927935
instance Exception MainException
928936
instance 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

Comments
 (0)