-
Notifications
You must be signed in to change notification settings - Fork 201
/
Shake.hs
389 lines (317 loc) · 14.2 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
-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstraintKinds #-}
-- | A Shake implementation of the compiler service.
module Development.IDE.State.Shake(
IdeState,
IdeRule, IdeResult,
shakeOpen, shakeShut,
shakeRun,
shakeProfile,
useStale,
use, uses,
use_, uses_,
define, defineEarlyCutoff,
getAllDiagnostics, unsafeClearAllDiagnostics,
reportSeriousError, reportSeriousErrorDie,
IsIdeGlobal, addIdeGlobal, getIdeGlobalState, getIdeGlobalAction,
garbageCollect,
setPriority,
sendEvent,
shakeLogDebug,
) where
import Development.Shake
import Development.Shake.Database
import Development.Shake.Classes
import Development.Shake.Rule
import qualified Data.HashMap.Strict as Map
import qualified Data.ByteString.Char8 as BS
import Data.Dynamic
import Data.Maybe
import Data.Either
import Data.List.Extra
import qualified Data.Text as T
import qualified Development.IDE.Logger as Logger
import Development.IDE.Types.LSP
import Development.IDE.Types.Diagnostics
import Control.Concurrent.Extra
import Control.Exception
import Control.DeepSeq
import System.Time.Extra
import Data.Typeable
import Data.Tuple.Extra
import System.FilePath
import qualified Development.Shake as Shake
import Control.Monad.Extra
import qualified Data.Set as Set
import Data.Time
import System.IO.Unsafe
import Numeric.Extra
-- information we stash inside the shakeExtra field
data ShakeExtras = ShakeExtras
{eventer :: Event -> IO ()
,logger :: Logger.Handle IO
,globals :: Var (Map.HashMap TypeRep Dynamic)
,state :: Var Values
}
getShakeExtras :: Action ShakeExtras
getShakeExtras = do
Just x <- getShakeExtra @ShakeExtras
return x
getShakeExtrasRules :: Rules ShakeExtras
getShakeExtrasRules = do
-- We'd like to use binding, but no MonadFail Rules https://github.com/ndmitchell/shake/issues/643
x <- getShakeExtraRules @ShakeExtras
return $ fromMaybe (error "Can't find ShakeExtras, serious error") x
class Typeable a => IsIdeGlobal a where
addIdeGlobal :: IsIdeGlobal a => a -> Rules ()
addIdeGlobal x@(typeOf -> ty) = do
ShakeExtras{globals} <- getShakeExtrasRules
liftIO $ modifyVar_ globals $ \mp -> case Map.lookup ty mp of
Just _ -> error $ "Can't addIdeGlobal twice on the same type, got " ++ show ty
Nothing -> return $! Map.insert ty (toDyn x) mp
getIdeGlobalExtras :: forall a . IsIdeGlobal a => ShakeExtras -> IO a
getIdeGlobalExtras ShakeExtras{globals} = do
Just x <- Map.lookup (typeRep (Proxy :: Proxy a)) <$> readVar globals
return $ fromDyn x $ error "Serious error, corrupt globals"
getIdeGlobalAction :: forall a . IsIdeGlobal a => Action a
getIdeGlobalAction = liftIO . getIdeGlobalExtras =<< getShakeExtras
getIdeGlobalState :: forall a . IsIdeGlobal a => IdeState -> IO a
getIdeGlobalState = getIdeGlobalExtras . shakeExtras
-- | The state of the all values - nested so you can easily find all errors at a given file.
type Values =
Map.HashMap FilePath
(Map.HashMap Key
(IdeResult Dynamic)
)
-- | Key type
data Key = forall k . (Typeable k, Hashable k, Eq k, Show k) => Key k
instance Show Key where
show (Key k) = show k
instance Eq Key where
Key k1 == Key k2 | Just k2' <- cast k2 = k1 == k2'
| otherwise = False
instance Hashable Key where
hashWithSalt salt (Key key) = hashWithSalt salt key
-- | The result of an IDE operation. Warnings and errors are in the Diagnostic,
-- and a value is in the Maybe. For operations that throw an error you
-- expect a non-empty list of diagnostics, at least one of which is an error,
-- and a Nothing. For operations that succeed you expect perhaps some warnings
-- and a Just. For operations that depend on other failing operations you may
-- get empty diagnostics and a Nothing, to indicate this phase throws no fresh
-- errors but still failed.
--
-- A rule on a file should only return diagnostics for that given file. It should
-- not propagate diagnostic errors through multiple phases.
type IdeResult v = ([Diagnostic], Maybe v)
type IdeRule k v =
( Shake.RuleResult k ~ IdeResult v
, Shake.ShakeValue k
, Show v
, Typeable v
, NFData v
)
-- | A Shake database plus persistent store. Can be thought of as storing
-- mappings from @(FilePath, k)@ to @RuleResult k@.
data IdeState = IdeState
{shakeDb :: ShakeDatabase
,shakeAbort :: Var (IO ()) -- close whoever was running last
,shakeClose :: IO ()
,shakeExtras :: ShakeExtras
}
profileDir :: Maybe FilePath
profileDir = Nothing -- set to Just the directory you want profile reports to appear in
-- This is debugging code that generates a series of profiles, if the Boolean is true
shakeRunDatabaseProfile :: ShakeDatabase -> [Action a] -> IO [a]
shakeRunDatabaseProfile shakeDb acts = do
(time, (res,_)) <- duration $ shakeRunDatabase shakeDb acts
whenJust profileDir $ \dir -> do
count <- modifyVar profileCounter $ \x -> let y = x+1 in return (y,y)
let file = "ide-" ++ profileStartTime ++ "-" ++ takeEnd 5 ("0000" ++ show count) ++ "-" ++ showDP 2 time <.> "html"
shakeProfileDatabase shakeDb $ dir </> file
return res
where
{-# NOINLINE profileStartTime #-}
profileStartTime :: String
profileStartTime = unsafePerformIO $ formatTime defaultTimeLocale "%Y%m%d-%H%M%S" <$> getCurrentTime
{-# NOINLINE profileCounter #-}
profileCounter :: Var Int
profileCounter = unsafePerformIO $ newVar 0
setValues :: IdeRule k v
=> Var Values
-> k
-> FilePath
-> IdeResult v
-> IO (Maybe [Diagnostic], [Diagnostic]) -- ^ (before, after)
setValues state key file val = modifyVar state $ \inVal -> do
let k = Key key
outVal = Map.insertWith Map.union file (Map.singleton k $ fmap toDyn <$> val) inVal
f = concatMap fst . Map.elems
return (outVal, (f <$> Map.lookup file inVal, f $ outVal Map.! file))
getValues :: forall k v. IdeRule k v => Var Values -> k -> FilePath -> IO (Maybe (IdeResult v))
getValues state key file = flip fmap (readVar state) $ \vs -> do
f <- Map.lookup file vs
k <- Map.lookup (Key key) f
pure $ fmap (fromJust . fromDynamic) <$> k
-- | Open a 'IdeState', should be shut using 'shakeShut'.
shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
-> Logger.Handle IO
-> ShakeOptions
-> Rules ()
-> IO IdeState
shakeOpen diags shakeLogger opts rules = do
shakeExtras <- ShakeExtras diags shakeLogger <$> newVar Map.empty <*> newVar Map.empty
(shakeDb, shakeClose) <- shakeOpenDatabase opts{shakeExtra = addShakeExtra shakeExtras $ shakeExtra opts} rules
shakeAbort <- newVar $ return ()
shakeDb <- shakeDb
return IdeState{..}
shakeProfile :: IdeState -> FilePath -> IO ()
shakeProfile IdeState{..} = shakeProfileDatabase shakeDb
shakeShut :: IdeState -> IO ()
shakeShut = shakeClose
-- | Spawn immediately, add an action to collect the results syncronously.
-- If you are already inside a call to shakeRun that will be aborted with an exception.
shakeRun :: IdeState -> [Action a] -> IO (IO [a])
-- FIXME: If there is already a shakeRun queued up and waiting to send me a kill, I should probably
-- not even start, which would make issues with async exceptions less problematic.
shakeRun IdeState{shakeExtras=ShakeExtras{..}, ..} acts = modifyVar shakeAbort $ \stop -> do
(stopTime,_) <- duration stop
Logger.logInfo logger $ T.pack $ "Starting shakeRun (aborting the previous one took " ++ showDuration stopTime ++ ")"
bar <- newBarrier
start <- offsetTime
thread <- forkFinally (shakeRunDatabaseProfile shakeDb acts) $ \res -> do
signalBarrier bar res
runTime <- start
Logger.logInfo logger $ T.pack $
"Finishing shakeRun (took " ++ showDuration runTime ++ ", " ++ (if isLeft res then "exception" else "completed") ++ ")"
-- important: we send an async exception to the thread, then wait for it to die, before continuing
return (do killThread thread; void $ waitBarrier bar, either throwIO return =<< waitBarrier bar)
-- | Use the last stale value, if it's ever been computed.
useStale
:: IdeRule k v
=> IdeState -> k -> FilePath -> IO (Maybe v)
useStale IdeState{shakeExtras=ShakeExtras{state}} k fp = do
v <- getValues state k fp
return $ maybe Nothing snd v
getAllDiagnostics :: IdeState -> IO [Diagnostic]
getAllDiagnostics IdeState{shakeExtras = ShakeExtras{state}} = do
val <- readVar state
return $ concatMap (concatMap fst . Map.elems) $ Map.elems val
-- | FIXME: This function is temporary! Only required because the files of interest doesn't work
unsafeClearAllDiagnostics :: IdeState -> IO ()
unsafeClearAllDiagnostics IdeState{shakeExtras = ShakeExtras{state}} = modifyVar_ state $
return . Map.map (Map.map (\(_, x) -> ([], x)))
-- | Clear the results for all files that do not match the given predicate.
garbageCollect :: (FilePath -> Bool) -> Action ()
garbageCollect keep = do
ShakeExtras{state} <- getShakeExtras
liftIO $ modifyVar_ state $ return . Map.filterWithKey (\file _ -> keep file)
define
:: IdeRule k v
=> (k -> FilePath -> Action (IdeResult v)) -> Rules ()
define op = defineEarlyCutoff $ \k v -> (Nothing,) <$> op k v
use :: IdeRule k v
=> k -> FilePath -> Action (IdeResult v)
use key file = head <$> uses key [file]
use_ :: IdeRule k v => k -> FilePath -> Action v
use_ key file = head <$> uses_ key [file]
uses_ :: IdeRule k v => k -> [FilePath] -> Action [v]
uses_ key files = do
res <- uses key files
case mapM snd res of
Nothing -> liftIO $ throwIO BadDependency
Just v -> return v
reportSeriousError :: String -> Action ()
reportSeriousError t = do
ShakeExtras{logger} <- getShakeExtras
liftIO $ Logger.logInfo logger $ T.pack t
reportSeriousErrorDie :: String -> Action a
reportSeriousErrorDie t = do
ShakeExtras{logger} <- getShakeExtras
liftIO $ Logger.logInfo logger $ T.pack t
fail t
-- | When we depend on something that reported an error, and we fail as a direct result, throw BadDependency
-- which short-circuits the rest of the action
data BadDependency = BadDependency deriving Show
instance Exception BadDependency
isBadDependency :: SomeException -> Bool
isBadDependency x
| Just (x :: ShakeException) <- fromException x = isBadDependency $ shakeExceptionInner x
| Just (_ :: BadDependency) <- fromException x = True
| otherwise = False
newtype Q k = Q (k, FilePath)
deriving (Eq,Hashable,Binary,NFData)
instance Show k => Show (Q k) where
show (Q (k, file)) = show k ++ "; " ++ file
-- | Invariant: the 'v' must be in normal form (fully evaluated).
-- Otherwise we keep repeatedly 'rnf'ing values taken from the Shake database
data A v = A v (Maybe BS.ByteString)
deriving Show
instance NFData (A v) where rnf (A v x) = v `seq` rnf x
type instance RuleResult (Q k) = A (RuleResult k)
-- | Compute the value
uses :: IdeRule k v
=> k -> [FilePath] -> Action [IdeResult v]
uses key files = map (\(A value _) -> value) <$> apply (map (Q . (key,)) files)
defineEarlyCutoff
:: IdeRule k v
=> (k -> FilePath -> Action (Maybe BS.ByteString, IdeResult v))
-> Rules ()
defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old mode -> do
ShakeExtras{state} <- getShakeExtras
val <- case old of
Just old | mode == RunDependenciesSame -> do
v <- liftIO $ getValues state key file
case v of
Just v -> return $ Just $ RunResult ChangedNothing old $ A v (unwrap old)
_ -> return Nothing
_ -> return Nothing
case val of
Just res -> return res
Nothing -> do
(bs, res) <- actionCatch
(do v <- op key file; liftIO $ evaluate $ force v) $
\(e :: SomeException) -> pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
res <- return $ first (map $ fixDiagnostic file) res
let badErrors = filter (\d -> null file || dRange d == noRange) $ fst res
when (badErrors /= []) $
reportSeriousError $ "Bad errors found for " ++ show (key, file) ++ " got " ++ show badErrors
(before, after) <- liftIO $ setValues state key file res
updateFileDiagnostics file before after
let eq = case (bs, fmap unwrap old) of
(Just a, Just (Just b)) -> a == b
_ -> False
return $ RunResult
(if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
(wrap bs)
$ A res bs
where
wrap = maybe BS.empty (BS.cons '_')
unwrap x = if BS.null x then Nothing else Just $ BS.tail x
-- | If any diagnostic has the wrong filename, generate a new diagnostic with the right file name
fixDiagnostic :: FilePath -> Diagnostic -> Diagnostic
fixDiagnostic x d
| dFilePath d == x = d
| otherwise = d{dFilePath = x, dRange = noRange, dMessage = T.pack ("Originally reported at " ++ dFilePath d ++ "\n") <> dMessage d}
updateFileDiagnostics ::
FilePath
-> Maybe [Diagnostic] -- ^ previous results for this file
-> [Diagnostic] -- ^ current results
-> Action ()
updateFileDiagnostics afp previousAll currentAll = do
let filt = Set.fromList . filter (\x -> dFilePath x == afp)
previous = fmap filt previousAll
current = filt currentAll
when (Just current /= previous) $
sendEvent $ EventFileDiagnostics $ FileDiagnostics afp $ Set.toList current
setPriority :: (Enum a) => a -> Action ()
setPriority p =
deprioritize (fromIntegral . negate $ fromEnum p)
sendEvent :: Event -> Action ()
sendEvent e = do
ShakeExtras{eventer} <- getShakeExtras
liftIO $ eventer e
shakeLogDebug :: IdeState -> T.Text -> IO ()
shakeLogDebug IdeState{shakeExtras=ShakeExtras{logger}} msg = Logger.logDebug logger msg