Skip to content

Commit 00c31c8

Browse files
committed
Parse ghc version from cabal path
1 parent 9e60906 commit 00c31c8

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/HIE/Bios/Cradle.hs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import Control.Monad.Extra (unlessM)
4747
import Control.Monad.Trans.Cont
4848
import Control.Monad.Trans.Maybe
4949
import Control.Monad.IO.Class
50-
import Data.Aeson ((.:))
50+
import Data.Aeson ((.:), (.:?))
5151
import qualified Data.Aeson as Aeson
5252
import qualified Data.Aeson.Types as Aeson
5353
import Data.Bifunctor (first)
@@ -540,10 +540,13 @@ cabalCradle :: LogAction IO (WithSeverity Log) -> ResolvedCradles b -> FilePath
540540
cabalCradle l cs wdir mc projectFile = do
541541
res <- runCradleResultT $ callCabalPathForCompilerPath l (cradleBuildToolVersions cs) wdir projectFile
542542
let
543-
ghcPath = case res of
544-
CradleSuccess path -> path
543+
cabalPathOutput = case res of
544+
CradleSuccess out -> out
545545
_ -> Nothing
546546

547+
ghcPath = fst <$> cabalPathOutput
548+
ghcVersion = snd =<< cabalPathOutput
549+
547550
runGhcCmd args = runCradleResultT $ do
548551
case ghcPath of
549552
Just p -> readProcessWithCwd_ l wdir p args ""
@@ -559,7 +562,7 @@ cabalCradle l cs wdir mc projectFile = do
559562
pure $ CradleAction
560563
{ actionName = Types.Cabal
561564
, runCradle = \fp ls -> do
562-
v <- getGhcVersion runGhcCmd
565+
v <- maybe (getGhcVersion runGhcCmd) (pure . Just) ghcVersion
563566
runCradleResultT $ cabalAction cs wdir ghcPath v mc l projectFile fp ls
564567
, runGhcCmd = runGhcCmd
565568
}
@@ -808,22 +811,30 @@ cabalGhcDirs l cabalProject workDir = do
808811
where
809812
projectFileArgs = projectFileProcessArgs cabalProject
810813

811-
callCabalPathForCompilerPath :: LogAction IO (WithSeverity Log) -> BuildToolVersions -> FilePath -> CradleProjectConfig -> CradleLoadResultT IO (Maybe FilePath)
814+
callCabalPathForCompilerPath :: LogAction IO (WithSeverity Log) -> BuildToolVersions -> FilePath -> CradleProjectConfig -> CradleLoadResultT IO (Maybe (FilePath, Maybe Version))
812815
callCabalPathForCompilerPath l vs workDir projectFile = do
813816
case isCabalPathSupported vs of
814817
False -> pure Nothing
815818
True -> do
816819
let
817820
args = ["path", "--output-format=json"] <> projectFileProcessArgs projectFile
818821
bs = BS.fromStrict . T.encodeUtf8 . T.pack
819-
parse_compiler_path = Aeson.parseEither ((.: "compiler") >=> (.: "path")) <=< Aeson.eitherDecode
820-
821822
compiler_info <- readProcessWithCwd_ l workDir "cabal" args ""
822-
case parse_compiler_path (bs compiler_info) of
823+
let
824+
parsed = do
825+
json <- Aeson.eitherDecode (bs compiler_info)
826+
flip Aeson.parseEither json $ \o -> do
827+
c <- o .: "compiler"
828+
p <- c .: "path"
829+
i <- c .:? "id"
830+
let v = versionMaybe . T.unpack . T.takeWhileEnd (/= '-') . T.pack =<< i
831+
pure (p, v)
832+
833+
case parsed of
823834
Left err -> do
824835
liftIO $ l <& WithSeverity (LogCabalPath $ T.pack err) Warning
825836
pure Nothing
826-
Right a -> pure a
837+
Right a -> pure $ Just a
827838

828839
isCabalPathSupported :: BuildToolVersions -> Bool
829840
isCabalPathSupported = maybe False (>= makeVersion [3,14]) . cabalVersion

0 commit comments

Comments
 (0)