Skip to content

Commit 13f3c91

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 8dd8ffc commit 13f3c91

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
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
@@ -1111,12 +1112,13 @@ addUnit unit_str = liftEwM $ do
11111112

11121113
-- | Throws if package flags are unsatisfiable
11131114
setOptions :: GhcMonad m
1114-
=> NormalizedFilePath
1115+
=> OptHaddockParse
1116+
-> NormalizedFilePath
11151117
-> ComponentOptions
11161118
-> DynFlags
11171119
-> FilePath -- ^ root dir, see Note [Root Directory]
11181120
-> m (NonEmpty (DynFlags, [GHC.Target]))
1119-
setOptions cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
1121+
setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11201122
((theOpts',_errs,_warns),units) <- processCmdLineP unit_flags [] (map noLoc theOpts)
11211123
case NE.nonEmpty units of
11221124
Just us -> initMulti us
@@ -1177,6 +1179,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11771179
dontWriteHieFiles $
11781180
setIgnoreInterfacePragmas $
11791181
setBytecodeLinkerOptions $
1182+
enableOptHaddock haddockOpt $
11801183
disableOptimisation $
11811184
Compat.setUpTypedHoles $
11821185
makeDynFlagsAbsolute compRoot -- makeDynFlagsAbsolute already accounts for workingDirectory
@@ -1190,6 +1193,14 @@ setIgnoreInterfacePragmas df =
11901193
disableOptimisation :: DynFlags -> DynFlags
11911194
disableOptimisation df = updOptLevel 0 df
11921195

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

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ import System.Info.Extra (isWindows)
174174
import qualified Data.IntMap as IM
175175
import GHC.Fingerprint
176176

177-
import GHC.Driver.Env (hsc_all_home_unit_ids)
178-
179177
data Log
180178
= LogShake Shake.Log
181179
| LogReindexingHieFile !NormalizedFilePath
@@ -262,12 +260,10 @@ getParsedModuleRule recorder =
262260
let ms = ms' { ms_hspp_opts = modify_dflags $ ms_hspp_opts ms' }
263261
reset_ms pm = pm { pm_mod_summary = ms' }
264262

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

269-
withOptHaddock :: ModSummary -> ModSummary
270-
withOptHaddock = withOption Opt_Haddock
265+
withoutOptHaddock :: ModSummary -> ModSummary
266+
withoutOptHaddock = withoutOption Opt_Haddock
271267

272268
withOption :: GeneralFlag -> ModSummary -> ModSummary
273269
withOption opt ms = ms{ms_hspp_opts= gopt_set (ms_hspp_opts ms) opt}
@@ -286,7 +282,7 @@ getParsedModuleWithCommentsRule recorder =
286282
ModSummaryResult{msrModSummary = ms, msrHscEnv = hsc} <- use_ GetModSummary file
287283
opt <- getIdeOptions
288284

289-
let ms' = withoutOption Opt_Haddock $ withOption Opt_KeepRawTokenStream ms
285+
let ms' = withoutOptHaddock $ withOption Opt_KeepRawTokenStream ms
290286
modify_dflags <- getModifyDynFlags dynFlagsModifyParser
291287
let ms'' = ms' { ms_hspp_opts = modify_dflags $ ms_hspp_opts ms' }
292288
reset_ms pm = pm { pm_mod_summary = ms' }
@@ -971,7 +967,7 @@ regenerateHiFile sess f ms compNeeded = do
971967
opt <- getIdeOptions
972968

973969
-- Embed haddocks in the interface file
974-
(diags, mb_pm) <- liftIO $ getParsedModuleDefinition hsc opt f (withOptHaddock ms)
970+
(diags, mb_pm) <- liftIO $ getParsedModuleDefinition hsc opt f ms
975971
case mb_pm of
976972
Nothing -> return (diags, Nothing)
977973
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)