From 155c16c5e0f7b6fd6c14b0b84bebd1d7933f8653 Mon Sep 17 00:00:00 2001 From: Mike Pilgrem Date: Wed, 7 Dec 2022 14:07:23 +0000 Subject: [PATCH] Fix Hpack #530 For the context to this pull request see [Hpack issue #530](https://github.com/sol/hpack/issues/530), the [Hpack PR #531](https://github.com/sol/hpack/pull/531) and the alternative [Hpack PR #535](https://github.com/sol/hpack/pull/535) (on which this PR builds). --- src/Pantry.hs | 22 +++++++++++++++++----- src/Pantry/Types.hs | 8 ++++---- stack.yaml | 3 ++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Pantry.hs b/src/Pantry.hs index 9218969d..432a624b 100644 --- a/src/Pantry.hs +++ b/src/Pantry.hs @@ -2,6 +2,7 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE LambdaCase #-} -- | Content addressable Haskell package management, providing for -- secure, reproducible acquisition of Haskell package contents and -- metadata. @@ -227,6 +228,16 @@ import Pantry.HTTP import Data.Char (isHexDigit) import Data.Time (getCurrentTime, diffUTCTime) +import Data.Yaml.Include (decodeFileWithWarnings) +import Hpack.Yaml (formatWarning) +import Hpack.Error (renderHpackError) + +decodeYaml :: FilePath -> IO (Either String ([String], Value)) +decodeYaml file = do + bimap displayException (first formatWarnings) <$> decodeFileWithWarnings file + where + formatWarnings = map (formatWarning file) + -- | Create a new 'PantryConfig' with the given settings. -- -- For something easier to use in simple cases, see 'runPantryApp'. @@ -741,15 +752,16 @@ hpack progName pkgDir = do he <- view $ pantryConfigL.to pcHpackExecutable case he of - HpackBundled -> do - r <- catchAny + HpackBundled -> ( liftIO - $ Hpack.hpackResult + $ Hpack.hpackResultWithError $ mHpackProgName + $ Hpack.setDecode decodeYaml $ Hpack.setTarget (toFilePath hpackFile) Hpack.defaultOptions - ) - ( throwIO . HpackLibraryException hpackFile ) + ) >>= \ case + Left err -> throwIO (HpackLibraryException hpackFile $ renderHpackError (fromMaybe "hpack" progName) err) + Right r -> do forM_ (Hpack.resultWarnings r) (logWarn . fromString) let cabalFile = fromString . Hpack.resultCabalFile $ r case Hpack.resultStatus r of diff --git a/src/Pantry/Types.hs b/src/Pantry/Types.hs index 45fd914d..9a904d2e 100644 --- a/src/Pantry/Types.hs +++ b/src/Pantry/Types.hs @@ -970,7 +970,7 @@ data PantryException | MigrationFailure !Text !(Path Abs File) !SomeException | InvalidTreeFromCasa !BlobKey !ByteString | ParseSnapNameException !Text - | HpackLibraryException !(Path Abs File) !SomeException + | HpackLibraryException !(Path Abs File) !String | HpackExeException !FilePath !(Path Abs Dir) !SomeException deriving Typeable @@ -1279,13 +1279,13 @@ instance Display PantryException where "Error: [S-994]\n" <> "Invalid snapshot name: " <> display t - display (HpackLibraryException file e) = + display (HpackLibraryException file err) = "Error: [S-305]\n" <> "Failed to generate a Cabal file using the Hpack library on file:\n" <> fromString (toFilePath file) <> "\n\n" - <> "The exception encountered was:\n\n" - <> fromString (show e) + <> "The error encountered was:\n\n" + <> fromString err display (HpackExeException fp dir e) = "Error: [S-720]\n" <> "Failed to generate a Cabal file using the Hpack executable:\n" diff --git a/stack.yaml b/stack.yaml index 8094e228..0be8567f 100644 --- a/stack.yaml +++ b/stack.yaml @@ -2,7 +2,8 @@ resolver: lts-18.28 extra-deps: -- hpack-0.35.0 +- git: https://github.com/sol/hpack + commit: 1ecc4158bdfc03609c88d0b1908fa9f41fdfbf5c ghc-options: "$locals": -fhide-source-paths