From d55ce2a5f69a9b92b1e2825f48f2c68781aa64aa Mon Sep 17 00:00:00 2001 From: Herbert Valerio Riedel Date: Mon, 17 Dec 2018 20:23:59 +0100 Subject: [PATCH] Extend `Distribution.Simple.Utils.rewriteFileEx` from ASCII to UTF-8 encoding This takes care of knock-off effects of #2557 Specifically, the `Paths_*.hs` and `cabal_macros.h` files would result being incorrectly by a `rewriteFileEx` which isn't UTF-8 capable. Now the `cabal_macros.h` file is written out exactly like the `.h` file generated internally by `ghc` is generated; note however that standard CPP doesn't support non-ASCII characters in CPP symbols and will thus not work with a standard CPP preprocessor. --- Cabal/ChangeLog.md | 1 + Cabal/Distribution/Simple/Utils.hs | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Cabal/ChangeLog.md b/Cabal/ChangeLog.md index b614e9e7c1a..910a2116760 100644 --- a/Cabal/ChangeLog.md +++ b/Cabal/ChangeLog.md @@ -10,6 +10,7 @@ * Add a `LibraryVisibility` field to `InstalledPackageInfo` * Fix corrupted config file header for non-ASCII package names ([2557](https://github.com/haskell/cabal/issues/2557)). + * Extend `Distribution.Simple.Utils.rewriteFileEx` from ASCII to UTF-8 encoding. ---- diff --git a/Cabal/Distribution/Simple/Utils.hs b/Cabal/Distribution/Simple/Utils.hs index 7acd49befa1..17ed16e83fb 100644 --- a/Cabal/Distribution/Simple/Utils.hs +++ b/Cabal/Distribution/Simple/Utils.hs @@ -205,7 +205,7 @@ import Control.Concurrent.MVar ( newEmptyMVar, putMVar, takeMVar ) import Data.Typeable ( cast ) -import qualified Data.ByteString.Lazy.Char8 as BS.Char8 +import qualified Data.ByteString.Lazy as BS import System.Directory ( Permissions(executable), getDirectoryContents, getPermissions @@ -1401,19 +1401,22 @@ rewriteFile = rewriteFileEx normal -- the same as the existing content then leave the file as is so that we do not -- update the file's modification time. -- --- NB: the file is assumed to be ASCII-encoded. +-- NB: Before Cabal-3.0 the file content was assumed to be +-- ASCII-representable. Since Cabal-3.0 the file is assumed to be +-- UTF-8 encoded. rewriteFileEx :: Verbosity -> FilePath -> String -> IO () rewriteFileEx verbosity path newContent = flip catchIO mightNotExist $ do - existingContent <- annotateIO verbosity $ readFile path - _ <- evaluate (length existingContent) - unless (existingContent == newContent) $ + existingContent <- annotateIO verbosity $ BS.readFile path + _ <- evaluate (BS.length existingContent) + unless (existingContent == newContent') $ annotateIO verbosity $ - writeFileAtomic path (BS.Char8.pack newContent) + writeFileAtomic path newContent' where + newContent' = toUTF8LBS newContent + mightNotExist e | isDoesNotExistError e - = annotateIO verbosity $ writeFileAtomic path - (BS.Char8.pack newContent) + = annotateIO verbosity $ writeFileAtomic path newContent' | otherwise = ioError e