简体   繁体   中英

How can I suppress “Failed, modules loaded:” in GHCi?

When refactoring a large project, I like to make the typechecker do the work for me by introducing the desired change, and then following the resulting type errors.

However, since the relevant type errors are displayed in GHCi before the Failed, modules loaded: output, a project with enough modules can cause the relevant errors to scroll off the top of even a generously-sized terminal.

Is there a flag or option to make GHCi suppress this output, or at least reorder it so that the errors appear last?

You could try compiling the file(s) using GHC instead of loading them in GHCi. That way, it won't try to load the files, but only compile them, thus only displaying the compile errors and warnings.

So, instead of this:

$ ghci -Wall -Werror
GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l foo.hs

... just do this:

$ ghc -Wall -Werror foo.hs

By the way, if you are using a terminal in UNIX/Linux, you can more easily change the font size, resize a terminal horizontally (which can't be done while a terminal is running by default on Windows), increase scroll-back, and otherwise increase the number of lines that you can see :)

I got desperate enough that I dug around in the code:

modulesLoadedMsg :: SuccessFlag -> [GHC.ModSummary] -> InputT GHCi ()
modulesLoadedMsg ok mods = do
  dflags <- getDynFlags
  unqual <- GHC.getPrintUnqual
  let mod_name mod = do
        is_interpreted <- GHC.isModuleInterpreted mod
        return $ if is_interpreted
                  then ppr (GHC.ms_mod mod)
                  else ppr (GHC.ms_mod mod)
                       <> text " ("
                       <> text (normalise $ msObjFilePath mod)
                       <> text ")" -- fix #9887
  mod_names <- mapM mod_name mods
  let mod_commas
        | null mods = text "none."
        | otherwise = hsep (punctuate comma mod_names) <> text "."
      status = case ok of
                   Failed    -> text "Failed"
                   Succeeded -> text "Ok"

      msg = status <> text ", modules loaded:" <+> mod_commas

  when (verbosity dflags > 0) $
     liftIO $ putStrLn $ showSDocForUser dflags unqual msg

Looks like the verbosity of 0 turns this off. You can set:

$ ghci -v0

or with cabal:

$ cabal repl --ghc-options="-v0"

or stack:

$ stack repl --ghci-options="-v0"

The sed workaround is a good idea, except that sed also receives Ctrl+c, breaking the pipe. Here's a variant which works around that problem:

cabal new-repl cabal-install | (trap '' SIGINT; sed -e '/Failed, modules loaded.*/d')

(Replace cabal new-repl with your favorite GHCi command.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM