-
-
Notifications
You must be signed in to change notification settings - Fork 389
Lockless diagnostics #2434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lockless diagnostics #2434
Changes from all commits
368e13c
5fe1dd0
4b6e762
7ebbaba
4a17518
43220a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,9 +191,9 @@ data ShakeExtras = ShakeExtras | |
,logger :: Logger | ||
,globals :: Var (HMap.HashMap TypeRep Dynamic) | ||
,state :: Values | ||
,diagnostics :: Var DiagnosticStore | ||
,hiddenDiagnostics :: Var DiagnosticStore | ||
,publishedDiagnostics :: Var (HMap.HashMap NormalizedUri [Diagnostic]) | ||
,diagnostics :: STMDiagnosticStore | ||
pepeiborra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
,hiddenDiagnostics :: STMDiagnosticStore | ||
,publishedDiagnostics :: STM.Map NormalizedUri [Diagnostic] | ||
-- ^ This represents the set of diagnostics that we have published. | ||
-- Due to debouncing not every change might get published. | ||
,positionMapping :: Var (HMap.HashMap NormalizedUri (Map TextDocumentVersion (PositionDelta, PositionMapping))) | ||
|
@@ -437,8 +437,8 @@ deleteValue | |
=> ShakeExtras | ||
-> k | ||
-> NormalizedFilePath | ||
-> IO () | ||
deleteValue ShakeExtras{dirtyKeys, state} key file = atomically $ do | ||
-> STM () | ||
deleteValue ShakeExtras{dirtyKeys, state} key file = do | ||
STM.delete (toKey key file) state | ||
modifyTVar' dirtyKeys $ HSet.insert (toKey key file) | ||
|
||
|
@@ -447,10 +447,11 @@ recordDirtyKeys | |
=> ShakeExtras | ||
-> k | ||
-> [NormalizedFilePath] | ||
-> IO () | ||
recordDirtyKeys ShakeExtras{dirtyKeys} key file = withEventTrace "recordDirtyKeys" $ \addEvent -> do | ||
atomically $ modifyTVar' dirtyKeys $ \x -> foldl' (flip HSet.insert) x (toKey key <$> file) | ||
addEvent (fromString $ "dirty " <> show key) (fromString $ unlines $ map fromNormalizedFilePath file) | ||
-> STM (IO ()) | ||
recordDirtyKeys ShakeExtras{dirtyKeys} key file = do | ||
modifyTVar' dirtyKeys $ \x -> foldl' (flip HSet.insert) x (toKey key <$> file) | ||
return $ withEventTrace "recordDirtyKeys" $ \addEvent -> do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm interesting. I guess we may end up with this happening a bit if we push STM out further and we hit more places where we want to do tracing. I wonder if this is a pattern that appears elsewhere: this is something like a "final" IO action that should be run only after the STM action succeeds. |
||
addEvent (fromString $ "dirty " <> show key) (fromString $ unlines $ map fromNormalizedFilePath file) | ||
|
||
|
||
-- | We return Nothing if the rule has not run and Just Failed if it has failed to produce a value. | ||
|
@@ -509,9 +510,9 @@ shakeOpen lspEnv defaultConfig logger debouncer | |
shakeExtras <- do | ||
globals <- newVar HMap.empty | ||
state <- STM.newIO | ||
diagnostics <- newVar mempty | ||
hiddenDiagnostics <- newVar mempty | ||
publishedDiagnostics <- newVar mempty | ||
diagnostics <- STM.newIO | ||
hiddenDiagnostics <- STM.newIO | ||
publishedDiagnostics <- STM.newIO | ||
positionMapping <- newVar HMap.empty | ||
knownTargetsVar <- newVar $ hashed HMap.empty | ||
let restartShakeSession = shakeRestart ideState | ||
|
@@ -756,15 +757,13 @@ instantiateDelayedAction (DelayedAction _ s p a) = do | |
d' = DelayedAction (Just u) s p a' | ||
return (b, d') | ||
|
||
getDiagnostics :: IdeState -> IO [FileDiagnostic] | ||
getDiagnostics :: IdeState -> STM [FileDiagnostic] | ||
getDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do | ||
val <- readVar diagnostics | ||
return $ getAllDiagnostics val | ||
getAllDiagnostics diagnostics | ||
|
||
getHiddenDiagnostics :: IdeState -> IO [FileDiagnostic] | ||
getHiddenDiagnostics :: IdeState -> STM [FileDiagnostic] | ||
getHiddenDiagnostics IdeState{shakeExtras = ShakeExtras{hiddenDiagnostics}} = do | ||
val <- readVar hiddenDiagnostics | ||
return $ getAllDiagnostics val | ||
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: | ||
|
@@ -1154,30 +1153,26 @@ updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, p | |
let (currentShown, currentHidden) = partition ((== ShowDiag) . fst) current | ||
uri = filePathToUri' fp | ||
ver = vfsVersion =<< modTime | ||
update new store = | ||
let store' = setStageDiagnostics uri ver (T.pack $ show k) new store | ||
new' = getUriDiagnostics uri store' | ||
in (store', new') | ||
update new store = setStageDiagnostics uri ver (T.pack $ show k) new store | ||
mask_ $ do | ||
-- Mask async exceptions to ensure that updated diagnostics are always | ||
-- published. Otherwise, we might never publish certain diagnostics if | ||
-- an exception strikes between modifyVar but before | ||
-- publishDiagnosticsNotification. | ||
newDiags <- modifyVar diagnostics $ pure . update (map snd currentShown) | ||
_ <- modifyVar hiddenDiagnostics $ pure . update (map snd currentHidden) | ||
newDiags <- liftIO $ atomically $ update (map snd currentShown) diagnostics | ||
_ <- liftIO $ atomically $ update (map snd currentHidden) hiddenDiagnostics | ||
pepeiborra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let uri = filePathToUri' fp | ||
let delay = if null newDiags then 0.1 else 0 | ||
registerEvent debouncer delay uri $ do | ||
join $ mask_ $ modifyVar publishedDiagnostics $ \published -> do | ||
let lastPublish = HMap.lookupDefault [] uri published | ||
!published' = HMap.insert uri newDiags published | ||
action = when (lastPublish /= newDiags) $ case lspEnv of | ||
join $ mask_ $ do | ||
lastPublish <- atomically $ STM.focus (Focus.lookupWithDefault [] <* Focus.insert newDiags) uri publishedDiagnostics | ||
let action = when (lastPublish /= newDiags) $ case lspEnv of | ||
Nothing -> -- Print an LSP event. | ||
logInfo logger $ showDiagnosticsColored $ map (fp,ShowDiag,) newDiags | ||
Just env -> LSP.runLspT env $ | ||
LSP.sendNotification LSP.STextDocumentPublishDiagnostics $ | ||
LSP.PublishDiagnosticsParams (fromNormalizedUri uri) ver (List newDiags) | ||
return (published', action) | ||
return action | ||
|
||
newtype Priority = Priority Double | ||
|
||
|
@@ -1192,10 +1187,21 @@ actionLogger = do | |
ShakeExtras{logger} <- getShakeExtras | ||
return logger | ||
|
||
-------------------------------------------------------------------------------- | ||
type STMDiagnosticStore = STM.Map NormalizedUri StoreItem | ||
|
||
getDiagnosticsFromStore :: StoreItem -> [Diagnostic] | ||
getDiagnosticsFromStore (StoreItem _ diags) = concatMap SL.fromSortedList $ Map.elems diags | ||
|
||
updateSTMDiagnostics :: STMDiagnosticStore | ||
-> NormalizedUri -> TextDocumentVersion -> DiagnosticsBySource | ||
-> STM [LSP.Diagnostic] | ||
updateSTMDiagnostics store uri mv newDiagsBySource = | ||
getDiagnosticsFromStore . fromJust <$> STM.focus (Focus.alter update *> Focus.lookup) uri store | ||
where | ||
update (Just(StoreItem mvs dbs)) | ||
| mvs == mv = Just (StoreItem mv (newDiagsBySource <> dbs)) | ||
update _ = Just (StoreItem mv newDiagsBySource) | ||
|
||
-- | Sets the diagnostics for a file and compilation step | ||
-- if you want to clear the diagnostics call this with an empty list | ||
|
@@ -1204,25 +1210,17 @@ setStageDiagnostics | |
-> TextDocumentVersion -- ^ the time that the file these diagnostics originate from was last edited | ||
-> T.Text | ||
-> [LSP.Diagnostic] | ||
-> DiagnosticStore | ||
-> DiagnosticStore | ||
setStageDiagnostics uri ver stage diags ds = updateDiagnostics ds uri ver updatedDiags | ||
-> STMDiagnosticStore | ||
-> STM [LSP.Diagnostic] | ||
setStageDiagnostics uri ver stage diags ds = updateSTMDiagnostics ds uri ver updatedDiags | ||
where | ||
updatedDiags = Map.singleton (Just stage) (SL.toSortedList diags) | ||
|
||
getAllDiagnostics :: | ||
DiagnosticStore -> | ||
[FileDiagnostic] | ||
STMDiagnosticStore -> | ||
STM [FileDiagnostic] | ||
getAllDiagnostics = | ||
concatMap (\(k,v) -> map (fromUri k,ShowDiag,) $ getDiagnosticsFromStore v) . HMap.toList | ||
|
||
getUriDiagnostics :: | ||
NormalizedUri -> | ||
DiagnosticStore -> | ||
[LSP.Diagnostic] | ||
getUriDiagnostics uri ds = | ||
maybe [] getDiagnosticsFromStore $ | ||
HMap.lookup uri ds | ||
fmap (concatMap (\(k,v) -> map (fromUri k,ShowDiag,) $ getDiagnosticsFromStore v)) . ListT.toList . STM.listT | ||
|
||
updatePositionMapping :: IdeState -> VersionedTextDocumentIdentifier -> List TextDocumentContentChangeEvent -> IO () | ||
updatePositionMapping IdeState{shakeExtras = ShakeExtras{positionMapping}} VersionedTextDocumentIdentifier{..} (List changes) = do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ library | |
, lens | ||
, lsp | ||
, regex-tdfa | ||
, stm | ||
, temporary | ||
, text | ||
, transformers | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed, this changes the type of
invalidateShakeCache
to:: IO (IO ())
. I suppose that is unintentional?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, either we need a
join
or have to consume the action produced at: https://github.com/haskell/haskell-language-server/pull/2434/files#diff-d7140bbfca1b7aaafd8c82d1d6aa21a3f941eb27ba8f08e4d9fb13766b21b418L393