Skip to content

Commit e6ce0e1

Browse files
committed
fix: do not crash when some include path does not exists
1 parent 049296c commit e6ce0e1

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

ghcide/src/Development/IDE/Core/Rules.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ getModuleGraphRule recorder = defineEarlyCutOffNoFile (cmapWithPrio LogShake rec
625625
dependencyInfoForFiles (HashSet.toList fs)
626626

627627
{-# NOINLINE cacheVar #-}
628+
-- TODO: this should not use unsaferPerformIO
628629
cacheVar = unsafePerformIO (newTVarIO mempty)
629630

630631
getModulesPathsRule :: Recorder (WithPriority Log) -> Rules ()
@@ -650,7 +651,10 @@ getModulesPathsRule recorder = defineEarlyCutoff (cmapWithPrio LogShake recorder
650651

651652
-- TODO: we are taking/droping extension, this could be factorized to save a few cpu cycles ;)
652653
-- TODO: do acceptedextensions needs to be a set ? or a vector?
653-
modules <- fmap (\path -> (toModule path, toNormalizedFilePath' path)) . filter (\y -> takeExtension y `elem` acceptedExtensions) <$> liftIO (listFilesInside predicate dir)
654+
-- If the directory is empty, we return an empty list of modules
655+
-- using 'catch' instead of an exception which would kill the LSP
656+
modules <- (fmap (\path -> (toModule path, toNormalizedFilePath' path)) . filter (\y -> takeExtension y `elem` acceptedExtensions) <$> liftIO (listFilesInside predicate dir))
657+
`catch` (\(_ :: IOException) -> pure [])
654658
let isSourceModule (_, path) = "-boot" `isSuffixOf` fromNormalizedFilePath path
655659
let (sourceModules, notSourceModules) = partition isSourceModule modules
656660
pure $ (Map.fromList notSourceModules, Map.fromList sourceModules)

ghcide/src/Development/IDE/Import/FindImports.hs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,6 @@ data LocateResult
6969
| LocateFoundReexport UnitId
7070
| LocateFoundFile UnitId NormalizedFilePath
7171

72-
{-
73-
-- | locate a module in the file system. Where we go from *daml to Haskell
74-
locateModuleFile :: MonadIO m
75-
=> [(UnitId, [FilePath], S.Set ModuleName)]
76-
-> [String]
77-
-> (ModuleName -> NormalizedFilePath -> m (Maybe NormalizedFilePath))
78-
-> Bool
79-
-> ModuleName
80-
-> m LocateResult
81-
locateModuleFile import_dirss exts targetFor isSource modName = do
82-
let candidates import_dirs =
83-
[ toNormalizedFilePath' (prefix </> moduleNameSlashes modName <.> maybeBoot ext)
84-
| prefix <- import_dirs , ext <- exts]
85-
mf <- firstJustM go (concat [map (uid,) (candidates dirs) | (uid, dirs, _) <- import_dirss])
86-
case mf of
87-
Nothing ->
88-
case find (\(_ , _, reexports) -> S.member modName reexports) import_dirss of
89-
Just (uid,_,_) -> pure $ LocateFoundReexport uid
90-
Nothing -> pure LocateNotFound
91-
Just (uid,file) -> pure $ LocateFoundFile uid file
92-
where
93-
go (uid, candidate) = fmap ((uid,) <$>) $ targetFor modName candidate
94-
maybeBoot ext
95-
| isSource = ext ++ "-boot"
96-
| otherwise = ext
97-
-}
98-
9972
-- | This function is used to map a package name to a set of import paths.
10073
-- It only returns Just for unit-ids which are possible to import into the
10174
-- current module. In particular, it will return Nothing for 'main' components
@@ -143,6 +116,15 @@ locateModule moduleMaps@(moduleMap, moduleMapSource) env comp_info exts modName
143116
-- - TODO: should we look for file existence now? If the file was
144117
-- removed from the disk, how will it behaves? How do we invalidate
145118
-- that?
119+
--
120+
-- [About The reexported module]
121+
--
122+
-- A package (or unit) A can reexport a module from another package/unit.
123+
--
124+
-- When it happen, it means two things:
125+
--
126+
-- - This module must appear in 'moduleMaps', using the correct package/unit
127+
-- - What about "conflict". Right now the moduleMaps maps a module name to a unique package/unit.
146128
let mbFile = case Map.lookup (unLoc modName) (if isSource then moduleMapSource else moduleMap) of
147129
Nothing -> LocateNotFound
148130
Just (uid, file) -> LocateFoundFile uid file

0 commit comments

Comments
 (0)