Skip to content

Commit b4d3c49

Browse files
committed
wait for shake restart only if needed
1 parent f9e1023 commit b4d3c49

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import Ide.Logger (Pretty (pretty),
6868
vcat, viaShow, (<+>))
6969
import Ide.Types (Config,
7070
SessionLoadingPreferenceConfig (..),
71+
ShouldWait (..),
7172
sessionLoading)
7273
import Language.LSP.Protocol.Message
7374
import Language.LSP.Server
@@ -625,7 +626,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
625626
, ..
626627
}
627628
sessionShake = SessionShake
628-
{ restartSession = restartShakeSession extras
629+
{ restartSession = restartShakeSession extras ShouldWait
629630
, invalidateCache = invalidateShakeCache
630631
, enqueueActions = shakeEnqueue extras
631632
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Development.IDE.Core.FileStore(
2222
registerFileWatches,
2323
shareFilePath,
2424
Log(..),
25+
setSomethingModifiedWait,
2526
) where
2627

2728
import Control.Concurrent.STM.Stats (STM, atomically)
@@ -279,7 +280,7 @@ setFileModified recorder vfs state saved nfp actionBefore = do
279280
AlwaysCheck -> True
280281
CheckOnSave -> saved
281282
_ -> False
282-
restartShakeSession (shakeExtras state) vfs (fromNormalizedFilePath nfp ++ " (modified)") ([mkDelayedAction "ParentTC" L.Debug (typecheckParentsAction recorder nfp) | checkParents]) $ do
283+
restartShakeSession (shakeExtras state) ShouldNotWait vfs (fromNormalizedFilePath nfp ++ " (modified)") ([mkDelayedAction "ParentTC" L.Debug (typecheckParentsAction recorder nfp) | checkParents]) $ do
283284
keys<-actionBefore
284285
return (toKey GetModificationTime nfp:keys)
285286

@@ -299,11 +300,16 @@ typecheckParentsAction recorder nfp = do
299300
-- | Note that some keys have been modified and restart the session
300301
-- Only valid if the virtual file system was initialised by LSP, as that
301302
-- independently tracks which files are modified.
302-
setSomethingModified :: VFSModified -> IdeState -> String -> IO [Key] -> IO ()
303-
setSomethingModified vfs state reason actionBetweenSession = do
303+
setSomethingModified' :: ShouldWait -> VFSModified -> IdeState -> String -> IO [Key] -> IO ()
304+
setSomethingModified' shouldWait vfs state reason actionBetweenSession = do
304305
-- Update database to remove any files that might have been renamed/deleted
305306
atomically $ writeTaskQueue (indexQueue $ hiedbWriter $ shakeExtras state) (\withHieDb -> withHieDb deleteMissingRealFiles)
306-
void $ restartShakeSession (shakeExtras state) vfs reason [] actionBetweenSession
307+
void $ restartShakeSession (shakeExtras state) shouldWait vfs reason [] actionBetweenSession
308+
setSomethingModified :: VFSModified -> IdeState -> String -> IO [Key] -> IO ()
309+
setSomethingModified vfs state reason actionBetweenSession = setSomethingModified' ShouldNotWait vfs state reason actionBetweenSession
310+
311+
setSomethingModifiedWait :: VFSModified -> IdeState -> String -> IO [Key] -> IO ()
312+
setSomethingModifiedWait vfs state reason actionBetweenSession = setSomethingModified' ShouldWait vfs state reason actionBetweenSession
307313

308314
registerFileWatches :: [String] -> LSP.LspT Config IO Bool
309315
registerFileWatches globs = do

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ import System.Time.Extra
190190
import UnliftIO (MonadUnliftIO (withRunInIO),
191191
newIORef, readIORef)
192192

193+
#if !MIN_VERSION_ghc(9,9,0)
194+
import Data.Foldable (foldl')
195+
#endif
193196

194197

195198
data Log
@@ -341,7 +344,8 @@ data ShakeExtras = ShakeExtras
341344
,ideTesting :: IdeTesting
342345
-- ^ Whether to enable additional lsp messages used by the test suite for checking invariants
343346
,restartShakeSession
344-
:: VFSModified
347+
:: ShouldWait
348+
-> VFSModified
345349
-> String
346350
-> [DelayedAction ()]
347351
-> IO [Key]
@@ -886,15 +890,21 @@ instance Semigroup ShakeRestartArgs where
886890
-- | Restart the current 'ShakeSession' with the given system actions.
887891
-- Any actions running in the current session will be aborted,
888892
-- but actions added via 'shakeEnqueue' will be requeued.
889-
shakeRestart :: ShakeControlQueue -> VFSModified -> String -> [DelayedAction ()] -> IO [Key] -> IO ()
890-
shakeRestart rts vfs reason acts ioActionBetweenShakeSession = do
891-
waitMVar <- newEmptyMVar
892-
-- submit at the head of the queue,
893-
-- prefer restart request over any pending actions
894-
void $ submitWorkAtHead rts $ Left $
895-
toDyn $ ShakeRestartArgs vfs reason acts ioActionBetweenShakeSession rts 1 [waitMVar]
896-
-- Wait until the restart is done
897-
takeMVar waitMVar
893+
shakeRestart :: ShakeControlQueue -> ShouldWait -> VFSModified -> String -> [DelayedAction ()] -> IO [Key] -> IO ()
894+
shakeRestart rts b vfs reason acts ioActionBetweenShakeSession = case b of
895+
ShouldWait ->
896+
do
897+
waitMVar <- newEmptyMVar
898+
-- submit at the head of the queue,
899+
-- prefer restart request over any pending actions
900+
void $ submitWorkAtHead rts $ Left $
901+
toDyn $ ShakeRestartArgs vfs reason acts ioActionBetweenShakeSession rts 1 [waitMVar]
902+
-- Wait until the restart is done
903+
takeMVar waitMVar
904+
ShouldNotWait ->
905+
void $ submitWorkAtHead rts $ Left $
906+
toDyn $ ShakeRestartArgs vfs reason acts ioActionBetweenShakeSession rts 1 []
907+
898908

899909
dynShakeRestart :: Dynamic -> ShakeRestartArgs
900910
dynShakeRestart dy = case fromDynamic dy of

hls-plugin-api/src/Ide/Types.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Ide.Types
4242
, installSigUsr1Handler
4343
, lookupCommandProvider
4444
, ResolveFunction
45-
, mkResolveHandler
45+
, mkResolveHandler, ShouldWait(..)
4646
)
4747
where
4848

@@ -1302,3 +1302,6 @@ installSigUsr1Handler h = void $ installHandler sigUSR1 (Catch h) Nothing
13021302
resolve handlers for the same method, than our assumptions that we never have
13031303
two responses break, and behavior is undefined.
13041304
-}
1305+
1306+
data ShouldWait = ShouldWait | ShouldNotWait
1307+
deriving Eq

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Then we restart the shake session, so that changes to our virtual files are actu
179179
-}
180180
restartCabalShakeSession :: ShakeExtras -> VFS.VFS -> NormalizedFilePath -> String -> IO [Key] -> IO ()
181181
restartCabalShakeSession shakeExtras vfs file actionMsg actionBetweenSession = do
182-
restartShakeSession shakeExtras (VFSModified vfs) (fromNormalizedFilePath file ++ " " ++ actionMsg) [] $ do
182+
restartShakeSession shakeExtras ShouldNotWait (VFSModified vfs) (fromNormalizedFilePath file ++ " " ++ actionMsg) [] $ do
183183
keys <- actionBetweenSession
184184
return (toKey GetModificationTime file:keys)
185185

@@ -188,7 +188,7 @@ restartCabalShakeSession shakeExtras vfs file actionMsg actionBetweenSession = d
188188
-- rule to get re-run if the file changes on disk.
189189
restartCabalShakeSessionPhysical :: ShakeExtras -> VFS.VFS -> NormalizedFilePath -> String -> IO [Key] -> IO ()
190190
restartCabalShakeSessionPhysical shakeExtras vfs file actionMsg actionBetweenSession = do
191-
restartShakeSession shakeExtras (VFSModified vfs) (fromNormalizedFilePath file ++ " " ++ actionMsg) [] $ do
191+
restartShakeSession shakeExtras ShouldNotWait (VFSModified vfs) (fromNormalizedFilePath file ++ " " ++ actionMsg) [] $ do
192192
keys <- actionBetweenSession
193193
return (toKey GetModificationTime file:toKey GetPhysicalModificationTime file:keys)
194194

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Handlers.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import Data.String (IsString)
4141
import Data.Text (Text)
4242
import qualified Data.Text as T
4343
import qualified Data.Text.Utf16.Rope.Mixed as Rope
44-
import Development.IDE.Core.FileStore (getUriContents, setSomethingModified)
44+
import Development.IDE.Core.FileStore (getUriContents, setSomethingModifiedWait)
4545
import Development.IDE.Core.Rules (IdeState,
4646
runAction)
4747
import Development.IDE.Core.Shake (use_, uses_, VFSModified (VFSUnmodified), useWithSeparateFingerprintRule_)
@@ -214,11 +214,11 @@ runEvalCmd recorder plId st mtoken EvalParams{..} =
214214

215215
-- enable codegen for the module which we need to evaluate.
216216
final_hscEnv <- liftIO $ bracket_
217-
(setSomethingModified VFSUnmodified st "Eval" $ do
217+
(setSomethingModifiedWait VFSUnmodified st "Eval" $ do
218218
queueForEvaluation st nfp
219219
return [toKey IsEvaluating nfp]
220220
)
221-
(setSomethingModified VFSUnmodified st "Eval" $ do
221+
(setSomethingModifiedWait VFSUnmodified st "Eval" $ do
222222
unqueueForEvaluation st nfp
223223
return [toKey IsEvaluating nfp]
224224
)

0 commit comments

Comments
 (0)