Skip to content

Commit

Permalink
Merge pull request haskell#10387 from MercuryTechnologies/fix-testsui…
Browse files Browse the repository at this point in the history
…te-unicode

Fix Unicode output in `cabal-testsuite`
  • Loading branch information
mergify[bot] authored Oct 3, 2024
2 parents 504c7bc + c2b8f7b commit f92d607
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cabal-testsuite/src/Test/Cabal/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ runTestM mode m =
testSkipSetupTests = argSkipSetupTests (testCommonArgs args),
testHaveCabalShared = runnerWithSharedLib senv,
testEnvironment =
-- Try to avoid Unicode output
[ ("LC_ALL", Just "C")
-- Use UTF-8 output on all platforms.
[ ("LC_ALL", Just "en_US.UTF-8")
-- Hermetic builds (knot-tied)
, ("HOME", Just (testHomeDir env))
-- Set CABAL_DIR in addition to HOME, since HOME has no
Expand Down
23 changes: 23 additions & 0 deletions cabal-testsuite/src/Test/Cabal/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ runAction _verbosity mb_cwd env_overrides path0 args input action = do
mb_env <- getEffectiveEnvironment env_overrides
putStrLn $ "+ " ++ showCommandForUser path args
(readh, writeh) <- createPipe

-- `System.Process.createPipe` calls (through many intermediaries)
-- `GHC.IO.Handle.FD.fdToHandle`, whose documentation says:
--
-- > Makes a binary Handle. This is for historical reasons; it should
-- > probably be a text Handle with the default encoding and newline
-- > translation instead.
--
-- The documentation for `System.IO.hSetBinaryMode` says:
--
-- > This has the same effect as calling `hSetEncoding` with `char8`, together
-- > with `hSetNewlineMode` with `noNewlineTranslation`.
--
-- But this is a lie, and Unicode written to or read from binary handles is
-- always encoded or decoded as Latin-1, which is always the wrong choice.
--
-- Therefore, we explicitly set the output to UTF-8 to keep it consistent
-- between platforms and correct on all modern computers.
--
-- See: https://gitlab.haskell.org/ghc/ghc/-/issues/25307
hSetEncoding readh utf8
hSetEncoding writeh utf8

hSetBuffering readh LineBuffering
hSetBuffering writeh LineBuffering
let drain = do
Expand Down

0 comments on commit f92d607

Please sign in to comment.