Skip to content

Commit 6885470

Browse files
author
Khan Thompson
committed
Changes based on feedback
* Use System.Directory.withCurrentDirectory * Throw an exception for invalid paths * Add in better options
1 parent 0d511e6 commit 6885470

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/Stack/Options/ExecParser.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,7 @@ execOptsExtraParser = eoPlainParser <|>
7575
eoCwdParser :: Parser (Maybe FilePath)
7676
eoCwdParser = optional
7777
(strOption (long "cwd"
78-
<> help "Sets the working directory before executing")
78+
<> help "Sets the working directory before executing"
79+
<> metavar "DIR"
80+
<> completer dirCompleter)
7981
)

src/main/Main.hs

Lines changed: 10 additions & 13 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 (isRelative, isValid, 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
@@ -789,19 +789,11 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
789789
pkgopts <- getPkgOpts menv wc pkgs
790790
return (prefix ++ compilerExeName wc, pkgopts ++ args)
791791

792+
runWithPath :: Maybe FilePath -> RIO EnvConfig () -> RIO EnvConfig ()
792793
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-
794+
Nothing -> callback
795+
Just p | not (isValid p) -> throwIO $ InvalidPathForExec p
796+
Just p -> withUnliftIO $ \ul -> D.withCurrentDirectory p $ unliftIO ul callback
805797

806798
-- | Evaluate some haskell code inline.
807799
evalCmd :: EvalOpts -> GlobalOpts -> IO ()
@@ -938,6 +930,7 @@ hpcReportCmd hropts go = withBuildConfig go $ generateHpcReportForTargets hropts
938930

939931
data MainException = InvalidReExecVersion String String
940932
| UpgradeCabalUnusable
933+
| InvalidPathForExec FilePath
941934
deriving (Typeable)
942935
instance Exception MainException
943936
instance Show MainException where
@@ -949,3 +942,7 @@ instance Show MainException where
949942
, "; found: "
950943
, actual]
951944
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)