@@ -38,9 +38,8 @@ import Data.Char (isLower)
38
38
import Data.Default
39
39
import Data.Either.Extra
40
40
import Data.Function
41
- import Data.Hashable
41
+ import Data.Hashable hiding ( hash )
42
42
import qualified Data.HashMap.Strict as HM
43
- import Data.IORef
44
43
import Data.List
45
44
import Data.List.Extra (dropPrefix , split )
46
45
import qualified Data.Map.Strict as Map
@@ -51,11 +50,11 @@ import Data.Time.Clock
51
50
import Data.Version
52
51
import Development.IDE.Core.RuleTypes
53
52
import Development.IDE.Core.Shake hiding (Log , Priority ,
54
- withHieDb )
53
+ knownTargets , withHieDb )
55
54
import qualified Development.IDE.GHC.Compat as Compat
56
55
import Development.IDE.GHC.Compat.Core hiding (Target ,
57
56
TargetFile , TargetModule ,
58
- Var , Warning )
57
+ Var , Warning , getOptions )
59
58
import qualified Development.IDE.GHC.Compat.Core as GHC
60
59
import Development.IDE.GHC.Compat.Env hiding (Logger )
61
60
import Development.IDE.GHC.Compat.Units (UnitId )
@@ -111,6 +110,12 @@ import HieDb.Utils
111
110
import qualified System.Random as Random
112
111
import System.Random (RandomGen )
113
112
113
+ -- See Note [Guidelines For Using CPP In GHCIDE Import Statements]
114
+
115
+ #if !MIN_VERSION_ghc(9,4,0)
116
+ import Data.IORef
117
+ #endif
118
+
114
119
data Log
115
120
= LogSettingInitialDynFlags
116
121
| LogGetInitialGhcLibDirDefaultCradleFail ! CradleError ! FilePath ! (Maybe FilePath ) ! (Cradle Void )
@@ -148,21 +153,21 @@ instance Pretty Log where
148
153
, " Cradle:" <+> viaShow cradle ]
149
154
LogGetInitialGhcLibDirDefaultCradleNone ->
150
155
" Couldn't load cradle. Cradle not found."
151
- LogHieDbRetry delay maxDelay maxRetryCount e ->
156
+ LogHieDbRetry delay maxDelay retriesRemaining e ->
152
157
nest 2 $
153
158
vcat
154
159
[ " Retrying hiedb action..."
155
160
, " delay:" <+> pretty delay
156
161
, " maximum delay:" <+> pretty maxDelay
157
- , " retries remaining:" <+> pretty maxRetryCount
162
+ , " retries remaining:" <+> pretty retriesRemaining
158
163
, " SQLite error:" <+> pretty (displayException e) ]
159
- LogHieDbRetriesExhausted baseDelay maxDelay maxRetryCount e ->
164
+ LogHieDbRetriesExhausted baseDelay maxDelay retriesRemaining e ->
160
165
nest 2 $
161
166
vcat
162
167
[ " Retries exhausted for hiedb action."
163
168
, " base delay:" <+> pretty baseDelay
164
169
, " maximum delay:" <+> pretty maxDelay
165
- , " retries remaining:" <+> pretty maxRetryCount
170
+ , " retries remaining:" <+> pretty retriesRemaining
166
171
, " Exception:" <+> pretty (displayException e) ]
167
172
LogHieDbWriterThreadSQLiteError e ->
168
173
nest 2 $
@@ -199,7 +204,7 @@ instance Pretty Log where
199
204
" Cradle:" <+> viaShow cradle
200
205
LogNewComponentCache componentCache ->
201
206
" New component cache HscEnvEq:" <+> viaShow componentCache
202
- LogHieBios log -> pretty log
207
+ LogHieBios msg -> pretty msg
203
208
204
209
-- | Bump this version number when making changes to the format of the data stored in hiedb
205
210
hiedbDataVersion :: String
@@ -263,17 +268,16 @@ loadWithImplicitCradle mHieYaml rootDir = do
263
268
264
269
getInitialGhcLibDirDefault :: Recorder (WithPriority Log ) -> FilePath -> IO (Maybe LibDir )
265
270
getInitialGhcLibDirDefault recorder rootDir = do
266
- let log = logWith recorder
267
271
hieYaml <- findCradle def rootDir
268
272
cradle <- loadCradle def hieYaml rootDir
269
273
libDirRes <- getRuntimeGhcLibDir (toCologActionWithPrio (cmapWithPrio LogHieBios recorder)) cradle
270
274
case libDirRes of
271
275
CradleSuccess libdir -> pure $ Just $ LibDir libdir
272
276
CradleFail err -> do
273
- log Error $ LogGetInitialGhcLibDirDefaultCradleFail err rootDir hieYaml cradle
277
+ logWith recorder Error $ LogGetInitialGhcLibDirDefaultCradleFail err rootDir hieYaml cradle
274
278
pure Nothing
275
279
CradleNone -> do
276
- log Warning LogGetInitialGhcLibDirDefaultCradleNone
280
+ logWith recorder Warning LogGetInitialGhcLibDirDefaultCradleNone
277
281
pure Nothing
278
282
279
283
-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir
@@ -301,28 +305,26 @@ retryOnException
301
305
-> g -- ^ random number generator
302
306
-> m a -- ^ action that may throw exception
303
307
-> m a
304
- retryOnException exceptionPred recorder maxDelay ! baseDelay ! maxRetryCount rng action = do
308
+ retryOnException exceptionPred recorder maxDelay ! baseDelay ! maxTimesRetry rng action = do
305
309
result <- tryJust exceptionPred action
306
310
case result of
307
311
Left e
308
- | maxRetryCount > 0 -> do
312
+ | maxTimesRetry > 0 -> do
309
313
-- multiply by 2 because baseDelay is midpoint of uniform range
310
314
let newBaseDelay = min maxDelay (baseDelay * 2 )
311
315
let (delay, newRng) = Random. randomR (0 , newBaseDelay) rng
312
- let newMaxRetryCount = maxRetryCount - 1
316
+ let newMaxTimesRetry = maxTimesRetry - 1
313
317
liftIO $ do
314
- log Warning $ LogHieDbRetry delay maxDelay newMaxRetryCount (toException e)
318
+ logWith recorder Warning $ LogHieDbRetry delay maxDelay newMaxTimesRetry (toException e)
315
319
threadDelay delay
316
- retryOnException exceptionPred recorder maxDelay newBaseDelay newMaxRetryCount newRng action
320
+ retryOnException exceptionPred recorder maxDelay newBaseDelay newMaxTimesRetry newRng action
317
321
318
322
| otherwise -> do
319
323
liftIO $ do
320
- log Warning $ LogHieDbRetriesExhausted baseDelay maxDelay maxRetryCount (toException e)
324
+ logWith recorder Warning $ LogHieDbRetriesExhausted baseDelay maxDelay maxTimesRetry (toException e)
321
325
throwIO e
322
326
323
327
Right b -> pure b
324
- where
325
- log = logWith recorder
326
328
327
329
-- | in microseconds
328
330
oneSecond :: Int
@@ -377,21 +379,19 @@ runWithDb recorder fp k = do
377
379
withAsync (writerThread withWriteDbRetryable chan) $ \ _ -> do
378
380
withHieDb fp (\ readDb -> k (makeWithHieDbRetryable recorder rng readDb) chan)
379
381
where
380
- log = logWith recorder
381
-
382
382
writerThread :: WithHieDb -> IndexQueue -> IO ()
383
383
writerThread withHieDbRetryable chan = do
384
384
-- Clear the index of any files that might have been deleted since the last run
385
385
_ <- withHieDbRetryable deleteMissingRealFiles
386
386
_ <- withHieDbRetryable garbageCollectTypeNames
387
387
forever $ do
388
- k <- atomically $ readTQueue chan
388
+ l <- atomically $ readTQueue chan
389
389
-- TODO: probably should let exceptions be caught/logged/handled by top level handler
390
- k withHieDbRetryable
390
+ l withHieDbRetryable
391
391
`Safe.catch` \ e@ SQLError {} -> do
392
- log Error $ LogHieDbWriterThreadSQLiteError e
393
- `Safe.catchAny` \ e -> do
394
- log Error $ LogHieDbWriterThreadException e
392
+ logWith recorder Error $ LogHieDbWriterThreadSQLiteError e
393
+ `Safe.catchAny` \ f -> do
394
+ logWith recorder Error $ LogHieDbWriterThreadException f
395
395
396
396
397
397
getHieDbLoc :: FilePath -> IO FilePath
@@ -520,7 +520,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
520
520
-- We will modify the unitId and DynFlags used for
521
521
-- compilation but these are the true source of
522
522
-- information.
523
-
523
+
524
524
new_deps = RawComponentInfo (homeUnitId_ df) df targets cfp opts dep_info
525
525
: maybe [] snd oldDeps
526
526
-- Get all the unit-ids for things in this component
@@ -532,7 +532,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
532
532
#if MIN_VERSION_ghc(9,3,0)
533
533
let (df2, uids) = (rawComponentDynFlags, [] )
534
534
#else
535
- let (df2, uids) = removeInplacePackages fakeUid inplace rawComponentDynFlags
535
+ let (df2, uids) = _removeInplacePackages fakeUid inplace rawComponentDynFlags
536
536
#endif
537
537
let prefix = show rawComponentUnitId
538
538
-- See Note [Avoiding bad interface files]
@@ -554,11 +554,11 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
554
554
-- scratch again (for now)
555
555
-- It's important to keep the same NameCache though for reasons
556
556
-- that I do not fully understand
557
- log Info $ LogMakingNewHscEnv inplace
558
- hscEnv <- emptyHscEnv ideNc libDir
557
+ logWith recorder Info $ LogMakingNewHscEnv inplace
558
+ hscEnvB <- emptyHscEnv ideNc libDir
559
559
! newHscEnv <-
560
560
-- Add the options for the current component to the HscEnv
561
- evalGhcEnv hscEnv $ do
561
+ evalGhcEnv hscEnvB $ do
562
562
_ <- setSessionDynFlags
563
563
#if !MIN_VERSION_ghc(9,3,0)
564
564
$ setHomeUnitId_ fakeUid
@@ -595,7 +595,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
595
595
res <- loadDLL hscEnv " libm.so.6"
596
596
case res of
597
597
Nothing -> pure ()
598
- Just err -> log Error $ LogDLLLoadError err
598
+ Just err -> logWith recorder Error $ LogDLLLoadError err
599
599
600
600
601
601
-- Make a map from unit-id to DynFlags, this is used when trying to
@@ -637,21 +637,22 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
637
637
let cs_exist = catMaybes (zipWith (<$) cfps' mmt)
638
638
modIfaces <- uses GetModIface cs_exist
639
639
-- update exports map
640
- extras <- getShakeExtras
640
+ shakeExtras <- getShakeExtras
641
641
let ! exportsMap' = createExportsMap $ mapMaybe (fmap hirModIface) modIfaces
642
- liftIO $ atomically $ modifyTVar' (exportsMap extras ) (exportsMap' <> )
642
+ liftIO $ atomically $ modifyTVar' (exportsMap shakeExtras ) (exportsMap' <> )
643
643
644
644
return (second Map. keys res)
645
645
646
646
let consultCradle :: Maybe FilePath -> FilePath -> IO (IdeResult HscEnvEq , [FilePath ])
647
647
consultCradle hieYaml cfp = do
648
- lfp <- flip makeRelative cfp <$> getCurrentDirectory
649
- log Info $ LogCradlePath lfp
648
+ lfpLog <- flip makeRelative cfp <$> getCurrentDirectory
649
+ logWith recorder Info $ LogCradlePath lfpLog
650
650
651
651
when (isNothing hieYaml) $
652
- log Warning $ LogCradleNotFound lfp
652
+ logWith recorder Warning $ LogCradleNotFound lfpLog
653
653
654
654
cradle <- loadCradle hieYaml dir
655
+ -- TODO: Why are we repeating the same command we have on line 646?
655
656
lfp <- flip makeRelative cfp <$> getCurrentDirectory
656
657
657
658
when optTesting $ mRunLspT lspEnv $
@@ -667,7 +668,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
667
668
addTag " result" (show res)
668
669
return res
669
670
670
- log Debug $ LogSessionLoadingResult eopts
671
+ logWith recorder Debug $ LogSessionLoadingResult eopts
671
672
case eopts of
672
673
-- The cradle gave us some options so get to work turning them
673
674
-- into and HscEnv.
@@ -727,11 +728,9 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
727
728
opts <- liftIO $ join $ mask_ $ modifyVar runningCradle $ \ as -> do
728
729
-- If the cradle is not finished, then wait for it to finish.
729
730
void $ wait as
730
- as <- async $ getOptions file
731
- return (as , wait as )
731
+ asyncRes <- async $ getOptions file
732
+ return (asyncRes , wait asyncRes )
732
733
pure opts
733
- where
734
- log = logWith recorder
735
734
736
735
-- | Run the specific cradle on a specific FilePath via hie-bios.
737
736
-- This then builds dependencies or whatever based on the cradle, gets the
@@ -787,14 +786,14 @@ fromTargetId :: [FilePath] -- ^ import paths
787
786
-> DependencyInfo
788
787
-> IO [TargetDetails ]
789
788
-- For a target module we consider all the import paths
790
- fromTargetId is exts (GHC. TargetModule mod ) env dep = do
791
- let fps = [i </> moduleNameSlashes mod -<.> ext <> boot
789
+ fromTargetId is exts (GHC. TargetModule modName ) env dep = do
790
+ let fps = [i </> moduleNameSlashes modName -<.> ext <> boot
792
791
| ext <- exts
793
792
, i <- is
794
793
, boot <- [" " , " -boot" ]
795
794
]
796
795
locs <- mapM (fmap toNormalizedFilePath' . makeAbsolute) fps
797
- return [TargetDetails (TargetModule mod ) env dep locs]
796
+ return [TargetDetails (TargetModule modName ) env dep locs]
798
797
-- For a 'TargetFile' we consider all the possible module names
799
798
fromTargetId _ _ (GHC. TargetFile f _) env deps = do
800
799
nf <- toNormalizedFilePath' <$> makeAbsolute f
@@ -1059,11 +1058,11 @@ getDependencyInfo :: [FilePath] -> IO DependencyInfo
1059
1058
getDependencyInfo fs = Map. fromList <$> mapM do_one fs
1060
1059
1061
1060
where
1062
- tryIO :: IO a -> IO (Either IOException a )
1063
- tryIO = Safe. try
1061
+ safeTryIO :: IO a -> IO (Either IOException a )
1062
+ safeTryIO = Safe. try
1064
1063
1065
1064
do_one :: FilePath -> IO (FilePath , Maybe UTCTime )
1066
- do_one fp = (fp,) . eitherToMaybe <$> tryIO (getModificationTime fp)
1065
+ do_one fp = (fp,) . eitherToMaybe <$> safeTryIO (getModificationTime fp)
1067
1066
1068
1067
-- | This function removes all the -package flags which refer to packages we
1069
1068
-- are going to deal with ourselves. For example, if a executable depends
@@ -1073,12 +1072,12 @@ getDependencyInfo fs = Map.fromList <$> mapM do_one fs
1073
1072
-- There are several places in GHC (for example the call to hptInstances in
1074
1073
-- tcRnImports) which assume that all modules in the HPT have the same unit
1075
1074
-- ID. Therefore we create a fake one and give them all the same unit id.
1076
- removeInplacePackages
1075
+ _removeInplacePackages -- Only used in ghc < 9.4
1077
1076
:: UnitId -- ^ fake uid to use for our internal component
1078
1077
-> [UnitId ]
1079
1078
-> DynFlags
1080
1079
-> (DynFlags , [UnitId ])
1081
- removeInplacePackages fake_uid us df = (setHomeUnitId_ fake_uid $
1080
+ _removeInplacePackages fake_uid us df = (setHomeUnitId_ fake_uid $
1082
1081
df { packageFlags = ps }, uids)
1083
1082
where
1084
1083
(uids, ps) = Compat. filterInplaceUnits us (packageFlags df)
0 commit comments