-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathShake.hs
1464 lines (1322 loc) · 67.4 KB
/
Shake.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE CPP #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE TypeFamilies #-}
-- | A Shake implementation of the compiler service.
--
-- There are two primary locations where data lives, and both of
-- these contain much the same data:
--
-- * The Shake database (inside 'shakeDb') stores a map of shake keys
-- to shake values. In our case, these are all of type 'Q' to 'A'.
-- During a single run all the values in the Shake database are consistent
-- so are used in conjunction with each other, e.g. in 'uses'.
--
-- * The 'Values' type stores a map of keys to values. These values are
-- always stored as real Haskell values, whereas Shake serialises all 'A' values
-- between runs. To deserialise a Shake value, we just consult Values.
module Development.IDE.Core.Shake(
IdeState, shakeSessionInit, shakeExtras, shakeDb, rootDir,
ShakeExtras(..), getShakeExtras, getShakeExtrasRules,
KnownTargets(..), Target(..), toKnownFiles, unionKnownTargets, mkKnownTargets,
IdeRule, IdeResult,
GetModificationTime(GetModificationTime, GetModificationTime_, missingFileDiagnostics),
shakeOpen, shakeShut,
shakeEnqueue,
newSession,
use, useNoFile, uses, useWithStaleFast, useWithStaleFast', delayedAction,
FastResult(..),
use_, useNoFile_, uses_,
useWithStale, usesWithStale,
useWithStale_, usesWithStale_,
BadDependency(..),
RuleBody(..),
define, defineNoDiagnostics,
defineEarlyCutoff,
defineNoFile, defineEarlyCutOffNoFile,
getDiagnostics,
mRunLspT, mRunLspTCallback,
getHiddenDiagnostics,
IsIdeGlobal, addIdeGlobal, addIdeGlobalExtras, getIdeGlobalState, getIdeGlobalAction,
getIdeGlobalExtras,
getIdeOptions,
getIdeOptionsIO,
GlobalIdeOptions(..),
HLS.getClientConfig,
getPluginConfigAction,
knownTargets,
ideLogger,
actionLogger,
getVirtualFile,
FileVersion(..),
updatePositionMapping,
updatePositionMappingHelper,
deleteValue,
WithProgressFunc, WithIndefiniteProgressFunc,
ProgressEvent(..),
DelayedAction, mkDelayedAction,
IdeAction(..), runIdeAction,
mkUpdater,
-- Exposed for testing.
Q(..),
IndexQueue,
HieDb,
HieDbWriter(..),
addPersistentRule,
garbageCollectDirtyKeys,
garbageCollectDirtyKeysOlderThan,
Log(..),
VFSModified(..), getClientConfigAction,
ThreadQueue(..),
runWithSignal
) where
import Control.Concurrent.Async
import Control.Concurrent.STM
import Control.Concurrent.STM.Stats (atomicallyNamed)
import Control.Concurrent.Strict
import Control.DeepSeq
import Control.Exception.Extra hiding (bracket_)
import Control.Lens ((%~), (&), (?~))
import Control.Monad.Extra
import Control.Monad.IO.Class
import Control.Monad.Reader
import Control.Monad.Trans.Maybe
import Data.Aeson (Result (Success),
toJSON)
import qualified Data.Aeson.Types as A
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Char8 as BS8
import Data.Coerce (coerce)
import Data.Default
import Data.Dynamic
import Data.EnumMap.Strict (EnumMap)
import qualified Data.EnumMap.Strict as EM
import Data.Foldable (find, for_)
import Data.Functor ((<&>))
import Data.Functor.Identity
import Data.Hashable
import qualified Data.HashMap.Strict as HMap
import Data.HashSet (HashSet)
import qualified Data.HashSet as HSet
import Data.List.Extra (foldl', partition,
takeEnd)
import qualified Data.Map.Strict as Map
import Data.Maybe
import qualified Data.SortedList as SL
import Data.String (fromString)
import qualified Data.Text as T
import Data.Time
import Data.Traversable
import Data.Tuple.Extra
import Data.Typeable
import Data.Unique
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Development.IDE.Core.Debouncer
import Development.IDE.Core.FileUtils (getModTime)
import Development.IDE.Core.PositionMapping
import Development.IDE.Core.ProgressReporting
import Development.IDE.Core.RuleTypes
import Development.IDE.Types.Options as Options
import qualified Language.LSP.Protocol.Message as LSP
import qualified Language.LSP.Server as LSP
import Development.IDE.Core.Tracing
import Development.IDE.Core.WorkerThread
import Development.IDE.GHC.Compat (NameCache,
NameCacheUpdater,
initNameCache,
knownKeyNames)
import Development.IDE.GHC.Orphans ()
import Development.IDE.Graph hiding (ShakeValue,
action)
import qualified Development.IDE.Graph as Shake
import Development.IDE.Graph.Database (ShakeDatabase,
shakeGetBuildStep,
shakeGetDatabaseKeys,
shakeNewDatabase,
shakeProfileDatabase,
shakeRunDatabaseForKeys)
import Development.IDE.Graph.Rule
import Development.IDE.Types.Action
import Development.IDE.Types.Diagnostics
import Development.IDE.Types.Exports hiding (exportsMapSize)
import qualified Development.IDE.Types.Exports as ExportsMap
import Development.IDE.Types.KnownTargets
import Development.IDE.Types.Location
import Development.IDE.Types.Monitoring (Monitoring (..))
import Development.IDE.Types.Shake
import qualified Focus
import GHC.Fingerprint
import GHC.Stack (HasCallStack)
import GHC.TypeLits (KnownSymbol)
import HieDb.Types
import Ide.Logger hiding (Priority)
import qualified Ide.Logger as Logger
import Ide.Plugin.Config
import qualified Ide.PluginUtils as HLS
import Ide.Types
import qualified Language.LSP.Protocol.Lens as L
import Language.LSP.Protocol.Message
import Language.LSP.Protocol.Types
import qualified Language.LSP.Protocol.Types as LSP
import Language.LSP.VFS hiding (start)
import qualified "list-t" ListT
import OpenTelemetry.Eventlog hiding (addEvent)
import qualified Prettyprinter as Pretty
import qualified StmContainers.Map as STM
import System.FilePath hiding (makeRelative)
import System.IO.Unsafe (unsafePerformIO)
import System.Time.Extra
import UnliftIO (MonadUnliftIO (withRunInIO))
data Log
= LogCreateHieDbExportsMapStart
| LogCreateHieDbExportsMapFinish !Int
| LogBuildSessionRestart !String ![DelayedActionInternal] !KeySet !Seconds !(Maybe FilePath)
| LogBuildSessionRestartTakingTooLong !Seconds
| LogDelayedAction !(DelayedAction ()) !Seconds
| LogBuildSessionFinish !(Maybe SomeException)
| LogDiagsDiffButNoLspEnv ![FileDiagnostic]
| LogDefineEarlyCutoffRuleNoDiagHasDiag !FileDiagnostic
| LogDefineEarlyCutoffRuleCustomNewnessHasDiag !FileDiagnostic
| LogCancelledAction !T.Text
| LogSessionInitialised
| LogLookupPersistentKey !T.Text
| LogShakeGarbageCollection !T.Text !Int !Seconds
-- * OfInterest Log messages
| LogSetFilesOfInterest ![(NormalizedFilePath, FileOfInterestStatus)]
deriving Show
instance Pretty Log where
pretty = \case
LogCreateHieDbExportsMapStart ->
"Initializing exports map from hiedb"
LogCreateHieDbExportsMapFinish exportsMapSize ->
"Done initializing exports map from hiedb. Size:" <+> pretty exportsMapSize
LogBuildSessionRestart reason actionQueue keyBackLog abortDuration shakeProfilePath ->
vcat
[ "Restarting build session due to" <+> pretty reason
, "Action Queue:" <+> pretty (map actionName actionQueue)
, "Keys:" <+> pretty (map show $ toListKeySet keyBackLog)
, "Aborting previous build session took" <+> pretty (showDuration abortDuration) <+> pretty shakeProfilePath ]
LogBuildSessionRestartTakingTooLong seconds ->
"Build restart is taking too long (" <> pretty seconds <> " seconds)"
LogDelayedAction delayedAct seconds ->
hsep
[ "Finished:" <+> pretty (actionName delayedAct)
, "Took:" <+> pretty (showDuration seconds) ]
LogBuildSessionFinish e ->
vcat
[ "Finished build session"
, pretty (fmap displayException e) ]
LogDiagsDiffButNoLspEnv fileDiagnostics ->
"updateFileDiagnostics published different from new diagnostics - file diagnostics:"
<+> pretty (showDiagnosticsColored fileDiagnostics)
LogDefineEarlyCutoffRuleNoDiagHasDiag fileDiagnostic ->
"defineEarlyCutoff RuleNoDiagnostics - file diagnostic:"
<+> pretty (showDiagnosticsColored [fileDiagnostic])
LogDefineEarlyCutoffRuleCustomNewnessHasDiag fileDiagnostic ->
"defineEarlyCutoff RuleWithCustomNewnessCheck - file diagnostic:"
<+> pretty (showDiagnosticsColored [fileDiagnostic])
LogCancelledAction action ->
pretty action <+> "was cancelled"
LogSessionInitialised -> "Shake session initialized"
LogLookupPersistentKey key ->
"LOOKUP PERSISTENT FOR:" <+> pretty key
LogShakeGarbageCollection label number duration ->
pretty label <+> "of" <+> pretty number <+> "keys (took " <+> pretty (showDuration duration) <> ")"
LogSetFilesOfInterest ofInterest ->
"Set files of interst to" <> Pretty.line
<> indent 4 (pretty $ fmap (first fromNormalizedFilePath) ofInterest)
-- | We need to serialize writes to the database, so we send any function that
-- needs to write to the database over the channel, where it will be picked up by
-- a worker thread.
data HieDbWriter
= HieDbWriter
{ indexQueue :: IndexQueue
, indexPending :: TVar (HMap.HashMap NormalizedFilePath Fingerprint) -- ^ Avoid unnecessary/out of date indexing
, indexCompleted :: TVar Int -- ^ to report progress
, indexProgressReporting :: ProgressReporting
}
-- | Actions to queue up on the index worker thread
-- The inner `(HieDb -> IO ()) -> IO ()` wraps `HieDb -> IO ()`
-- with (currently) retry functionality
type IndexQueue = TQueue (((HieDb -> IO ()) -> IO ()) -> IO ())
data ThreadQueue = ThreadQueue {
tIndexQueue :: IndexQueue
, tRestartQueue :: TQueue (IO ())
, tLoaderQueue :: TQueue (IO ())
}
-- Note [Semantic Tokens Cache Location]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- storing semantic tokens cache for each file in shakeExtras might
-- not be ideal, since it most used in LSP request handlers
-- instead of rules. We should consider moving it to a more
-- appropriate place in the future if we find one, store it for now.
-- information we stash inside the shakeExtra field
data ShakeExtras = ShakeExtras
{ --eventer :: LSP.FromServerMessage -> IO ()
lspEnv :: Maybe (LSP.LanguageContextEnv Config)
,debouncer :: Debouncer NormalizedUri
,shakeRecorder :: Recorder (WithPriority Log)
,idePlugins :: IdePlugins IdeState
,globals :: TVar (HMap.HashMap TypeRep Dynamic)
-- ^ Registry of global state used by rules.
-- Small and immutable after startup, so not worth using an STM.Map.
,state :: Values
,diagnostics :: STMDiagnosticStore
,hiddenDiagnostics :: STMDiagnosticStore
,publishedDiagnostics :: STM.Map NormalizedUri [FileDiagnostic]
-- ^ This represents the set of diagnostics that we have published.
-- Due to debouncing not every change might get published.
,semanticTokensCache:: STM.Map NormalizedFilePath SemanticTokens
-- ^ Cache of last response of semantic tokens for each file,
-- so we can compute deltas for semantic tokens(SMethod_TextDocumentSemanticTokensFullDelta).
-- putting semantic tokens cache and id in shakeExtras might not be ideal
-- see Note [Semantic Tokens Cache Location]
,semanticTokensId :: TVar Int
-- ^ semanticTokensId is used to generate unique ids for each lsp response of semantic tokens.
,positionMapping :: STM.Map NormalizedUri (EnumMap Int32 (PositionDelta, PositionMapping))
-- ^ Map from a text document version to a PositionMapping that describes how to map
-- positions in a version of that document to positions in the latest version
-- First mapping is delta from previous version and second one is an
-- accumulation to the current version.
,progress :: PerFileProgressReporting
,ideTesting :: IdeTesting
-- ^ Whether to enable additional lsp messages used by the test suite for checking invariants
,restartShakeSession
:: VFSModified
-> String
-> [DelayedAction ()]
-> IO [Key]
-> IO ()
,ideNc :: NameCache
-- | A mapping of module name to known target (or candidate targets, if missing)
,knownTargetsVar :: TVar (Hashed KnownTargets)
-- | A mapping of exported identifiers for local modules. Updated on kick
,exportsMap :: TVar ExportsMap
-- | A work queue for actions added via 'runInShakeSession'
,actionQueue :: ActionQueue
,clientCapabilities :: ClientCapabilities
, withHieDb :: WithHieDb -- ^ Use only to read.
, hiedbWriter :: HieDbWriter -- ^ use to write
, persistentKeys :: TVar (KeyMap GetStalePersistent)
-- ^ Registry for functions that compute/get "stale" results for the rule
-- (possibly from disk)
, vfsVar :: TVar VFS
-- ^ A snapshot of the current state of the virtual file system. Updated on shakeRestart
-- VFS state is managed by LSP. However, the state according to the lsp library may be newer than the state of the current session,
-- leaving us vulnerable to subtle race conditions. To avoid this, we take a snapshot of the state of the VFS on every
-- restart, so that the whole session sees a single consistent view of the VFS.
-- We don't need a STM.Map because we never update individual keys ourselves.
, defaultConfig :: Config
-- ^ Default HLS config, only relevant if the client does not provide any Config
, dirtyKeys :: TVar KeySet
-- ^ Set of dirty rule keys since the last Shake run
, restartQueue :: TQueue (IO ())
-- ^ Queue of restart actions to be run.
, loaderQueue :: TQueue (IO ())
-- ^ Queue of loader actions to be run.
}
type WithProgressFunc = forall a.
T.Text -> LSP.ProgressCancellable -> ((LSP.ProgressAmount -> IO ()) -> IO a) -> IO a
type WithIndefiniteProgressFunc = forall a.
T.Text -> LSP.ProgressCancellable -> IO a -> IO a
type GetStalePersistent = NormalizedFilePath -> IdeAction (Maybe (Dynamic,PositionDelta,Maybe Int32))
getShakeExtras :: Action ShakeExtras
getShakeExtras = do
-- Will fail the action with a pattern match failure, but be caught
Just x <- getShakeExtra @ShakeExtras
return x
getShakeExtrasRules :: Rules ShakeExtras
getShakeExtrasRules = do
mExtras <- getShakeExtraRules @ShakeExtras
case mExtras of
Just x -> return x
-- This will actually crash HLS
Nothing -> liftIO $ fail "missing ShakeExtras"
-- See Note [Client configuration in Rules]
-- | Returns the client configuration, creating a build dependency.
-- You should always use this function when accessing client configuration
-- from build rules.
getClientConfigAction :: Action Config
getClientConfigAction = do
ShakeExtras{lspEnv, idePlugins} <- getShakeExtras
currentConfig <- (`LSP.runLspT` LSP.getConfig) `traverse` lspEnv
mbVal <- unhashed <$> useNoFile_ GetClientSettings
let defValue = fromMaybe def currentConfig
case A.parse (parseConfig idePlugins defValue) <$> mbVal of
Just (Success c) -> return c
_ -> return defValue
getPluginConfigAction :: PluginId -> Action PluginConfig
getPluginConfigAction plId = do
config <- getClientConfigAction
ShakeExtras{idePlugins = IdePlugins plugins} <- getShakeExtras
let plugin = fromMaybe (error $ "Plugin not found: " <> show plId) $
find (\p -> pluginId p == plId) plugins
return $ HLS.configForPlugin config plugin
-- | Register a function that will be called to get the "stale" result of a rule, possibly from disk
-- This is called when we don't already have a result, or computing the rule failed.
-- The result of this function will always be marked as 'stale', and a 'proper' rebuild of the rule will
-- be queued if the rule hasn't run before.
addPersistentRule :: IdeRule k v => k -> (NormalizedFilePath -> IdeAction (Maybe (v,PositionDelta,Maybe Int32))) -> Rules ()
addPersistentRule k getVal = do
ShakeExtras{persistentKeys} <- getShakeExtrasRules
void $ liftIO $ atomically $ modifyTVar' persistentKeys $ insertKeyMap (newKey k) (fmap (fmap (first3 toDyn)) . getVal)
class Typeable a => IsIdeGlobal a where
-- | Read a virtual file from the current snapshot
getVirtualFile :: NormalizedFilePath -> Action (Maybe VirtualFile)
getVirtualFile nf = do
vfs <- fmap _vfsMap . liftIO . readTVarIO . vfsVar =<< getShakeExtras
pure $! Map.lookup (filePathToUri' nf) vfs -- Don't leak a reference to the entire map
-- Take a snapshot of the current LSP VFS
vfsSnapshot :: Maybe (LSP.LanguageContextEnv a) -> IO VFS
vfsSnapshot Nothing = pure $ VFS mempty
vfsSnapshot (Just lspEnv) = LSP.runLspT lspEnv LSP.getVirtualFiles
addIdeGlobal :: IsIdeGlobal a => a -> Rules ()
addIdeGlobal x = do
extras <- getShakeExtrasRules
liftIO $ addIdeGlobalExtras extras x
addIdeGlobalExtras :: IsIdeGlobal a => ShakeExtras -> a -> IO ()
addIdeGlobalExtras ShakeExtras{globals} x@(typeOf -> ty) =
void $ liftIO $ atomically $ modifyTVar' globals $ \mp -> case HMap.lookup ty mp of
Just _ -> error $ "Internal error, addIdeGlobalExtras, got the same type twice for " ++ show ty
Nothing -> HMap.insert ty (toDyn x) mp
getIdeGlobalExtras :: forall a . (HasCallStack, IsIdeGlobal a) => ShakeExtras -> IO a
getIdeGlobalExtras ShakeExtras{globals} = do
let typ = typeRep (Proxy :: Proxy a)
x <- HMap.lookup (typeRep (Proxy :: Proxy a)) <$> readTVarIO globals
case x of
Just y
| Just z <- fromDynamic y -> pure z
| otherwise -> errorIO $ "Internal error, getIdeGlobalExtras, wrong type for " ++ show typ ++ " (got " ++ show (dynTypeRep y) ++ ")"
Nothing -> errorIO $ "Internal error, getIdeGlobalExtras, no entry for " ++ show typ
getIdeGlobalAction :: forall a . (HasCallStack, IsIdeGlobal a) => Action a
getIdeGlobalAction = liftIO . getIdeGlobalExtras =<< getShakeExtras
getIdeGlobalState :: forall a . IsIdeGlobal a => IdeState -> IO a
getIdeGlobalState = getIdeGlobalExtras . shakeExtras
newtype GlobalIdeOptions = GlobalIdeOptions IdeOptions
instance IsIdeGlobal GlobalIdeOptions
getIdeOptions :: Action IdeOptions
getIdeOptions = do
GlobalIdeOptions x <- getIdeGlobalAction
mbEnv <- lspEnv <$> getShakeExtras
case mbEnv of
Nothing -> return x
Just env -> do
config <- liftIO $ LSP.runLspT env HLS.getClientConfig
return x{optCheckProject = pure $ checkProject config,
optCheckParents = pure $ checkParents config
}
getIdeOptionsIO :: ShakeExtras -> IO IdeOptions
getIdeOptionsIO ide = do
GlobalIdeOptions x <- getIdeGlobalExtras ide
return x
-- | Return the most recent, potentially stale, value and a PositionMapping
-- for the version of that value.
lastValueIO :: IdeRule k v => ShakeExtras -> k -> NormalizedFilePath -> IO (Maybe (v, PositionMapping))
lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
let readPersistent
| IdeTesting testing <- ideTesting s -- Don't read stale persistent values in tests
, testing = pure Nothing
| otherwise = do
pmap <- readTVarIO persistentKeys
mv <- runMaybeT $ do
liftIO $ logWith (shakeRecorder s) Debug $ LogLookupPersistentKey (T.pack $ show k)
f <- MaybeT $ pure $ lookupKeyMap (newKey k) pmap
(dv,del,ver) <- MaybeT $ runIdeAction "lastValueIO" s $ f file
MaybeT $ pure $ (,del,ver) <$> fromDynamic dv
case mv of
Nothing -> atomicallyNamed "lastValueIO 1" $ do
STM.focus (Focus.alter (alterValue $ Failed True)) (toKey k file) state
return Nothing
Just (v,del,mbVer) -> do
actual_version <- case mbVer of
Just ver -> pure (Just $ VFSVersion ver)
Nothing -> (Just . ModificationTime <$> getModTime (fromNormalizedFilePath file))
`catch` (\(_ :: IOException) -> pure Nothing)
atomicallyNamed "lastValueIO 2" $ do
STM.focus (Focus.alter (alterValue $ Stale (Just del) actual_version (toDyn v))) (toKey k file) state
Just . (v,) . addOldDelta del <$> mappingForVersion positionMapping file actual_version
-- We got a new stale value from the persistent rule, insert it in the map without affecting diagnostics
alterValue new Nothing = Just (ValueWithDiagnostics new mempty) -- If it wasn't in the map, give it empty diagnostics
alterValue new (Just old@(ValueWithDiagnostics val diags)) = Just $ case val of
-- Old failed, we can update it preserving diagnostics
Failed{} -> ValueWithDiagnostics new diags
-- Something already succeeded before, leave it alone
_ -> old
atomicallyNamed "lastValueIO 4" (STM.lookup (toKey k file) state) >>= \case
Nothing -> readPersistent
Just (ValueWithDiagnostics value _) -> case value of
Succeeded ver (fromDynamic -> Just v) ->
atomicallyNamed "lastValueIO 5" $ Just . (v,) <$> mappingForVersion positionMapping file ver
Stale del ver (fromDynamic -> Just v) ->
atomicallyNamed "lastValueIO 6" $ Just . (v,) . maybe id addOldDelta del <$> mappingForVersion positionMapping file ver
Failed p | not p -> readPersistent
_ -> pure Nothing
-- | Return the most recent, potentially stale, value and a PositionMapping
-- for the version of that value.
lastValue :: IdeRule k v => k -> NormalizedFilePath -> Action (Maybe (v, PositionMapping))
lastValue key file = do
s <- getShakeExtras
liftIO $ lastValueIO s key file
mappingForVersion
:: STM.Map NormalizedUri (EnumMap Int32 (a, PositionMapping))
-> NormalizedFilePath
-> Maybe FileVersion
-> STM PositionMapping
mappingForVersion allMappings file (Just (VFSVersion ver)) = do
mapping <- STM.lookup (filePathToUri' file) allMappings
return $ maybe zeroMapping snd $ EM.lookup ver =<< mapping
mappingForVersion _ _ _ = pure zeroMapping
type IdeRule k v =
( Shake.RuleResult k ~ v
, Shake.ShakeValue k
, Show v
, Typeable v
, NFData v
)
-- | A live Shake session with the ability to enqueue Actions for running.
-- Keeps the 'ShakeDatabase' open, so at most one 'ShakeSession' per database.
newtype ShakeSession = ShakeSession
{ cancelShakeSession :: IO ()
-- ^ Closes the Shake session
}
-- Note [Root Directory]
-- ~~~~~~~~~~~~~~~~~~~~~
-- We keep track of the root directory explicitly, which is the directory of the project root.
-- We might be setting it via these options with decreasing priority:
--
-- 1. from LSP workspace root, `resRootPath` in `LanguageContextEnv`.
-- 2. command line (--cwd)
-- 3. default to the current directory.
--
-- Using `getCurrentDirectory` makes it more difficult to run the tests, as we spawn one thread of HLS per test case.
-- If we modify the global Variable CWD, via `setCurrentDirectory`, all other test threads are suddenly affected,
-- forcing us to run all integration tests sequentially.
--
-- Also, there might be a race condition if we depend on the current directory, as some plugin might change it.
-- e.g. stylish's `loadConfig`. https://github.com/haskell/haskell-language-server/issues/4234
--
-- But according to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_workspaceFolders
-- The root dir is deprecated, that means we should cleanup dependency on the project root(Or $CWD) thing gradually,
-- so multi-workspaces can actually be supported when we use absolute path everywhere(might also need some high level design).
-- That might not be possible unless we have everything adapted to it, like 'hlint' and 'evaluation of template haskell'.
-- But we should still be working towards the goal.
--
-- We can drop it in the future once:
-- 1. We can get rid all the usages of root directory in the codebase.
-- 2. LSP version we support actually removes the root directory from the protocol.
--
-- | A Shake database plus persistent store. Can be thought of as storing
-- mappings from @(FilePath, k)@ to @RuleResult k@.
data IdeState = IdeState
{shakeDb :: ShakeDatabase
,shakeSession :: MVar ShakeSession
,shakeExtras :: ShakeExtras
,shakeDatabaseProfile :: ShakeDatabase -> IO (Maybe FilePath)
,stopMonitoring :: IO ()
-- | See Note [Root Directory]
,rootDir :: FilePath
}
-- This is debugging code that generates a series of profiles, if the Boolean is true
shakeDatabaseProfileIO :: Maybe FilePath -> IO(ShakeDatabase -> IO (Maybe FilePath))
shakeDatabaseProfileIO mbProfileDir = do
profileStartTime <- formatTime defaultTimeLocale "%Y%m%d-%H%M%S" <$> getCurrentTime
profileCounter <- newVar (0::Int)
return $ \shakeDb ->
for mbProfileDir $ \dir -> do
count <- modifyVar profileCounter $ \x -> let !y = x+1 in return (y,y)
let file = "ide-" ++ profileStartTime ++ "-" ++ takeEnd 5 ("0000" ++ show count) <.> "html"
shakeProfileDatabase shakeDb $ dir </> file
return (dir </> file)
setValues :: IdeRule k v
=> Values
-> k
-> NormalizedFilePath
-> Value v
-> Vector FileDiagnostic
-> STM ()
setValues state key file val diags =
STM.insert (ValueWithDiagnostics (fmap toDyn val) diags) (toKey key file) state
-- | Delete the value stored for a given ide build key
-- and return the key that was deleted.
deleteValue
:: Shake.ShakeValue k
=> ShakeExtras
-> k
-> NormalizedFilePath
-> STM [Key]
deleteValue ShakeExtras{state} key file = do
STM.delete (toKey key file) state
return [toKey key file]
-- | We return Nothing if the rule has not run and Just Failed if it has failed to produce a value.
getValues ::
forall k v.
IdeRule k v =>
Values ->
k ->
NormalizedFilePath ->
STM (Maybe (Value v, Vector FileDiagnostic))
getValues state key file = do
STM.lookup (toKey key file) state >>= \case
Nothing -> pure Nothing
Just (ValueWithDiagnostics v diagsV) -> do
let !r = seqValue $ fmap (fromJust . fromDynamic @v) v
!res = (r,diagsV)
-- Force to make sure we do not retain a reference to the HashMap
-- and we blow up immediately if the fromJust should fail
-- (which would be an internal error).
return $ Just res
-- | Get all the files in the project
knownTargets :: Action (Hashed KnownTargets)
knownTargets = do
ShakeExtras{knownTargetsVar} <- getShakeExtras
liftIO $ readTVarIO knownTargetsVar
-- | Seq the result stored in the Shake value. This only
-- evaluates the value to WHNF not NF. We take care of the latter
-- elsewhere and doing it twice is expensive.
seqValue :: Value v -> Value v
seqValue val = case val of
Succeeded ver v -> rnf ver `seq` v `seq` val
Stale d ver v -> rnf d `seq` rnf ver `seq` v `seq` val
Failed _ -> val
-- | Open a 'IdeState', should be shut using 'shakeShut'.
shakeOpen :: Recorder (WithPriority Log)
-> Maybe (LSP.LanguageContextEnv Config)
-> Config
-> IdePlugins IdeState
-> Debouncer NormalizedUri
-> Maybe FilePath
-> IdeReportProgress
-> IdeTesting
-> WithHieDb
-> ThreadQueue
-> ShakeOptions
-> Monitoring
-> Rules ()
-> FilePath
-- ^ Root directory, this one might be picking up from `LanguageContextEnv`'s `resRootPath`
-- , see Note [Root Directory]
-> IO IdeState
shakeOpen recorder lspEnv defaultConfig idePlugins debouncer
shakeProfileDir (IdeReportProgress reportProgress)
ideTesting
withHieDb threadQueue opts monitoring rules rootDir = mdo
-- see Note [Serializing runs in separate thread]
let indexQueue = tIndexQueue threadQueue
restartQueue = tRestartQueue threadQueue
loaderQueue = tLoaderQueue threadQueue
ideNc <- initNameCache 'r' knownKeyNames
shakeExtras <- do
globals <- newTVarIO HMap.empty
state <- STM.newIO
diagnostics <- STM.newIO
hiddenDiagnostics <- STM.newIO
publishedDiagnostics <- STM.newIO
semanticTokensCache <- STM.newIO
positionMapping <- STM.newIO
knownTargetsVar <- newTVarIO $ hashed emptyKnownTargets
let restartShakeSession = shakeRestart recorder ideState
persistentKeys <- newTVarIO mempty
indexPending <- newTVarIO HMap.empty
indexCompleted <- newTVarIO 0
semanticTokensId <- newTVarIO 0
indexProgressReporting <- progressReportingNoTrace
(liftM2 (+) (length <$> readTVar indexPending) (readTVar indexCompleted))
(readTVar indexCompleted)
lspEnv "Indexing" optProgressStyle
let hiedbWriter = HieDbWriter{..}
exportsMap <- newTVarIO mempty
-- lazily initialize the exports map with the contents of the hiedb
-- TODO: exceptions can be swallowed here?
_ <- async $ do
logWith recorder Debug LogCreateHieDbExportsMapStart
em <- createExportsMapHieDb withHieDb
atomically $ modifyTVar' exportsMap (<> em)
logWith recorder Debug $ LogCreateHieDbExportsMapFinish (ExportsMap.size em)
progress <-
if reportProgress
then progressReporting lspEnv "Processing" optProgressStyle
else noPerFileProgressReporting
actionQueue <- newQueue
let clientCapabilities = maybe def LSP.resClientCapabilities lspEnv
dirtyKeys <- newTVarIO mempty
-- Take one VFS snapshot at the start
vfsVar <- newTVarIO =<< vfsSnapshot lspEnv
pure ShakeExtras{shakeRecorder = recorder, ..}
shakeDb <-
shakeNewDatabase
opts { shakeExtra = newShakeExtra shakeExtras }
rules
shakeSession <- newEmptyMVar
shakeDatabaseProfile <- shakeDatabaseProfileIO shakeProfileDir
IdeOptions
{ optProgressStyle
, optCheckParents
} <- getIdeOptionsIO shakeExtras
checkParents <- optCheckParents
-- monitoring
let readValuesCounter = fromIntegral . countRelevantKeys checkParents <$> getStateKeys shakeExtras
readDirtyKeys = fromIntegral . countRelevantKeys checkParents . toListKeySet <$> readTVarIO(dirtyKeys shakeExtras)
readIndexPending = fromIntegral . HMap.size <$> readTVarIO (indexPending $ hiedbWriter shakeExtras)
readExportsMap = fromIntegral . ExportsMap.exportsMapSize <$> readTVarIO (exportsMap shakeExtras)
readDatabaseCount = fromIntegral . countRelevantKeys checkParents . map fst <$> shakeGetDatabaseKeys shakeDb
readDatabaseStep = fromIntegral <$> shakeGetBuildStep shakeDb
registerGauge monitoring "ghcide.values_count" readValuesCounter
registerGauge monitoring "ghcide.dirty_keys_count" readDirtyKeys
registerGauge monitoring "ghcide.indexing_pending_count" readIndexPending
registerGauge monitoring "ghcide.exports_map_count" readExportsMap
registerGauge monitoring "ghcide.database_count" readDatabaseCount
registerCounter monitoring "ghcide.num_builds" readDatabaseStep
stopMonitoring <- start monitoring
let ideState = IdeState{..}
return ideState
getStateKeys :: ShakeExtras -> IO [Key]
getStateKeys = (fmap.fmap) fst . atomically . ListT.toList . STM.listT . state
-- | Must be called in the 'Initialized' handler and only once
shakeSessionInit :: Recorder (WithPriority Log) -> IdeState -> IO ()
shakeSessionInit recorder IdeState{..} = do
-- Take a snapshot of the VFS - it should be empty as we've received no notifications
-- till now, but it can't hurt to be in sync with the `lsp` library.
vfs <- vfsSnapshot (lspEnv shakeExtras)
initSession <- newSession recorder shakeExtras (VFSModified vfs) shakeDb [] "shakeSessionInit"
putMVar shakeSession initSession
logWith recorder Debug LogSessionInitialised
shakeShut :: IdeState -> IO ()
shakeShut IdeState{..} = do
runner <- tryReadMVar shakeSession
-- Shake gets unhappy if you try to close when there is a running
-- request so we first abort that.
for_ runner cancelShakeSession
void $ shakeDatabaseProfile shakeDb
progressStop $ progress shakeExtras
progressStop $ indexProgressReporting $ hiedbWriter shakeExtras
stopMonitoring
-- | This is a variant of withMVar where the first argument is run unmasked and if it throws
-- an exception, the previous value is restored while the second argument is executed masked.
withMVar' :: MVar a -> (a -> IO b) -> (b -> IO (a, c)) -> IO c
withMVar' var unmasked masked = uninterruptibleMask $ \restore -> do
a <- takeMVar var
b <- restore (unmasked a) `onException` putMVar var a
(a', c) <- masked b
putMVar var a'
pure c
mkDelayedAction :: String -> Logger.Priority -> Action a -> DelayedAction a
mkDelayedAction = DelayedAction Nothing
-- | These actions are run asynchronously after the current action is
-- finished running. For example, to trigger a key build after a rule
-- has already finished as is the case with useWithStaleFast
delayedAction :: DelayedAction a -> IdeAction (IO a)
delayedAction a = do
extras <- ask
liftIO $ shakeEnqueue extras a
-- | Restart the current 'ShakeSession' with the given system actions.
-- Any actions running in the current session will be aborted,
-- but actions added via 'shakeEnqueue' will be requeued.
shakeRestart :: Recorder (WithPriority Log) -> IdeState -> VFSModified -> String -> [DelayedAction ()] -> IO [Key] -> IO ()
shakeRestart recorder IdeState{..} vfs reason acts ioActionBetweenShakeSession =
void $ awaitRunInThread (restartQueue shakeExtras) $ do
withMVar'
shakeSession
(\runner -> do
(stopTime,()) <- duration $ logErrorAfter 10 $ cancelShakeSession runner
keys <- ioActionBetweenShakeSession
-- it is every important to update the dirty keys after we enter the critical section
-- see Note [Housekeeping rule cache and dirty key outside of hls-graph]
atomically $ modifyTVar' (dirtyKeys shakeExtras) $ \x -> foldl' (flip insertKeySet) x keys
res <- shakeDatabaseProfile shakeDb
backlog <- readTVarIO $ dirtyKeys shakeExtras
queue <- atomicallyNamed "actionQueue - peek" $ peekInProgress $ actionQueue shakeExtras
-- this log is required by tests
logWith recorder Debug $ LogBuildSessionRestart reason queue backlog stopTime res
)
-- It is crucial to be masked here, otherwise we can get killed
-- between spawning the new thread and updating shakeSession.
-- See https://github.com/haskell/ghcide/issues/79
(\() -> do
(,()) <$> newSession recorder shakeExtras vfs shakeDb acts reason)
where
logErrorAfter :: Seconds -> IO () -> IO ()
logErrorAfter seconds action = flip withAsync (const action) $ do
sleep seconds
logWith recorder Error (LogBuildSessionRestartTakingTooLong seconds)
-- | Enqueue an action in the existing 'ShakeSession'.
-- Returns a computation to block until the action is run, propagating exceptions.
-- Assumes a 'ShakeSession' is available.
--
-- Appropriate for user actions other than edits.
shakeEnqueue :: ShakeExtras -> DelayedAction a -> IO (IO a)
shakeEnqueue ShakeExtras{actionQueue, shakeRecorder} act = do
(b, dai) <- instantiateDelayedAction act
atomicallyNamed "actionQueue - push" $ pushQueue dai actionQueue
let wait' barrier =
waitBarrier barrier `catches`
[ Handler(\BlockedIndefinitelyOnMVar ->
fail $ "internal bug: forever blocked on MVar for " <>
actionName act)
, Handler (\e@AsyncCancelled -> do
logWith shakeRecorder Debug $ LogCancelledAction (T.pack $ actionName act)
atomicallyNamed "actionQueue - abort" $ abortQueue dai actionQueue
throw e)
]
return (wait' b >>= either throwIO return)
data VFSModified = VFSUnmodified | VFSModified !VFS
-- | Set up a new 'ShakeSession' with a set of initial actions
-- Will crash if there is an existing 'ShakeSession' running.
newSession
:: Recorder (WithPriority Log)
-> ShakeExtras
-> VFSModified
-> ShakeDatabase
-> [DelayedActionInternal]
-> String
-> IO ShakeSession
newSession recorder extras@ShakeExtras{..} vfsMod shakeDb acts reason = do
-- Take a new VFS snapshot
case vfsMod of
VFSUnmodified -> pure ()
VFSModified vfs -> atomically $ writeTVar vfsVar vfs
IdeOptions{optRunSubset} <- getIdeOptionsIO extras
reenqueued <- atomicallyNamed "actionQueue - peek" $ peekInProgress actionQueue
allPendingKeys <-
if optRunSubset
then Just <$> readTVarIO dirtyKeys
else return Nothing
let
-- A daemon-like action used to inject additional work
-- Runs actions from the work queue sequentially
pumpActionThread otSpan = do
d <- liftIO $ atomicallyNamed "action queue - pop" $ popQueue actionQueue
actionFork (run otSpan d) $ \_ -> pumpActionThread otSpan
-- TODO figure out how to thread the otSpan into defineEarlyCutoff
run _otSpan d = do
start <- liftIO offsetTime
getAction d
liftIO $ atomicallyNamed "actionQueue - done" $ doneQueue d actionQueue
runTime <- liftIO start
logWith recorder (actionPriority d) $ LogDelayedAction d runTime
-- The inferred type signature doesn't work in ghc >= 9.0.1
workRun :: (forall b. IO b -> IO b) -> IO (IO ())
workRun restore = withSpan "Shake session" $ \otSpan -> do
setTag otSpan "reason" (fromString reason)
setTag otSpan "queue" (fromString $ unlines $ map actionName reenqueued)
whenJust allPendingKeys $ \kk -> setTag otSpan "keys" (BS8.pack $ unlines $ map show $ toListKeySet kk)
let keysActs = pumpActionThread otSpan : map (run otSpan) (reenqueued ++ acts)
res <- try @SomeException $
restore $ shakeRunDatabaseForKeys (toListKeySet <$> allPendingKeys) shakeDb keysActs
return $ do
let exception =
case res of
Left e -> Just e
_ -> Nothing
logWith recorder Debug $ LogBuildSessionFinish exception
-- Do the work in a background thread
workThread <- asyncWithUnmask workRun
-- run the wrap up in a separate thread since it contains interruptible
-- commands (and we are not using uninterruptible mask)
-- TODO: can possibly swallow exceptions?
_ <- async $ join $ wait workThread
-- Cancelling is required to flush the Shake database when either
-- the filesystem or the Ghc configuration have changed
let cancelShakeSession :: IO ()
cancelShakeSession = cancel workThread
pure (ShakeSession{..})
instantiateDelayedAction
:: DelayedAction a
-> IO (Barrier (Either SomeException a), DelayedActionInternal)
instantiateDelayedAction (DelayedAction _ s p a) = do
u <- newUnique
b <- newBarrier
let a' = do
-- work gets reenqueued when the Shake session is restarted
-- it can happen that a work item finished just as it was reenqueued
-- in that case, skipping the work is fine
alreadyDone <- liftIO $ isJust <$> waitBarrierMaybe b
unless alreadyDone $ do
x <- actionCatch @SomeException (Right <$> a) (pure . Left)
-- ignore exceptions if the barrier has been filled concurrently
liftIO $ void $ try @SomeException $ signalBarrier b x
d' = DelayedAction (Just u) s p a'
return (b, d')
getDiagnostics :: IdeState -> STM [FileDiagnostic]
getDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do
getAllDiagnostics diagnostics
getHiddenDiagnostics :: IdeState -> STM [FileDiagnostic]
getHiddenDiagnostics IdeState{shakeExtras = ShakeExtras{hiddenDiagnostics}} = do
getAllDiagnostics hiddenDiagnostics
-- | Find and release old keys from the state Hashmap
-- For the record, there are other state sources that this process does not release:
-- * diagnostics store (normal, hidden and published)
-- * position mapping store
-- * indexing queue
-- * exports map
garbageCollectDirtyKeys :: Action [Key]
garbageCollectDirtyKeys = do
IdeOptions{optCheckParents} <- getIdeOptions
checkParents <- liftIO optCheckParents
garbageCollectDirtyKeysOlderThan 0 checkParents
garbageCollectDirtyKeysOlderThan :: Int -> CheckParents -> Action [Key]
garbageCollectDirtyKeysOlderThan maxAge checkParents = otTracedGarbageCollection "dirty GC" $ do
dirtySet <- getDirtySet
garbageCollectKeys "dirty GC" maxAge checkParents dirtySet
garbageCollectKeys :: String -> Int -> CheckParents -> [(Key, Int)] -> Action [Key]
garbageCollectKeys label maxAge checkParents agedKeys = do
start <- liftIO offsetTime
ShakeExtras{state, dirtyKeys, lspEnv, shakeRecorder, ideTesting} <- getShakeExtras
(n::Int, garbage) <- liftIO $
foldM (removeDirtyKey dirtyKeys state) (0,[]) agedKeys
t <- liftIO start
when (n>0) $ liftIO $ do
logWith shakeRecorder Debug $ LogShakeGarbageCollection (T.pack label) n t
when (coerce ideTesting) $ liftIO $ mRunLspT lspEnv $
LSP.sendNotification (SMethod_CustomMethod (Proxy @"ghcide/GC"))
(toJSON $ mapMaybe (fmap showKey . fromKeyType) garbage)
return garbage
where
showKey = show . Q
removeDirtyKey dk values st@(!counter, keys) (k, age)
| age > maxAge
, Just (kt,_) <- fromKeyType k
, not(kt `HSet.member` preservedKeys checkParents)
= atomicallyNamed "GC" $ do
gotIt <- STM.focus (Focus.member <* Focus.delete) k values
when gotIt $
modifyTVar' dk (insertKeySet k)
return $ if gotIt then (counter+1, k:keys) else st
| otherwise = pure st
countRelevantKeys :: CheckParents -> [Key] -> Int
countRelevantKeys checkParents =
Prelude.length . filter (maybe False (not . (`HSet.member` preservedKeys checkParents) . fst) . fromKeyType)
preservedKeys :: CheckParents -> HashSet TypeRep
preservedKeys checkParents = HSet.fromList $
-- always preserved
[ typeOf GetFileExists
, typeOf GetModificationTime
, typeOf IsFileOfInterest
, typeOf GhcSessionIO
, typeOf GetClientSettings
, typeOf AddWatchedFile
, typeOf GetKnownTargets
]
++ concat
-- preserved if CheckParents is enabled since we need to rebuild the ModuleGraph