Skip to content

Commit 2e90eed

Browse files
committed
Avoid unnecessary recompilation due to -haddock
Due to unprincipled adding and removing the `-haddock` flag during compilation and recompilation checking, we were performing more work than necessary. We avoid this by compiling everything with `-haddock` by default. This is safe nowadays, we have essentially been doing this for many releases, and know this is fine. For the occasion where we actually want to parse without the `-haddock` flag, we keep explicitly disabling it. We enable `-haddock` flag during session loading, since we already perform a number of DynFlags tweaks. This behaviour is dependent on the `OptHaddockParse` opton, which can, currently, only be modified at compile-time.
1 parent c3b61fe commit 2e90eed

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
452452
IdeOptions{ optTesting = IdeTesting optTesting
453453
, optCheckProject = getCheckProject
454454
, optExtensions
455+
, optHaddockParse
455456
} <- getIdeOptions
456457

457458
-- populate the knownTargetsVar with all the
@@ -496,7 +497,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
496497
packageSetup (hieYaml, cfp, opts, libDir) = do
497498
-- Parse DynFlags for the newly discovered component
498499
hscEnv <- emptyHscEnv ideNc libDir
499-
newTargetDfs <- evalGhcEnv hscEnv $ setOptions cfp opts (hsc_dflags hscEnv) rootDir
500+
newTargetDfs <- evalGhcEnv hscEnv $ setOptions optHaddockParse cfp opts (hsc_dflags hscEnv) rootDir
500501
let deps = componentDependencies opts ++ maybeToList hieYaml
501502
dep_info <- getDependencyInfo deps
502503
-- Now lookup to see whether we are combining with an existing HscEnv
@@ -1110,12 +1111,13 @@ addUnit unit_str = liftEwM $ do
11101111

11111112
-- | Throws if package flags are unsatisfiable
11121113
setOptions :: GhcMonad m
1113-
=> NormalizedFilePath
1114+
=> OptHaddockParse
1115+
-> NormalizedFilePath
11141116
-> ComponentOptions
11151117
-> DynFlags
11161118
-> FilePath -- ^ root dir, see Note [Root Directory]
11171119
-> m (NonEmpty (DynFlags, [GHC.Target]))
1118-
setOptions cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
1120+
setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11191121
((theOpts',_errs,_warns),units) <- processCmdLineP unit_flags [] (map noLoc theOpts)
11201122
case NE.nonEmpty units of
11211123
Just us -> initMulti us
@@ -1179,6 +1181,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11791181
dontWriteHieFiles $
11801182
setIgnoreInterfacePragmas $
11811183
setBytecodeLinkerOptions $
1184+
enableOptHaddock haddockOpt $
11821185
disableOptimisation $
11831186
Compat.setUpTypedHoles $
11841187
makeDynFlagsAbsolute compRoot -- makeDynFlagsAbsolute already accounts for workingDirectory
@@ -1192,6 +1195,14 @@ setIgnoreInterfacePragmas df =
11921195
disableOptimisation :: DynFlags -> DynFlags
11931196
disableOptimisation df = updOptLevel 0 df
11941197

1198+
-- | We always compile with '-haddock' unless explicitly disabled.
1199+
--
1200+
-- This avoids inconsistencies when doing recompilation checking which was
1201+
-- observed in https://github.com/haskell/haskell-language-server/issues/4511
1202+
enableOptHaddock :: OptHaddockParse -> DynFlags -> DynFlags
1203+
enableOptHaddock HaddockParse d = gopt_set d Opt_Haddock
1204+
enableOptHaddock NoHaddockParse d = d
1205+
11951206
setHiDir :: FilePath -> DynFlags -> DynFlags
11961207
setHiDir f d =
11971208
-- override user settings to avoid conflicts leading to recompilation

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,10 @@ getParsedModuleRule recorder =
260260
let ms = ms' { ms_hspp_opts = modify_dflags $ ms_hspp_opts ms' }
261261
reset_ms pm = pm { pm_mod_summary = ms' }
262262

263-
-- We still parse with Haddocks whether Opt_Haddock is True or False to collect information
264-
-- but we no longer need to parse with and without Haddocks separately for above GHC90.
265-
liftIO $ (fmap.fmap.fmap) reset_ms $ getParsedModuleDefinition hsc opt file (withOptHaddock ms)
263+
liftIO $ (fmap.fmap.fmap) reset_ms $ getParsedModuleDefinition hsc opt file ms
266264

267-
withOptHaddock :: ModSummary -> ModSummary
268-
withOptHaddock = withOption Opt_Haddock
265+
withoutOptHaddock :: ModSummary -> ModSummary
266+
withoutOptHaddock = withoutOption Opt_Haddock
269267

270268
withOption :: GeneralFlag -> ModSummary -> ModSummary
271269
withOption opt ms = ms{ms_hspp_opts= gopt_set (ms_hspp_opts ms) opt}
@@ -284,7 +282,7 @@ getParsedModuleWithCommentsRule recorder =
284282
ModSummaryResult{msrModSummary = ms, msrHscEnv = hsc} <- use_ GetModSummary file
285283
opt <- getIdeOptions
286284

287-
let ms' = withoutOption Opt_Haddock $ withOption Opt_KeepRawTokenStream ms
285+
let ms' = withoutOptHaddock $ withOption Opt_KeepRawTokenStream ms
288286
modify_dflags <- getModifyDynFlags dynFlagsModifyParser
289287
let ms'' = ms' { ms_hspp_opts = modify_dflags $ ms_hspp_opts ms' }
290288
reset_ms pm = pm { pm_mod_summary = ms' }
@@ -973,7 +971,7 @@ regenerateHiFile sess f ms compNeeded = do
973971
opt <- getIdeOptions
974972

975973
-- Embed haddocks in the interface file
976-
(diags, mb_pm) <- liftIO $ getParsedModuleDefinition hsc opt f (withOptHaddock ms)
974+
(diags, mb_pm) <- liftIO $ getParsedModuleDefinition hsc opt f ms
977975
case mb_pm of
978976
Nothing -> return (diags, Nothing)
979977
Just pm -> do

ghcide/src/Development/IDE/Types/Options.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ data IdeOptions = IdeOptions
6969
-- ^ When to typecheck reverse dependencies of a file
7070
, optHaddockParse :: OptHaddockParse
7171
-- ^ Whether to return result of parsing module with Opt_Haddock.
72-
-- Otherwise, return the result of parsing without Opt_Haddock, so
73-
-- that the parsed module contains the result of Opt_KeepRawTokenStream,
74-
-- which might be necessary for hlint.
72+
-- Otherwise, return the result of parsing without Opt_Haddock.
7573
, optModifyDynFlags :: Config -> DynFlagsModifications
7674
-- ^ Will be called right after setting up a new cradle,
7775
-- allowing to customize the Ghc options used

0 commit comments

Comments
 (0)