Skip to content
Merged
Changes from all commits
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
34 changes: 24 additions & 10 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,32 @@ interpreterHandler
-> ParserFailure ParserHelp
-> IO (GlobalOptsMonoid, (GlobalOpts -> IO (), t))
interpreterHandler args f = do
isFile <- D.doesFileExist file
if isFile
then runInterpreterCommand file
else parseResultHandler (errorCombine (noSuchFile file))
-- args can include top-level config such as --extra-lib-dirs=... (set by
-- nix-shell) - we need to find the first argument which is a file, everything
-- afterwards is an argument to the script, everything before is an argument
-- to Stack
(stackArgs, fileArgs) <- spanM (fmap not . D.doesFileExist) args
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can use takeWhileM from monad-loops. monad-loops is already a dependency of stack via monad-logger.

The spanM function can be added to monad-loops package.

case fileArgs of
(file:fileArgs') -> runInterpreterCommand file stackArgs fileArgs'
[] -> parseResultHandler (errorCombine (noSuchFile firstArg))
where
file = head args
firstArg = head args

-- if the filename contains a path separator then we know that it is not a
-- command it is a file to be interpreted. In that case we only show the
spanM _ [] = return ([], [])
spanM p xs@(x:xs') = do
r <- p x
if r
then do
(ys, zs) <- spanM p xs'
return (x:ys, zs)
else
return ([], xs)

-- if the first argument contains a path separator then it might be a file,
-- or a Stack option referencing a file. In that case we only show the
-- interpreter error message and exclude the command related error messages.
errorCombine =
if elem pathSeparator file
if elem pathSeparator firstArg
then overrideErrorHelp
else vcatErrorHelp

Expand All @@ -520,11 +534,11 @@ interpreterHandler args f = do
noSuchFile name = errorHelp $ stringChunk
("File does not exist or is not a regular file `" ++ name ++ "'")

runInterpreterCommand path = do
runInterpreterCommand path stackArgs fileArgs = do
progName <- getProgName
iargs <- getInterpreterArgs path
let parseCmdLine = commandLineHandler progName True
let cmdArgs = iargs ++ "--" : args
let cmdArgs = stackArgs ++ iargs ++ "--" : path : fileArgs
-- TODO show the command in verbose mode
-- hPutStrLn stderr $ unwords $
-- ["Running", "[" ++ progName, unwords cmdArgs ++ "]"]
Expand Down