on master help sign in
Move packaging logic out of haskell-ide-core (#1239)

* The PackageDynFlags instances are no longer required because its not a rule result * Move all the package custom pieces out of the ide-core * Move the optMbPackageName out to daml-ghc * Make sure we clean up all temporary files * Clean up the import list * Move runGhcFast to its one use site

commit60f0177f9b9461b7747b0215900c675812b621ec authorNeil Mitchell <35463327+neil-da@users.noreply.github.com> dateMon 20 May 2019, 12:05 (7 years ago) parent2e704bd
5 files changed+17−103
src/Development/IDE/Functions/Compile.hs−7 src/Development/IDE/Orphans.hs−8 src/Development/IDE/State/Rules.hs+1−6 src/Development/IDE/Types/Options.hs−6 src/Development/IDE/UtilGHC.hs+16−76
src/Development/IDE/Functions/Compile.hs−7
@@ -18,7 +18,6 @@ module Development.IDE.Functions.Compile
1818 , typecheckModule
1919 , loadPackage
2020 , computePackageDeps
21− , generatePackageState
2221 ) where
2322
2423import Development.IDE.Functions.Warnings
@@ -442,12 +441,6 @@ parsePragmasIntoDynFlags fp contents = catchSrcErrors $ do
442441 (dflags, _, _) <- parseDynamicFilePragma dflags0 opts
443442 return dflags
444443
445−generatePackageState :: [FilePath] -> Bool -> [(String, ModRenaming)] -> IO PackageDynFlags
446−generatePackageState paths hideAllPkgs pkgImports = do
447− let dflags = setPackageImports hideAllPkgs pkgImports $ setPackageDbs paths fakeDynFlags
448− (newDynFlags, _) <- initPackages dflags
449− pure $ getPackageDynFlags newDynFlags
450−
451444-- | Run something in a Ghc monad and catch the errors (SourceErrors and
452445-- compiler-internal exceptions like Panic or InstallationError).
453446catchSrcErrors :: GhcMonad m => m a -> Ex.ExceptT [FileDiagnostic] m a
src/Development/IDE/Orphans.hs−8
@@ -38,11 +38,3 @@ instance Show (GenLocated SrcSpan ModuleName) where show = prettyPrint
3838instance Show PackageName where show = prettyPrint
3939instance Show PackageState where show _ = "PackageState"
4040instance Show Name where show = prettyPrint
41−
42−
43−-- Things which are defined in this module, but still orphan since I need
44−-- the definitions in this module
45−
46−deriving instance Show PackageDynFlags
47−instance NFData PackageDynFlags where
48− rnf (PackageDynFlags db state insts) = db `seq` state `seq` rnf insts
src/Development/IDE/State/Rules.hs+1−6
@@ -33,7 +33,6 @@ import Development.IDE.State.FileStore
3333import Development.IDE.Types.Diagnostics as Base
3434import Data.Bifunctor
3535import Data.Either.Extra
36−import Development.IDE.UtilGHC
3736import Data.Maybe
3837import Data.Foldable
3938import qualified Data.Map.Strict as Map
@@ -45,7 +44,6 @@ import Development.IDE.Types.LSP as Compiler
4544import Development.IDE.State.RuleTypes
4645
4746import GHC
48−import HscTypes
4947import Development.IDE.Compat
5048import UniqSupply
5149import Module as M
@@ -300,10 +298,7 @@ loadGhcSession :: Rules ()
300298loadGhcSession =
301299 defineNoFile $ \GhcSession -> do
302300 opts <- envOptions <$> getServiceEnv
303− env <- Compile.optGhcSession opts
304− pkg <- liftIO $ Compile.generatePackageState
305− (Compile.optPackageDbs opts) (Compile.optHideAllPkgs opts) (Compile.optPackageImports opts)
306− return env{hsc_dflags = setPackageDynFlags pkg $ hsc_dflags env}
301+ Compile.optGhcSession opts
307302
308303
309304getHieFileRule :: Rules ()
src/Development/IDE/Types/Options.hs−6
@@ -23,12 +23,6 @@ data IdeOptions = IdeOptions
2323 , optWriteIface :: Bool
2424 , optExtensions :: [String]
2525
26− , optMbPackageName :: Maybe String
27−
28− , optPackageDbs :: [FilePath]
29− , optHideAllPkgs :: Bool
30− , optPackageImports :: [(String, ModRenaming)]
31−
3226 , optThreads :: Int
3327 , optShakeProfiling :: Maybe FilePath
3428 }
src/Development/IDE/UtilGHC.hs+16−76
@@ -10,57 +10,26 @@
1010--
1111-- * Call setSessionDynFlags, use modifyDynFlags instead. It's faster and avoids loading packages.
1212module Development.IDE.UtilGHC(
13− PackageDynFlags(..), setPackageDynFlags, getPackageDynFlags,
1413 lookupPackageConfig,
1514 modifyDynFlags,
16− setPackageImports,
17− setPackageDbs,
1815 fakeDynFlags,
1916 prettyPrint,
20− runGhcFast,
2117 runGhcEnv
2218 ) where
2319
24−import Config
25−import Fingerprint
26−import GHC hiding (convertLit)
27−import GhcMonad
28−import GhcPlugins as GHC hiding (fst3, (<>))
29−import HscMain
30−import qualified Packages
31−import Platform
32−import qualified EnumSet
33−import Data.IORef
34−import System.FilePath
35−import GHC.Generics (Generic)
20+import Config
21+import Fingerprint
22+import GHC
23+import GhcMonad
24+import GhcPlugins
25+import Platform
26+import Data.IORef
27+import Control.Exception
28+import FileCleanup
3629
3730----------------------------------------------------------------------
3831-- GHC setup
3932
40−setPackageDbs :: [FilePath] -> DynFlags -> DynFlags
41−setPackageDbs paths dflags =
42− dflags
43− { packageDBFlags =
44− [PackageDB $ PkgConfFile $ path </> "package.conf.d" | path <- paths] ++ [NoGlobalPackageDB, ClearPackageDBs]
45− , pkgDatabase = if null paths then Just [] else Nothing
46− -- if we don't load any packages set the package database to empty and loaded.
47− , settings = (settings dflags)
48− {sTopDir = case paths of p:_ -> p; _ -> error "No package db path available but used $topdir"
49− , sSystemPackageConfig = case paths of p:_ -> p; _ -> error "No package db path available but used system package config"
50− }
51− }
52−
53−setPackageImports :: Bool -> [(String, ModRenaming)] -> DynFlags -> DynFlags
54−setPackageImports hideAllPkgs pkgImports dflags = dflags {
55− packageFlags = packageFlags dflags ++
56− [ExposePackage pkgName (UnitIdArg $ stringToUnitId pkgName) renaming
57− | (pkgName, renaming) <- pkgImports
58− ]
59− , generalFlags = if hideAllPkgs
60− then Opt_HideAllPackages `EnumSet.insert` generalFlags dflags
61− else generalFlags dflags
62− }
63−
6433modifyDynFlags :: GhcMonad m => (DynFlags -> DynFlags) -> m ()
6534modifyDynFlags f = do
6635 newFlags <- f <$> getSessionDynFlags
@@ -69,27 +38,6 @@ modifyDynFlags f = do
6938 modifySession $ \h ->
7039 h { hsc_dflags = newFlags, hsc_IC = (hsc_IC h) {ic_dflags = newFlags} }
7140
72−-- | The subset of @DynFlags@ computed by package initialization.
73−data PackageDynFlags = PackageDynFlags
74− { pdfPkgDatabase :: !(Maybe [(FilePath, [Packages.PackageConfig])])
75− , pdfPkgState :: !Packages.PackageState
76− , pdfThisUnitIdInsts :: !(Maybe [(ModuleName, Module)])
77− } deriving (Generic)
78−
79−setPackageDynFlags :: PackageDynFlags -> DynFlags -> DynFlags
80−setPackageDynFlags PackageDynFlags{..} dflags = dflags
81− { pkgDatabase = pdfPkgDatabase
82− , pkgState = pdfPkgState
83− , thisUnitIdInsts_ = pdfThisUnitIdInsts
84− }
85−
86−getPackageDynFlags :: DynFlags -> PackageDynFlags
87−getPackageDynFlags DynFlags{..} = PackageDynFlags
88− { pdfPkgDatabase = pkgDatabase
89− , pdfPkgState = pkgState
90− , pdfThisUnitIdInsts = thisUnitIdInsts_
91− }
92−
9341lookupPackageConfig :: UnitId -> HscEnv -> Maybe PackageConfig
9442lookupPackageConfig unitId env =
9543 lookupPackage' False pkgConfigMap unitId
@@ -106,22 +54,14 @@ prettyPrint = showSDoc fakeDynFlags . ppr
10654
10755runGhcEnv :: HscEnv -> Ghc a -> IO a
10856runGhcEnv env act = do
109− ref <- newIORef env
110− unGhc act $ Session ref
111−
57+ filesToClean <- newIORef emptyFilesToClean
58+ dirsToClean <- newIORef mempty
59+ let dflags = (hsc_dflags env){filesToClean=filesToClean, dirsToClean=dirsToClean}
60+ ref <- newIORef env{hsc_dflags=dflags}
61+ unGhc act (Session ref) `finally` do
62+ cleanTempFiles dflags
63+ cleanTempDirs dflags
11264
113−-- | Like 'runGhc' but much faster (400x), with less IO and no file dependency
114−runGhcFast :: Ghc a -> IO a
115−-- copied from GHC with the nasty bits dropped
116−runGhcFast act = do
117− ref <- newIORef (error "empty session")
118− let session = Session ref
119− flip unGhc session $ do
120− dflags <- liftIO $ initDynFlags fakeDynFlags
121− liftIO $ setUnsafeGlobalDynFlags dflags
122− env <- liftIO $ newHscEnv dflags
123− setSession env
124− withCleanupSession act
12565
12666-- Fake DynFlags which are mostly undefined, but define enough to do a little bit
12767fakeDynFlags :: DynFlags