Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #3264 adding --cwd to exec #3497

Merged
merged 2 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Stack/Options/ExecParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ execOptsExtraParser = eoPlainParser <|>
<$> eoEnvSettingsParser
<*> eoPackagesParser
<*> eoRtsOptionsParser
<*> eoCwdParser
where
eoEnvSettingsParser :: Parser EnvSettings
eoEnvSettingsParser = EnvSettings
Expand Down Expand Up @@ -70,3 +71,9 @@ execOptsExtraParser = eoPlainParser <|>
eoPlainParser = flag' ExecOptsPlain
(long "plain" <>
help "Use an unmodified environment (only useful with Docker)")

eoCwdParser :: Parser (Maybe FilePath)
eoCwdParser = optional
(strOption (long "cwd"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add

               metavar "DIR" <>
               completer dirCompleter <>

For better help output and path completion for this option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

<> help "Sets the working directory before executing")
)
1 change: 1 addition & 0 deletions src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ data ExecOptsExtra
{ eoEnvSettings :: !EnvSettings
, eoPackages :: ![String]
, eoRtsOptions :: ![String]
, eoCwd :: !(Maybe FilePath)
}
deriving (Show)

Expand Down
19 changes: 17 additions & 2 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import qualified Stack.Upload as Upload
import qualified System.Directory as D
import System.Environment (getProgName, getArgs, withArgs)
import System.Exit
import System.FilePath (pathSeparator)
import System.FilePath (isRelative, isValid, pathSeparator)
import System.IO (hIsTerminalDevice, stderr, stdin, stdout, hSetBuffering, BufferMode(..), hPutStrLn, hGetEncoding, hSetEncoding)

-- | Change the character encoding of the given Handle to transliterate
Expand Down Expand Up @@ -767,7 +767,8 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
(ExecRunGhc, args) ->
getGhcCmd "run" menv eoPackages args
munlockFile lk -- Unlock before transferring control away.
exec menv cmd args

runWithPath eoCwd $ exec menv cmd args
where
-- return the package-id of the first package in GHC_PACKAGE_PATH
getPkgId menv wc name = do
Expand All @@ -788,6 +789,20 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
pkgopts <- getPkgOpts menv wc pkgs
return (prefix ++ compilerExeName wc, pkgopts ++ args)

runWithPath path callback = case path of
Nothing -> callback
Just p | not (isValid p) -> callback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it seems like this would silently ignore --cwd for invalid paths. Is there a reason for this? Seems to me like it should throw an error.

Just p ->
if isRelative p
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be cleaner to just use System.Directory.withCurrentDirectory. I wish path had RelOrAbs or something, but for now FilePath is the best way to represent having either.

then parseRelDir p >>= runInDirectory
else parseAbsDir p >>= runInDirectory
where
runInDirectory :: (Path t Dir) -> RIO EnvConfig ()
runInDirectory directory =
withUnliftIO $ \unlift ->
withCurrentDir directory $ unliftIO unlift callback


-- | Evaluate some haskell code inline.
evalCmd :: EvalOpts -> GlobalOpts -> IO ()
evalCmd EvalOpts {..} go@GlobalOpts {..} = execCmd execOpts go
Expand Down