GHC issueshttps://gitlab.haskell.org/ghc/ghc/-/issues2021-05-11T16:19:25Zhttps://gitlab.haskell.org/ghc/ghc/-/issues/6132Can't use both shebang line and #ifdef declarations in the same file.2021-05-11T16:19:25ZgfxmonkCan't use both shebang line and #ifdef declarations in the same file.I have an (admittedly awkward) script which can be compiled or interpreted.
If it's compiled, I want the full goodness. If it's interpreted, I want to run a "minimal" version (because if I don't have the compiled version, I probably don...I have an (admittedly awkward) script which can be compiled or interpreted.
If it's compiled, I want the full goodness. If it's interpreted, I want to run a "minimal" version (because if I don't have the compiled version, I probably don't have the required libraries either).
The following almost works:
```
module Main (main) where
#ifdef FANCY
import qualified System.Console.ANSI as Term
start = Term.setSGR [Term.SetColor Term.Foreground Term.Dull Term.Green]
end = Term.setSGR []
#else
start = return ()
end = return ()
#endif
main :: IO ()
main = do
start
putStrLn "hello world"
end
```
and then I can do:
```
$ runghc -cpp main.hs
hello world
^^ plain text
```
```
$ ghc -O -cpp -DFANCY main.hs
$ ./main
hello world
^^ green text (a.k.a "fancy")
```
I attempted to make this directly runnable by adding a shebang line of
{{{
\#!/usr/bin/runghc -cpp
}}}
But unfortunately that chokes with -cpp:
```
$ ghc -O -cpp -DFANCY main.hs
main.hs:1:0: error: invalid preprocessing directive #!
```
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ---------------- |
| Version | 7.0.4 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | Unknown/Multiple |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"Can't use both shebang line and #ifdef declarations in the same file.","status":"New","operating_system":"Unknown/Multiple","component":"Compiler","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"7.0.4","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"I have an (admittedly awkward) script which can be compiled or interpreted.\r\n\r\nIf it's compiled, I want the full goodness. If it's interpreted, I want to run a \"minimal\" version (because if I don't have the compiled version, I probably don't have the required libraries either).\r\n\r\nThe following almost works:\r\n\r\n\r\n\r\n{{{\r\n\r\nmodule Main (main) where\r\n#ifdef FANCY\r\nimport qualified System.Console.ANSI as Term\r\nstart = Term.setSGR [Term.SetColor Term.Foreground Term.Dull Term.Green]\r\nend = Term.setSGR []\r\n#else\r\nstart = return ()\r\nend = return ()\r\n#endif\r\nmain :: IO ()\r\nmain = do\r\n\tstart\r\n\tputStrLn \"hello world\"\r\n\tend\r\n}}}\r\n\r\n\r\nand then I can do:\r\n\r\n\r\n{{{\r\n$ runghc -cpp main.hs\r\nhello world\r\n^^ plain text\r\n}}}\r\n\r\n\r\n\r\n{{{\r\n$ ghc -O -cpp -DFANCY main.hs\r\n$ ./main\r\nhello world\r\n^^ green text (a.k.a \"fancy\")\r\n}}}\r\n\r\n\r\nI attempted to make this directly runnable by adding a shebang line of\r\n\r\n{{{\r\n#!/usr/bin/runghc -cpp\r\n}}}\r\n\r\n\r\nBut unfortunately that chokes with -cpp:\r\n\r\n{{{\r\n\r\n$ ghc -O -cpp -DFANCY main.hs\r\n\r\nmain.hs:1:0: error: invalid preprocessing directive #!\r\n}}}\r\n\r\n","type_of_failure":"OtherFailure","blocking":[]} -->8.10.2