When GHC is run in expression-evaluation mode (i.e. with the "-e" flag) and the expression does not compile, GHC correctly prints an error message, but then exits with exit status 0. For example:
$ ghc -e foo<interactive>:1:1: Not in scope: `foo'$ echo $?0
This is inconvenient for automated scripts that call GHC.
Trac metadata
Trac field
Value
Version
7.6.2
Type
FeatureRequest
TypeOfFailure
OtherFailure
Priority
normal
Resolution
Unresolved
Component
GHCi
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
GHC does what it was expected to do successfully. If it was to throw a non-zero code, it would indicate that there's something wrong with our usage of GHC or that it had some kind of other issue (and in fact we get ‘1’ when we try to compile a non-existent file).
GHC -e executes successfully and therefore exits with 0. It couldn't really care less about what happens during the session. For scripting, you probably want to write a Haskell program that does what you want and if it fails, throw whatever error code you want. It just isn't GHCs place to decide what it considers successful inside the session. Just my 2 cents.
It seems odd that GHC would return 1 in both of those cases, but return 0 for "ghc -e <invalid expression>". Either the return value in one or both of those cases should be changed to 0, or the return value for "ghc -e <invalid expression>" should be changed to 1.
"runghc" behaves like a call to "ghc -e main <some file>". If there is an error in the expression "main" (i.e. if "main" is not in scope) it returns 0:
$ cat >Test2.hsfoo = 1 $ runghc Test2.hsTest2.hs:1:33: Not in scope: `main' Perhaps you meant `min' (imported from Prelude)$ echo $?0
But if there is an error in the text of the program itself, or if the program throws an exception when "main" is run, then it returns 1.
I agree with the original report. The man page says "**-e expr** Evaluate expr; see for details." (#3351...) If expr doesn't compile, then ghc has failed to evaluate it, IMO.
In reality I can pass arbitrary GHCi commands to ghc -e and it will run them; but the documentation doesn't say anything about this (either in the man page or in the user's guide).
Making ghc -e expr return 1 on compile error would also be consistent with other languages:
rwbarton@adjunction:/tmp$ bash -c if ; echo $?bash: -c: line 1: syntax error: unexpected end of file1rwbarton@adjunction:/tmp$ python -c if ; echo $? File "<string>", line 1 if ^SyntaxError: invalid syntax1rwbarton@adjunction:/tmp$ perl -e if ; echo $?syntax error at -e line 1, at EOFExecution of -e aborted due to compilation errors.255rwbarton@adjunction:/tmp$ ruby -e if ; echo $?-e:1: syntax error, unexpected $end1rwbarton@adjunction:/tmp$ ghc -e if ; echo $?<interactive>:1:3: parse error (possibly incorrect indentation)0
Attempting to take this on as my first ticket, ran into a couple problems. Essentially, using -e eventually calls into code common between GHCi and -e.
All the code for the following is in ghc/InteractiveUI.hs
Relevant control enters in runGHCi, in the case maybe_exprs block. Both cases of the block (one with -e arguments, the other with normal interpreting) then call runCommands, which eventually filters down to GhciMonad.runStmt, which eventually handles the source errors.
I am unsure of where to insert the logic to return a nonzero exit code, as putting it in GhciMonad/related would also affect normal GHCi.
The test suite currently does not run on my Windows machine ("tee: standard output: Permission denied", followed by a hang), so I was unable to run the validation script.
As this is my first patch, please tell me what I did wrong :)
Don't forget to add a test to testsuite/tests/ghci. If the testsuite isn't working right now, search for a ticket about that, and if there isn't one, make one :-)
Attached Simon's suggestion, and also some *possible* test files (which are, ironically, untested). I am not submitting them as a .patch because of their untested nature, and more of a "here's an example of what it might look like".
Also, I thought it might be better if they go into testsuite/tests/ghc-e, not ghci.
I'll go submit a ticket for the failing test suite now.