Skip to content

Commit 31f7e60

Browse files
committed
Add custom cache layer for session loading
Achieves this by adding a HashMap from NormalizedFilePath to its respective hie.yaml location. This works nicely with the existing implementation, but increases the memory usage by an considerable amount. This change is motivated by haskell/hie-bios#264 where we set the build-dir for cabal based projects to some config directory in order to avoid recompilation issues whenever users invoked `cabal build`. However, this uncovered an issue in the existing code-base, as HLS will ask for the compilation options for generated modules, such as `Paths_*`, by looking for the responsible `hie.yaml` file and using an internal cache for the options. This used to be fine, until the aforementioned hie-bios change, since now `Paths_*` module will be in some `~/.cache/hie-bios/...` directory, which has no responsible `hie.yaml` which is why we see error messages such: `No hie.yaml found, use implicit hie-bios`.
1 parent fde10fa commit 31f7e60

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import NameCache
7070
import Packages
7171
import Control.Exception (evaluate)
7272
import Data.Void
73+
import Control.Applicative (Alternative((<|>)))
7374

7475

7576
data CacheDirs = CacheDirs
@@ -113,6 +114,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
113114
hscEnvs <- newVar Map.empty :: IO (Var HieMap)
114115
-- Mapping from a Filepath to HscEnv
115116
fileToFlags <- newVar Map.empty :: IO (Var FlagsMap)
117+
filesMap <- newVar HM.empty :: IO (Var FilesMap)
116118
-- Version of the mappings above
117119
version <- newVar 0
118120
let returnWithVersion fun = IdeGhcSession fun <$> liftIO (readVar version)
@@ -271,6 +273,8 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
271273

272274
modifyVar_ fileToFlags $ \var -> do
273275
pure $ Map.insert hieYaml (HM.fromList (concatMap toFlagsMap all_targets)) var
276+
modifyVar_ filesMap $ \var -> do
277+
pure $ HM.union var (HM.fromList (zip (map fst $ concatMap toFlagsMap all_targets) (repeat hieYaml)))
274278

275279
extendKnownTargets all_targets
276280

@@ -329,7 +333,9 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
329333
let res = (map (renderCradleError ncfp) err, Nothing)
330334
modifyVar_ fileToFlags $ \var -> do
331335
pure $ Map.insertWith HM.union hieYaml (HM.singleton ncfp (res, dep_info)) var
332-
return (res, maybe [] pure hieYaml ++ concatMap cradleErrorDependencies err)
336+
modifyVar_ filesMap $ \var -> do
337+
pure $ HM.insert ncfp hieYaml var
338+
return (res,[])
333339

334340
-- This caches the mapping from hie.yaml + Mod.hs -> [String]
335341
-- Returns the Ghc session and the cradle dependencies
@@ -358,9 +364,11 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
358364
-- before attempting to do so.
359365
let getOptions :: FilePath -> IO (IdeResult HscEnvEq, [FilePath])
360366
getOptions file = do
367+
ncfp <- toNormalizedFilePath' <$> canonicalizePath file
368+
cachedHieYamlLocation <- HM.lookup ncfp <$> readVar filesMap
361369
hieYaml <- cradleLoc file
362-
sessionOpts (hieYaml, file) `catch` \e ->
363-
return (([renderPackageSetupException file e], Nothing), maybe [] pure hieYaml)
370+
sessionOpts (join cachedHieYamlLocation <|> hieYaml, file) `catch` \e ->
371+
return (([renderPackageSetupException file e], Nothing),[])
364372

365373
returnWithVersion $ \file -> do
366374
opts <- liftIO $ join $ mask_ $ modifyVar runningCradle $ \as -> do
@@ -544,6 +552,7 @@ renderCradleError nfp (CradleError _ _ec t) =
544552
type DependencyInfo = Map.Map FilePath (Maybe UTCTime)
545553
type HieMap = Map.Map (Maybe FilePath) (HscEnv, [RawComponentInfo])
546554
type FlagsMap = Map.Map (Maybe FilePath) (HM.HashMap NormalizedFilePath (IdeResult HscEnvEq, DependencyInfo))
555+
type FilesMap = HM.HashMap NormalizedFilePath (Maybe FilePath)
547556

548557
-- This is pristine information about a component
549558
data RawComponentInfo = RawComponentInfo

0 commit comments

Comments
 (0)