11-- Copyright (c) 2019 The DAML Authors. All rights reserved.
22-- SPDX-License-Identifier: Apache-2.0
3- {-# LANGUAGE CPP #-} -- To get precise GHC version
3+ {-# LANGUAGE CPP #-} -- To get precise GHC version and check if distributed binary
44{-# LANGUAGE TemplateHaskell #-}
55{-# OPTIONS_GHC -Wno-dodgy-imports #-} -- GHC no longer exports def in GHC 8.6 and above
66{-# LANGUAGE DeriveGeneric #-}
@@ -58,7 +58,7 @@ import GHC hiding (def)
5858import GHC.Check ( VersionCheck (.. ), makeGhcVersionChecker )
5959-- import GhcMonad
6060import HIE.Bios.Cradle
61- import HIE.Bios.Environment (addCmdOpts , makeDynFlagsAbsolute )
61+ import HIE.Bios.Environment (addCmdOpts , makeDynFlagsAbsolute , getRuntimeGhcLibDir )
6262import HIE.Bios.Types
6363import HscTypes (HscEnv (.. ), ic_dflags )
6464import qualified Language.Haskell.LSP.Core as LSP
@@ -316,10 +316,9 @@ loadSession dir = do
316316 -- If the hieYaml file already has an HscEnv, the new component is
317317 -- combined with the components in the old HscEnv into a new HscEnv
318318 -- which contains both.
319- packageSetup <- return $ \ (hieYaml, cfp, opts) -> do
319+ packageSetup <- return $ \ (hieYaml, cfp, opts, libDir ) -> do
320320 -- Parse DynFlags for the newly discovered component
321- libdir <- getGhcLibDir opts
322- hscEnv <- emptyHscEnv libdir nc
321+ hscEnv <- emptyHscEnv libDir nc
323322 (df, targets) <- evalGhcEnv hscEnv $ do
324323 setOptions opts (hsc_dflags hscEnv)
325324 dep_info <- getDependencyInfo (componentDependencies opts)
@@ -355,7 +354,7 @@ loadSession dir = do
355354 -- It's important to keep the same NameCache though for reasons
356355 -- that I do not fully understand
357356 print (" Making new HscEnv" ++ (show inplace))
358- hscEnv <- emptyHscEnv libdir nc
357+ hscEnv <- emptyHscEnv libDir nc
359358 newHscEnv <-
360359 -- Add the options for the current component to the HscEnv
361360 evalGhcEnv hscEnv $ do
@@ -372,8 +371,8 @@ loadSession dir = do
372371 pure (Map. insert hieYaml (newHscEnv, new_deps) m, (newHscEnv, head new_deps', tail new_deps'))
373372
374373
375- session <- return $ \ (hieYaml, cfp, opts) -> do
376- (hscEnv, new, old_deps) <- packageSetup (hieYaml, cfp, opts)
374+ session <- return $ \ (hieYaml, cfp, opts, libDir ) -> do
375+ (hscEnv, new, old_deps) <- packageSetup (hieYaml, cfp, opts, libDir )
377376 -- TODO Handle the case where there is no hie.yaml
378377 -- Make a map from unit-id to DynFlags, this is used when trying to
379378 -- resolve imports.
@@ -447,19 +446,34 @@ loadSession dir = do
447446 void $ forkIO $ do
448447 putStrLn $ " Consulting the cradle for " <> show file
449448 cradle <- maybe (loadImplicitCradle $ addTrailingPathSeparator dir) loadCradle hieYaml
449+
450+ -- we don't want to use ghc-paths if this is a portable, distributed binary
451+ mLibDir <- getRuntimeGhcLibDir cradle
452+ # ifdef DIST_BINARY
453+ True
454+ # else
455+ False
456+ # endif
457+
450458 eopts <- cradleToSessionOpts cradle cfp
451459 print eopts
452- case eopts of
453- Right opts -> do
454- (cs, res) <- session (hieYaml, toNormalizedFilePath' cfp, opts)
460+
461+ let ncfp = toNormalizedFilePath' cfp
462+ cradleMishap diags = do
463+ dep_info <- getDependencyInfo ([fp | Just fp <- [hieYaml]])
464+ let res = (diags, Nothing )
465+ modifyVar_ fileToFlags $ \ var -> do
466+ pure $ Map. insertWith HM. union hieYaml (HM. singleton ncfp (res, dep_info)) var
467+ signalBarrier finished_barrier ([(ncfp, (res, dep_info) )], res)
468+
469+ case (eopts, mLibDir) of
470+ (Right opts, Just libDir) -> do
471+ (cs, res) <- session (hieYaml, toNormalizedFilePath' cfp, opts, libDir)
455472 signalBarrier finished_barrier (cs, fst res)
456- Left err -> do
457- dep_info <- getDependencyInfo ([fp | Just fp <- [hieYaml]])
458- let ncfp = toNormalizedFilePath' cfp
459- let res = (map (renderCradleError ncfp) err, Nothing )
460- modifyVar_ fileToFlags $ \ var -> do
461- pure $ Map. insertWith HM. union hieYaml (HM. singleton ncfp (res, dep_info)) var
462- signalBarrier finished_barrier ([(ncfp, (res, dep_info) )], res)
473+
474+ (Left err, _) -> cradleMishap $ map (renderCradleError ncfp) err
475+ (_, Nothing ) -> cradleMishap [ideErrorText ncfp " Couldn't get the GHC library directory" ]
476+
463477 waitBarrier finished_barrier
464478
465479 dummyAs <- async $ return (error " Uninitialised" )
@@ -615,7 +629,7 @@ memoIO op = do
615629 Just res -> return (mp, res)
616630
617631setOptions :: GhcMonad m => ComponentOptions -> DynFlags -> m (DynFlags , [Target ])
618- setOptions (ComponentOptions theOpts compRoot _deps _mlibdir ) dflags = do
632+ setOptions (ComponentOptions theOpts compRoot _deps) dflags = do
619633 (dflags_, targets) <- addCmdOpts theOpts dflags
620634 let dflags' = makeDynFlagsAbsolute compRoot dflags_
621635 let dflags'' =
0 commit comments