Skip to content

Commit 1d2136c

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 0a9b1cb commit 1d2136c

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
@@ -451,6 +451,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
451451
IdeOptions{ optTesting = IdeTesting optTesting
452452
, optCheckProject = getCheckProject
453453
, optExtensions
454+
, optHaddockParse
454455
} <- getIdeOptions
455456

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

11081109
-- | Throws if package flags are unsatisfiable
11091110
setOptions :: GhcMonad m
1110-
=> NormalizedFilePath
1111+
=> OptHaddockParse
1112+
-> NormalizedFilePath
11111113
-> ComponentOptions
11121114
-> DynFlags
11131115
-> FilePath -- ^ root dir, see Note [Root Directory]
11141116
-> m (NonEmpty (DynFlags, [GHC.Target]))
1115-
setOptions cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
1117+
setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11161118
((theOpts',_errs,_warns),units) <- processCmdLineP unit_flags [] (map noLoc theOpts)
11171119
case NE.nonEmpty units of
11181120
Just us -> initMulti us
@@ -1173,6 +1175,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11731175
dontWriteHieFiles $
11741176
setIgnoreInterfacePragmas $
11751177
setBytecodeLinkerOptions $
1178+
enableOptHaddock haddockOpt $
11761179
disableOptimisation $
11771180
Compat.setUpTypedHoles $
11781181
makeDynFlagsAbsolute compRoot -- makeDynFlagsAbsolute already accounts for workingDirectory
@@ -1186,6 +1189,14 @@ setIgnoreInterfacePragmas df =
11861189
disableOptimisation :: DynFlags -> DynFlags
11871190
disableOptimisation df = updOptLevel 0 df
11881191

1192+
-- | We always compile with '-haddock' unless explicitly disabled.
1193+
--
1194+
-- This avoids inconsistencies when doing recompilation checking which was
1195+
-- observed in https://github.com/haskell/haskell-language-server/issues/4511
1196+
enableOptHaddock :: OptHaddockParse -> DynFlags -> DynFlags
1197+
enableOptHaddock HaddockParse d = gopt_set d Opt_Haddock
1198+
enableOptHaddock NoHaddockParse d = d
1199+
11891200
setHiDir :: FilePath -> DynFlags -> DynFlags
11901201
setHiDir f d =
11911202
-- 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)