|
| 1 | +module Development.IDE.Core.Dependencies |
| 2 | + ( indexDependencyHieFiles |
| 3 | + ) where |
| 4 | + |
| 5 | +import Control.Concurrent.STM (atomically) |
| 6 | +import Control.Concurrent.STM.TQueue (writeTQueue) |
| 7 | +import Control.Monad (unless, void) |
| 8 | +import Data.Foldable (traverse_) |
| 9 | +import qualified Data.Map as Map |
| 10 | +import Data.Maybe (isNothing) |
| 11 | +import Data.Set (Set) |
| 12 | +import qualified Data.Set as Set |
| 13 | +import Development.IDE.Core.Compile (indexHieFile) |
| 14 | +import Development.IDE.Core.Rules (HieFileCheck (..), Log, |
| 15 | + checkHieFile) |
| 16 | +import Development.IDE.Core.Shake (HieDbWriter (indexQueue), |
| 17 | + ShakeExtras (hiedbWriter, lspEnv, withHieDb)) |
| 18 | +import qualified Development.IDE.GHC.Compat as GHC |
| 19 | +import Development.IDE.Types.Location (NormalizedFilePath, |
| 20 | + toNormalizedFilePath') |
| 21 | +import HieDb (SourceFile (FakeFile), |
| 22 | + lookupPackage, |
| 23 | + removeDependencySrcFiles) |
| 24 | +import Ide.Logger (Recorder, WithPriority) |
| 25 | +import Language.LSP.Server (resRootPath) |
| 26 | +import System.Directory (doesDirectoryExist) |
| 27 | +import System.FilePath ((<.>), (</>)) |
| 28 | + |
| 29 | +newtype Package = Package GHC.UnitInfo deriving Eq |
| 30 | +instance Ord Package where |
| 31 | + compare (Package u1) (Package u2) = compare (GHC.unitId u1) (GHC.unitId u2) |
| 32 | + |
| 33 | +-- indexDependencyHieFiles gets all of the direct and transitive dependencies |
| 34 | +-- from the HscEnv and indexes their HIE files in the HieDb. |
| 35 | +indexDependencyHieFiles :: Recorder (WithPriority Log) -> ShakeExtras -> GHC.HscEnv -> IO () |
| 36 | +indexDependencyHieFiles recorder se hscEnv = do |
| 37 | + -- Check whether the .hls directory exists. |
| 38 | + dotHlsDirExists <- maybe (pure False) doesDirectoryExist mHlsDir |
| 39 | + -- If the .hls directory does not exits, it may have been deleted. |
| 40 | + -- In this case, delete the indexed source files for all |
| 41 | + -- dependencies that are already indexed. |
| 42 | + unless dotHlsDirExists deleteMissingDependencySources |
| 43 | + -- Index all dependency HIE files in the HieDb database. |
| 44 | + void $ Map.traverseWithKey indexPackageHieFiles packagesWithModules |
| 45 | + where |
| 46 | + mHlsDir :: Maybe FilePath |
| 47 | + mHlsDir = do |
| 48 | + projectDir <- resRootPath =<< lspEnv se |
| 49 | + pure $ projectDir </> ".hls" |
| 50 | + -- Add the deletion of dependency source files from the |
| 51 | + -- HieDb database to the database write queue. |
| 52 | + deleteMissingDependencySources :: IO () |
| 53 | + deleteMissingDependencySources = |
| 54 | + atomically $ writeTQueue (indexQueue $ hiedbWriter se) $ |
| 55 | + \withHieDb -> |
| 56 | + withHieDb $ \db -> |
| 57 | + removeDependencySrcFiles db |
| 58 | + -- Index all of the modules in a package (a Unit). |
| 59 | + indexPackageHieFiles :: Package -> [GHC.Module] -> IO () |
| 60 | + indexPackageHieFiles (Package package) modules = do |
| 61 | + let pkgLibDir :: FilePath |
| 62 | + pkgLibDir = case GHC.unitLibraryDirs package of |
| 63 | + [] -> "" |
| 64 | + (libraryDir : _) -> libraryDir |
| 65 | + -- Cabal puts the HIE files for a package in the |
| 66 | + -- extra-compilation-artifacts directory, provided |
| 67 | + -- it is compiled with the -fwrite-ide-info ghc option. |
| 68 | + hieDir :: FilePath |
| 69 | + hieDir = pkgLibDir </> "extra-compilation-artifacts" |
| 70 | + unit :: GHC.Unit |
| 71 | + unit = GHC.RealUnit $ GHC.Definite $ GHC.unitId package |
| 72 | + -- Check if we have already indexed this package. |
| 73 | + moduleRows <- withHieDb se $ \db -> |
| 74 | + lookupPackage db unit |
| 75 | + case moduleRows of |
| 76 | + -- There are no modules from this package in the database, |
| 77 | + -- so go ahead and index all the modules. |
| 78 | + [] -> traverse_ (indexModuleHieFile hieDir) modules |
| 79 | + -- There are modules from this package in the database, |
| 80 | + -- so assume all the modules have already been indexed |
| 81 | + -- and do nothing. |
| 82 | + _ -> return () |
| 83 | + indexModuleHieFile :: FilePath -> GHC.Module -> IO () |
| 84 | + indexModuleHieFile hieDir m = do |
| 85 | + let hiePath :: NormalizedFilePath |
| 86 | + hiePath = toNormalizedFilePath' $ |
| 87 | + hieDir </> GHC.moduleNameSlashes (GHC.moduleName m) <.> "hie" |
| 88 | + -- Check that the module HIE file has correctly loaded. If there |
| 89 | + -- was some problem loading it, or if it has already been indexed |
| 90 | + -- (which shouldn't happen because we check whether each package |
| 91 | + -- has been indexed), then do nothing. Otherwise, call the |
| 92 | + -- indexHieFile function from Core.Compile. |
| 93 | + hieCheck <- checkHieFile recorder se "newHscEnvEqWithImportPaths" hiePath |
| 94 | + case hieCheck of |
| 95 | + HieFileMissing -> return () |
| 96 | + HieAlreadyIndexed -> return () |
| 97 | + CouldNotLoadHie _e -> return () |
| 98 | + DoIndexing hash hie -> |
| 99 | + -- At this point there is no source file for the HIE file, |
| 100 | + -- so the HieDb.SourceFile we give is FakeFile Nothing. |
| 101 | + indexHieFile se hiePath (FakeFile Nothing) hash hie |
| 102 | + packagesWithModules :: Map.Map Package [GHC.Module] |
| 103 | + packagesWithModules = Map.fromSet getModulesForPackage packages |
| 104 | + packages :: Set Package |
| 105 | + packages = Set.fromList |
| 106 | + $ map Package |
| 107 | + $ Map.elems |
| 108 | + -- Take only the packages in the unitInfoMap that are direct |
| 109 | + -- or transitive dependencies. |
| 110 | + $ Map.filterWithKey (\uid _ -> uid `Set.member` dependencyIds) unitInfoMap |
| 111 | + where |
| 112 | + unitInfoMap :: GHC.UnitInfoMap |
| 113 | + unitInfoMap = GHC.getUnitInfoMap hscEnv |
| 114 | + dependencyIds :: Set GHC.UnitId |
| 115 | + dependencyIds = |
| 116 | + calculateTransitiveDependencies unitInfoMap directDependencyIds directDependencyIds |
| 117 | + directDependencyIds :: Set GHC.UnitId |
| 118 | + directDependencyIds = Set.fromList |
| 119 | + $ map GHC.toUnitId |
| 120 | + $ GHC.explicitUnits |
| 121 | + $ GHC.unitState hscEnv |
| 122 | + |
| 123 | +-- calculateTransitiveDependencies finds the UnitId keys in the UnitInfoMap |
| 124 | +-- that are dependencies or transitive dependencies. |
| 125 | +calculateTransitiveDependencies :: GHC.UnitInfoMap -> Set GHC.UnitId -> Set GHC.UnitId -> Set GHC.UnitId |
| 126 | +calculateTransitiveDependencies unitInfoMap allDependencies newDepencencies |
| 127 | + -- If there are no new dependencies, we have found them all, |
| 128 | + -- so return allDependencies |
| 129 | + | Set.null newDepencencies = allDependencies |
| 130 | + -- Otherwise recursively add any dependencies of the newDepencencies |
| 131 | + -- that are not in allDependencies already. |
| 132 | + | otherwise = calculateTransitiveDependencies unitInfoMap nextAll nextNew |
| 133 | + where |
| 134 | + nextAll :: Set GHC.UnitId |
| 135 | + nextAll = Set.union allDependencies nextNew |
| 136 | + -- Get the dependencies of the newDependencies. Then the nextNew depencencies |
| 137 | + -- will be the set difference of the dependencies we have so far (allDependencies), |
| 138 | + -- and the dependencies of the newDepencencies. |
| 139 | + nextNew :: Set GHC.UnitId |
| 140 | + nextNew = flip Set.difference allDependencies |
| 141 | + $ Set.unions |
| 142 | + $ map (Set.fromList . GHC.unitDepends) |
| 143 | + $ Map.elems |
| 144 | + $ Map.filterWithKey (\uid _ -> uid `Set.member` newDepencencies) unitInfoMap |
| 145 | + |
| 146 | +getModulesForPackage :: Package -> [GHC.Module] |
| 147 | +getModulesForPackage (Package package) = |
| 148 | + map makeModule allModules |
| 149 | + where |
| 150 | + allModules :: [GHC.ModuleName] |
| 151 | + allModules = map fst |
| 152 | + -- The modules with a Just value in the tuple |
| 153 | + -- are from other packages. These won't have |
| 154 | + -- an HIE file in this package, and should be |
| 155 | + -- covered by the transitive dependencies. |
| 156 | + ( filter (isNothing . snd) |
| 157 | + $ GHC.unitExposedModules package |
| 158 | + ) |
| 159 | + ++ GHC.unitHiddenModules package |
| 160 | + makeModule :: GHC.ModuleName |
| 161 | + -> GHC.Module |
| 162 | + makeModule = GHC.mkModule (GHC.unitInfoId package) |
0 commit comments