Skip to content

Commit 5941175

Browse files
committed
Avoid errors on non UTF-8 Windows
Problem ==== haddock exits with errors like below: `(1)` ``` haddock: internal error: <stderr>: hPutChar: invalid argument (invalid character) ``` `(2)` ``` haddock: internal error: Language\Haskell\HsColour\Anchors.hs: hGetContents: invalid argument (invalid byte sequence) ``` `(1)` is caused by printing [the "bullet" character](http://www.fileformat.info/info/unicode/char/2022/index.htm) onto stderr. For example, this warning contains it: ``` Language\Haskell\HsColour\ANSI.hs:62:10: warning: [-Wmissing-methods] • No explicit implementation for ‘toEnum’ • In the instance declaration for ‘Enum Highlight’ ``` `(2)` is caused when the input file of `readFile` contains some Unicode characters. In the case above, '⇒' is the cause. Environment ---- OS: Windows 10 haddock: 2.17.3 GHC: 8.0.1 Solution ==== Add `hSetEncoding handle utf8` to avoid the errors. Note ==== - I found the detailed causes by these changes for debugging: - haskell@8f29edb - haskell@1dd23bf - These errors happen even after executing `chcp 65001` on the console. According to the debug code, `hGetEncoding stderr` returns `CP932` regardless of the console encoding.
1 parent 58edf9f commit 5941175

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

haddock-api/src/Haddock/Interface.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import qualified Data.Set as Set
4848
import Distribution.Verbosity
4949
import System.Directory
5050
import System.FilePath
51+
import System.IO
5152
import Text.Printf
5253

5354
import Digraph
@@ -56,6 +57,7 @@ import Exception
5657
import GHC hiding (verbosity)
5758
import HscTypes
5859
import FastString (unpackFS)
60+
import MonadUtils (liftIO)
5961

6062
-- | Create 'Interface's and a link environment by typechecking the list of
6163
-- modules using the GHC API and processing the resulting syntax trees.
@@ -165,6 +167,7 @@ createIfaces verbosity flags instIfaceMap mods = do
165167
processModule :: Verbosity -> ModSummary -> [Flag] -> IfaceMap -> InstIfaceMap -> Ghc (Maybe Interface)
166168
processModule verbosity modsum flags modMap instIfaceMap = do
167169
out verbosity verbose $ "Checking module " ++ moduleString (ms_mod modsum) ++ "..."
170+
liftIO $ hSetEncoding stderr utf8
168171
tm <- loadModule =<< typecheckModule =<< parseModule modsum
169172
if not $ isBootSummary modsum then do
170173
out verbosity verbose "Creating interface..."

haddock-api/src/Haddock/Interface/Create.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import Control.DeepSeq (force)
4343
import Control.Exception (evaluate)
4444
import Control.Monad
4545
import Data.Traversable
46+
import Data.Function (on)
47+
import System.IO
4648

4749
import qualified Packages
4850
import qualified Module

0 commit comments

Comments
 (0)