Opened 3 years ago

Closed 3 years ago

#8601 closed bug (fixed)

runghc from standard input and --ghc-args

Reported by: wuzzeb Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.6.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


I am trying to use runghc with a cabal sandbox, and it works when the haskell code is in a file like follows:

runghc -- -package-db --ghc-arg=.cabal-sandbox/i386-linux-ghc-7.6.3-packages.conf.d somefile.hs

But if I try and load the script from standard input (actually I am trying to run a GHC script from a bash script using "runghc << EOF" but it is easier to see if you just run from the console)

runghc -- -package-db --ghc-arg=.cabal-sandbox/i386-linux-ghc-7.6.3-packages.conf.d < somefile.hs

you get an error

    Not in scope: `main'
    Perhaps you meant `min' (imported from Prelude)

Looking at the utils/runghc/runghc.hs source file, what happens is inside the doIt function. The first thing the doIt function does is call getGhcArgs which strips the --ghc-args. Then it checks the args and sees there is no file, so falls into the case where it reads standard input, creates a temp file, and then recalls doIt. Here is the problem, because the "--ghc-arg" string has already been stripped so the second call to doIt is where the error occurs.

With that, I can actually get it to work with the following :)

runghc -- -package-db --ghc-arg=--ghc-arg=.cabal-sandbox/i386-linux-ghc-7.6.3-packages.conf.d < somefile.hs

By passing --ghc-arg= twice, it is stripped once the first time doIt is called and once the second time and GHC gets the correct args :)

Attachments (2)

Change History (6)

comment:1 Changed 3 years ago by wuzzeb

Status: newpatch

I fixed the bug in the attached patch. I also kept around the ability to use --ghc-arg=--ghc-arg= as a prefix for backwards compatibility, although I have no strong feelings so if you want to take it out that is fine.

comment:2 Changed 3 years ago by Austin Seipp <austin@…>

In ec4af3fbcd47ca3af1727e70fa20e7cb8db0fb41/ghc:

runghc: Fix interaction of stdin and --ghc-args

When reading the program from standard input, runghc did not properly
handle the --ghc-arg= escape for arguments to ghc which do not start
with a dash, since arguments were processed twice and the first time the
--ghc-arg= was stripped.  Now arguments are only processed once.  For
backwards compatibility, a prefix of --ghc-arg=--ghc-arg= is allowed
since this prefix will work on both old and new versions of ghc.

This fixes #8601

Signed-off-by: Austin Seipp <>

comment:3 Changed 3 years ago by Austin Seipp <austin@…>

In f50c270457a2b9fbf831ab53081e111c84156bf9/testsuite:

Tests for #8601

Signed-off-by: Austin Seipp <>

comment:4 Changed 3 years ago by thoughtpolice

Resolution: fixed
Status: patchclosed

Merged (with a fixed test.) Thanks!

Note: See TracTickets for help on using tickets.