Skip to content

Commit cc3ac98

Browse files
committed
Parse ghc version from cabal path
1 parent f843b95 commit cc3ac98

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)
@@ -591,10 +591,13 @@ cabalCradle :: LogAction IO (WithSeverity Log) -> ResolvedCradles b -> FilePath
591591
cabalCradle l cs wdir mc projectFile = do
592592
res <- runCradleResultT $ callCabalPathForCompilerPath l (cradleBuildToolVersions cs) wdir projectFile
593593
let
594-
ghcPath = case res of
595-
CradleSuccess path -> path
594+
cabalPathOutput = case res of
595+
CradleSuccess out -> out
596596
_ -> Nothing
597597

598+
ghcPath = fst <$> cabalPathOutput
599+
ghcVersion = snd =<< cabalPathOutput
600+
598601
runGhcCmd args = runCradleResultT $ do
599602
case ghcPath of
600603
Just p -> readProcessWithCwd_ l wdir p args ""
@@ -610,7 +613,7 @@ cabalCradle l cs wdir mc projectFile = do
610613
pure $ CradleAction
611614
{ actionName = Types.Cabal
612615
, runCradle = \fp ls -> do
613-
v <- getGhcVersion runGhcCmd
616+
v <- maybe (getGhcVersion runGhcCmd) (pure . Just) ghcVersion
614617
runCradleResultT $ cabalAction cs wdir ghcPath v mc l projectFile fp ls
615618
, runGhcCmd = runGhcCmd
616619
}
@@ -853,22 +856,30 @@ cabalGhcDirs l cabalProject workDir = do
853856
where
854857
projectFileArgs = projectFileProcessArgs cabalProject
855858

856-
callCabalPathForCompilerPath :: LogAction IO (WithSeverity Log) -> BuildToolVersions -> FilePath -> CradleProjectConfig -> CradleLoadResultT IO (Maybe FilePath)
859+
callCabalPathForCompilerPath :: LogAction IO (WithSeverity Log) -> BuildToolVersions -> FilePath -> CradleProjectConfig -> CradleLoadResultT IO (Maybe (FilePath, Maybe Version))
857860
callCabalPathForCompilerPath l vs workDir projectFile = do
858861
case isCabalPathSupported vs of
859862
False -> pure Nothing
860863
True -> do
861864
let
862865
args = ["path", "--output-format=json"] <> projectFileProcessArgs projectFile
863866
bs = BS.fromStrict . T.encodeUtf8 . T.pack
864-
parse_compiler_path = Aeson.parseEither ((.: "compiler") >=> (.: "path")) <=< Aeson.eitherDecode
865-
866867
compiler_info <- readProcessWithCwd_ l workDir "cabal" args ""
867-
case parse_compiler_path (bs compiler_info) of
868+
let
869+
parsed = do
870+
json <- Aeson.eitherDecode (bs compiler_info)
871+
flip Aeson.parseEither json $ \o -> do
872+
c <- o .: "compiler"
873+
p <- c .: "path"
874+
i <- c .:? "id"
875+
let v = versionMaybe . T.unpack . T.takeWhileEnd (/= '-') . T.pack =<< i
876+
pure (p, v)
877+
878+
case parsed of
868879
Left err -> do
869880
liftIO $ l <& WithSeverity (LogCabalPath $ T.pack err) Warning
870881
pure Nothing
871-
Right a -> pure a
882+
Right a -> pure $ Just a
872883

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

0 commit comments

Comments
 (0)