Skip to content

Commit 366e23e

Browse files
committed
Allow pathToId to fail
1 parent 12748f1 commit 366e23e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Diff for: ghcide/src/Development/IDE/Core/Rules.hs

+14-11
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,20 @@ reportImportCyclesRule :: Recorder (WithPriority Log) -> Rules ()
526526
reportImportCyclesRule recorder =
527527
defineEarlyCutoff (cmapWithPrio LogShake recorder) $ Rule $ \ReportImportCycles file -> fmap (\errs -> if null errs then (Just "1",([], Just ())) else (Nothing, (errs, Nothing))) $ do
528528
DependencyInformation{..} <- useNoFile_ GetModuleGraph
529-
let fileId = pathToId depPathIdMap file
530-
case IntMap.lookup (getFilePathId fileId) depErrorNodes of
531-
Nothing -> pure []
532-
Just errs -> do
533-
let cycles = mapMaybe (cycleErrorInFile fileId) (toList errs)
534-
-- Convert cycles of files into cycles of module names
535-
forM cycles $ \(imp, files) -> do
536-
modNames <- forM files $ \fileId -> do
537-
let file = idToPath depPathIdMap fileId
538-
getModuleName file
539-
pure $ toDiag imp $ sort modNames
529+
case pathToId depPathIdMap file of
530+
-- The header of the file does not parse, so it can't be part of any import cycles.
531+
Nothing -> pure []
532+
Just fileId ->
533+
case IntMap.lookup (getFilePathId fileId) depErrorNodes of
534+
Nothing -> pure []
535+
Just errs -> do
536+
let cycles = mapMaybe (cycleErrorInFile fileId) (toList errs)
537+
-- Convert cycles of files into cycles of module names
538+
forM cycles $ \(imp, files) -> do
539+
modNames <- forM files $ \fileId -> do
540+
let file = idToPath depPathIdMap fileId
541+
getModuleName file
542+
pure $ toDiag imp $ sort modNames
540543
where cycleErrorInFile f (PartOfCycle imp fs)
541544
| f `elem` fs = Just (imp, fs)
542545
cycleErrorInFile _ _ = Nothing

Diff for: ghcide/src/Development/IDE/Import/DependencyInformation.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ getPathId path m@PathIdMap{..} =
105105
insertImport :: FilePathId -> Either ModuleParseError ModuleImports -> RawDependencyInformation -> RawDependencyInformation
106106
insertImport (FilePathId k) v rawDepInfo = rawDepInfo { rawImports = IntMap.insert k v (rawImports rawDepInfo) }
107107

108-
pathToId :: PathIdMap -> NormalizedFilePath -> FilePathId
109-
pathToId PathIdMap{pathToIdMap} path = pathToIdMap HMS.! path
108+
pathToId :: PathIdMap -> NormalizedFilePath -> Maybe FilePathId
109+
pathToId PathIdMap{pathToIdMap} path = pathToIdMap HMS.!? path
110110

111111
lookupPathToId :: PathIdMap -> NormalizedFilePath -> Maybe FilePathId
112112
lookupPathToId PathIdMap{pathToIdMap} path = HMS.lookup path pathToIdMap
@@ -343,7 +343,7 @@ immediateReverseDependencies file DependencyInformation{..} = do
343343
-- | returns all transitive dependencies in topological order.
344344
transitiveDeps :: DependencyInformation -> NormalizedFilePath -> Maybe TransitiveDependencies
345345
transitiveDeps DependencyInformation{..} file = do
346-
let !fileId = pathToId depPathIdMap file
346+
!fileId <- pathToId depPathIdMap file
347347
reachableVs <-
348348
-- Delete the starting node
349349
IntSet.delete (getFilePathId fileId) .

0 commit comments

Comments
 (0)