GHCi 6.12.3 stdout redirection works for :type, but not for :info

Claus Reinke claus.reinke at
Wed Sep 22 11:05:09 EDT 2010

> The problem is that my code for redirecting the output
> of GHCi commands still works for things like :type, but
> no longer for things like :info (this is my smallest example
> demonstrating the effect).

The difference seems to be between commands that use
old-style GHCi output (works with redirection) versus
commands that output via Haskelline (redirection fails).

':type' calls typeOfExpr, which still uses 'printForUser',
which ultimately goes down to
'compiler/utils/Pretty.lhs:printDoc', where it just calls
'hPutChr/Str stdout'.

':info' calls 'info', which uses 'outputStrLn', which
ultimately goes down to
'Haskelline/Backend/Win32.hsc:putOut', where it
calls either 'WriteConsole' (if to terminal), or
'Data.ByteString.putStr' (no terminal).

The distinction between terminal and no terminal
is currently done only once per GHCi session, so
redirecting stdout to a file for just one command
will call 'WriteConsole' on a non-terminal handle
(redirecting the whole session works, though it is
useless for writing advanced GHCi commands).

I consider this a serious GHCi regression (never
mind the inconsistencies between GHCi commands),
as it breaks all the nice .ghci goodies from my old
tutorial (*) that people keep writing to me about.

If this analysis is correct, possible fixes would be:

- use 'printForUser/outputStrLn' consistently
    (why is the latter needed for some commands,
    but not for others?), and ensure that terminal
    status is checked for each GHCi command,
    instead of once per session (the main problem
    is the version of 'putOut' that goes into the
    'RunTerm structure's 'putStrOut' field - it is
    set by 'myRunTerm' in 'runInputTWithPrefs')

- expose a GHCi command that allows users
    to trigger Haskelline's terminal status check,
    and rewrite the redirection code accordingly

Sadly, I won't be able to implement either myself
at the moment - is there a chance of getting this
fixed for the 7.0.1 release (the first variant would
be the proper fix, the second variant a quick hack
to restore functionality)?



> With the attached GHCi script defining a
>    :redir <var> <command>
> command that should redirect the ouput of <command>
> into variable <var>, we get the output appended below
> (note the Exception for WriteConsole "handle is invalid",
> which is written into the temporary file when :info is
> called while stdout is redirected to that temporary file;
> in other words, the error message appears where the
> output of the command fails to appear).
> Is that an expected change with a known workaround,
> so that I just need to update my code, or is that
> unexpected behaviour that can only be fixed in GHCi?
> Claus
> PS. This is on Windows 7, if that matters.
> ------------- example session output
> $ -ignore-dot-ghci
> GHCi, version 6.12.3:  :? for help
> ..
> Prelude> :cmd readFile "redir.script"
> Prelude> :redir x :t map
> ..
> Prelude> x
> "-- logging GHCi stdout\nmap :: (a -> b) -> [a] -> [b]\n"
> Prelude> :redir y :i map
> Prelude> y
> "-- logging GHCi stdout\n*** Exception: WriteConsole: invalid argument 
> (Das
> Handle ist ung\252ltig.)\n"


More information about the Glasgow-haskell-users mailing list