diff --git a/Cabal/ChangeLog.md b/Cabal/ChangeLog.md index 9c041e3be65..b9dfefe4dd4 100644 --- a/Cabal/ChangeLog.md +++ b/Cabal/ChangeLog.md @@ -18,6 +18,9 @@ supports fully static linking; [`glibc` has some issues](https://sourceware.org/glibc/wiki/FAQ#Even_statically_linked_programs_need_some_shared_libraries_which_is_not_acceptable_for_me.__What_can_I_do.3F) with fully static linking. + * 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/Configure.hs b/Cabal/Distribution/Simple/Configure.hs index 328dc2ae095..ba860f24041 100644 --- a/Cabal/Distribution/Simple/Configure.hs +++ b/Cabal/Distribution/Simple/Configure.hs @@ -275,7 +275,7 @@ parseHeader header = case BLC8.words header of ["Saved", "package", "config", "for", pkgId, "written", "by", cabalId, "using", compId] -> fromMaybe (throw ConfigStateFileBadHeader) $ do - _ <- simpleParsec (BLC8.unpack pkgId) :: Maybe PackageIdentifier + _ <- simpleParsec (fromUTF8LBS pkgId) :: Maybe PackageIdentifier cabalId' <- simpleParsec (BLC8.unpack cabalId) compId' <- simpleParsec (BLC8.unpack compId) return (cabalId', compId') @@ -286,7 +286,7 @@ showHeader :: PackageIdentifier -- ^ The processed package. -> ByteString showHeader pkgId = BLC8.unwords [ "Saved", "package", "config", "for" - , BLC8.pack $ prettyShow pkgId + , toUTF8LBS $ prettyShow pkgId , "written", "by" , BLC8.pack $ prettyShow currentCabalId , "using" 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