-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathDiagnostics.scala
More file actions
476 lines (430 loc) Β· 14.9 KB
/
Diagnostics.scala
File metadata and controls
476 lines (430 loc) Β· 14.9 KB
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
package scala.meta.internal.metals
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicReference
import java.{util => ju}
import scala.collection.concurrent.TrieMap
import scala.collection.mutable
import scala.util.Try
import scala.meta.inputs.Input
import scala.meta.internal.metals.JsonParser._
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.PositionSyntax._
import scala.meta.internal.parsing.TokenEditDistance
import scala.meta.io.AbsolutePath
import ch.epfl.scala.bsp4j
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.{lsp4j => l}
private final case class CompilationStatus(
code: bsp4j.StatusCode,
errors: Int,
)
case class DiagnosticWithOrigin(diagnostic: Diagnostic, originId: String)
/**
* Converts diagnostics from the build server and Scalameta parser into LSP diagnostics.
*
* BSP diagnostics have different semantics from LSP diagnostics with regards to how they
* are published. BSP diagnostics can be accumulated when `reset=false`, meaning the client
* (Metals) is responsible for aggregating multiple `build/publishDiagnostics` notifications
* into a single `textDocument/publishDiagnostics` notification.
*
* Another challenge is to consolidate syntax errors on every keystroke with type errors
* on batch compile because positions get stale in batch compile errors as you type new
* syntax errors. To solve this problem we use token edit distance the same way we support
* goto definition in stale buffers.
*/
final class Diagnostics(
buffers: Buffers,
languageClient: LanguageClient,
statistics: StatisticsConfig,
workspace: Option[AbsolutePath],
scalaVersionSelector: ScalaVersionSelector,
buildTargets: BuildTargets,
downstreamTargets: PreviouslyCompiledDownsteamTargets,
config: MetalsServerConfig,
clientConfig: ClientConfiguration,
) {
private val diagnostics =
TrieMap.empty[AbsolutePath, ju.Queue[DiagnosticWithOrigin]]
private val syntaxError =
TrieMap.empty[AbsolutePath, Diagnostic]
private val scalafixDiagnostics =
TrieMap.empty[AbsolutePath, List[Diagnostic]]
private val snapshots =
TrieMap.empty[AbsolutePath, Input.VirtualFile]
private val lastPublished =
new AtomicReference[AbsolutePath]()
private val diagnosticsBuffer =
new ConcurrentLinkedQueue[AbsolutePath]()
private val compileTimer =
TrieMap.empty[BuildTargetIdentifier, Timer]
private val compilationStatus =
TrieMap.empty[BuildTargetIdentifier, CompilationStatus]
def forFile(path: AbsolutePath): Seq[Diagnostic] = {
diagnostics
.getOrElse(path, new ConcurrentLinkedQueue[DiagnosticWithOrigin]())
.asScala
.map(_.diagnostic)
.toList
}
def allDiagnostics: Seq[(AbsolutePath, Diagnostic)] =
diagnostics.toList.flatMap { case (path, queue) =>
queue.asScala.map(diag => (path, diag.diagnostic))
}
def reset(): Unit = {
val keys = diagnostics.keys ++ scalafixDiagnostics.keys
diagnostics.clear()
scalafixDiagnostics.clear()
keys.foreach { key => publishDiagnostics(key) }
}
def reset(paths: Seq[AbsolutePath]): Unit =
for (path <- paths if diagnostics.contains(path)) {
diagnostics.remove(path)
publishDiagnostics(path)
}
def onStartCompileBuildTarget(target: BuildTargetIdentifier): Unit = {
if (statistics.isDiagnostics) {
compileTimer(target) = new Timer(Time.system)
}
}
def onFinishCompileBuildTarget(
report: bsp4j.CompileReport,
statusCode: bsp4j.StatusCode,
originId: String,
): Unit = {
val target = report.getTarget()
// if we use best effort compilation downstream targets
// should get recompiled even if compilation fails
def shouldUnpublishForDownstreamTargets =
!(buildTargets
.scalaTarget(target)
.exists(_.isBestEffort) && config.enableBestEffort)
if (statusCode.isError && shouldUnpublishForDownstreamTargets) {
removeInverseDependenciesDiagnostics(target)
} else {
downstreamTargets.remove(target)
}
// Bazel doesn't clean diagnostics for paths with no errors, so instead we remove everything
// from previous compilations.
val isBazel = buildTargets.buildServerOf(target).exists(_.isBazel)
if (isBazel) {
diagnostics
.filter { case (path, _) =>
buildTargets.inverseSources(path).exists(target => target == target)
}
.foreach { case (path, queue) =>
val updatedQueue = queue.asScala.filter {
case DiagnosticWithOrigin(_, diagOriginId) =>
diagOriginId == originId
}
if (updatedQueue.isEmpty) {
reset(Seq(path))
}
diagnostics.remove(path)
}
}
publishDiagnosticsBuffer()
compileTimer.remove(target)
val status = CompilationStatus(statusCode, report.getErrors())
compilationStatus.update(target, status)
}
def onSyntaxError(path: AbsolutePath, diags: List[Diagnostic]): Unit = {
diags.headOption match {
case Some(diagnostic) if !workspace.exists(path.isInReadonlyDirectory) =>
syntaxError(path) = diagnostic
publishDiagnostics(path)
case _ =>
onClose(path)
}
}
def onClose(path: AbsolutePath): Unit = {
val diags = if (path.isWorksheet) {
diagnostics.remove(path).toList.flatMap(_.asScala) ++
syntaxError.remove(path)
} else syntaxError.remove(path).toList
val hadScalafixDiags = scalafixDiagnostics.remove(path).isDefined
if (diags.nonEmpty || hadScalafixDiags) {
publishDiagnostics(path)
}
}
def didDelete(path: AbsolutePath): Unit = {
diagnostics.remove(path)
syntaxError.remove(path)
scalafixDiagnostics.remove(path)
languageClient.publishDiagnostics(
new PublishDiagnosticsParams(
path.toURI.toString(),
ju.Collections.emptyList(),
)
)
}
def didChange(path: AbsolutePath): Unit = {
publishDiagnostics(path)
}
def onBuildPublishDiagnostics(
params: bsp4j.PublishDiagnosticsParams
): Unit = {
val diagnostics =
for {
path <- Try(params.getTextDocument.getUri.toAbsolutePath).toOption
shouldShowExplainDiagnostic = buildTargets
.inverseSources(path)
.flatMap(buildTargets.scalaTarget)
.exists(target => target.supportExplainDiagnostic)
isVirtualDocumentSupported = clientConfig
.isVirtualDocumentSupported() && shouldShowExplainDiagnostic
diagnostics = params
.getDiagnostics()
.asScala
.map(
_.toLsp(
path,
isVirtualDocumentSupported,
)
)
.toSeq
if (path.isFile)
_ = onPublishDiagnostics(
path,
diagnostics,
params.getReset(),
params.getOriginId(),
)
} yield diagnostics
diagnostics.getOrElse {
scribe.warn(
s"Invalid text document uri received from build server: ${params.getTextDocument.getUri}"
)
}
}
def onPublishDiagnostics(
path: AbsolutePath,
diagnostics: Seq[Diagnostic],
isReset: Boolean,
originId: String,
): Unit = {
val isSamePathAsLastDiagnostic = path == lastPublished.get()
lastPublished.set(path)
val queue = this.diagnostics.getOrElseUpdate(
path,
new ConcurrentLinkedQueue[DiagnosticWithOrigin](),
)
if (isReset) {
queue.clear()
snapshots.remove(path)
}
if (queue.isEmpty && !diagnostics.isEmpty) {
snapshots(path) = path.toInput
}
diagnostics.foreach { diagnostic =>
queue.add(DiagnosticWithOrigin(diagnostic, originId))
}
// NOTE(olafur): we buffer up several diagnostics for the same path before forwarding
// them to the editor client. Without buffering, we risk publishing an exponential number
// notifications for a file with N number of diagnostics:
// Notification 1: [1]
// Notification 2: [1, 2]
// Notification 3: [1, 2, 3]
// Notification N: [1, ..., N]
if (isReset || !isSamePathAsLastDiagnostic) {
publishDiagnosticsBuffer()
publishDiagnostics(path, queue)
} else {
diagnosticsBuffer.add(path)
}
}
private def removeInverseDependenciesDiagnostics(
buildTarget: BuildTargetIdentifier
) = {
val inverseDeps =
buildTargets.allInverseDependencies(buildTarget) - buildTarget
val targets = for {
path <- diagnostics.keySet
targets <- buildTargets.sourceBuildTargets(path)
targetSet = targets.toSet
/* We add the check for `!targetSet.contains(buildTarget)` to avoid removing diagnostics
* if a file is duplicated across targets */
if targets.exists(inverseDeps.apply) && !targetSet.contains(buildTarget)
} yield {
diagnostics.remove(path)
publishDiagnostics(path)
targets
}
val targetsSet = targets.flatten.toSet
if (targetsSet.nonEmpty) {
downstreamTargets.addMapping(buildTarget, targetsSet)
}
}
private def publishDiagnostics(path: AbsolutePath): Unit = {
publishDiagnostics(
path,
diagnostics.getOrElse(path, new ju.LinkedList[DiagnosticWithOrigin]()),
)
}
def hasCompilationErrors(buildTarget: BuildTargetIdentifier): Boolean = {
compilationStatus
.get(buildTarget)
.map(status => status.code.isError || status.errors > 0)
.getOrElse(false)
}
def upstreamTargetsWithCompilationErrors(
buildTarget: BuildTargetIdentifier
): List[BuildTargetIdentifier] = {
buildTargets
.buildTargetTransitiveDependencies(buildTarget)
.filter(id => id != buildTarget && hasCompilationErrors(id))
.toList
}
def onScalafixLint(path: AbsolutePath, diags: List[Diagnostic]): Unit = {
if (diags.nonEmpty)
scalafixDiagnostics(path) = diags
else
scalafixDiagnostics.remove(path)
publishDiagnostics(path)
}
def hasSyntaxError(path: AbsolutePath): Boolean =
syntaxError.contains(path)
def hasDiagnosticError(path: AbsolutePath): Boolean = {
val fileDiagnostics = diagnostics
.get(path)
fileDiagnostics match {
case Some(diagnostics) =>
diagnostics.asScala.exists(
_.diagnostic.getSeverity() == l.DiagnosticSeverity.Error
)
case None => false
}
}
def getFileDiagnostics(path: AbsolutePath): List[Diagnostic] =
diagnostics.get(path).map(_.asScala.map(_.diagnostic).toList).getOrElse(Nil)
private def publishDiagnostics(
path: AbsolutePath,
queue: ju.Queue[DiagnosticWithOrigin],
): Unit = {
if (!path.isFile) return didDelete(path)
val uri = path.toURI.toString
val all = new ju.ArrayList[Diagnostic](queue.size() + 1)
for {
diagnostic <- queue.asScala
freshDiagnostic <- toFreshDiagnostic(path, diagnostic.diagnostic)
} {
all.add(freshDiagnostic)
}
for {
d <- syntaxError.get(path)
// De-duplicate only the most common and basic syntax errors.
isSameMessage = all.asScala.exists(diag =>
diag.getRange() == d
.getRange() && diag.getMessageAsString == d.getMessageAsString
)
isDuplicate =
d.getMessageAsString
.replace("`", "")
.startsWith("identifier expected but") &&
all.asScala.exists { other =>
other.getMessageAsString
.replace("`", "")
.startsWith("identifier expected") &&
other.getRange().getStart() == d.getRange().getStart()
}
if !isDuplicate && !isSameMessage
} {
all.add(d)
}
for (scalafixDiag <- scalafixDiagnostics.getOrElse(path, Nil))
all.add(scalafixDiag)
languageClient.publishDiagnostics(new PublishDiagnosticsParams(uri, all))
}
private def publishDiagnosticsBuffer(): Unit = {
clearDiagnosticsBuffer().foreach { path => publishDiagnostics(path) }
}
// Adjust positions for type errors for changes in the open buffer.
// Only needed when merging syntax errors with type errors.
def toFreshDiagnostic(
path: AbsolutePath,
d: Diagnostic,
fallbackToNearest: Boolean = true,
): Option[Diagnostic] = {
val snapshot = snapshots.get(path)
snapshot match {
case Some(snapshot) =>
val edit =
buffers.tokenEditDistance(path, snapshot.value, scalaVersionSelector)
val result = edit
.toRevised(
range = d.getRange,
adjustWithinToken = shouldAdjustWithinToken(d),
fallbackToNearest = fallbackToNearest,
)
.map { range =>
val ld = new l.Diagnostic(
range,
d.getMessageAsString,
d.getSeverity,
d.getSource,
)
// Scala 3 sets the diagnostic code to -1 for NoExplanation Messages. Ideally
// this will change and we won't need this check in the future, but for now
// let's not forward them, since they are not valid for all clients.
val isScala3NoExplanationDiag = d.getCode() != null && d
.getCode()
.isLeft() && d.getCode().getLeft() == "-1"
if (!isScala3NoExplanationDiag) ld.setCode(d.getCode())
ld.setTags(d.getTags())
ld.setRelatedInformation(d.getRelatedInformation)
ld.setCodeDescription(d.getCodeDescription())
adjustedDiagnosticData(d, edit)
.map(newData => ld.setData(newData))
ld
}
if (result.isEmpty) {
d.getRange.toMeta(snapshot).foreach { pos =>
val message = pos.formatMessage(
s"stale ${d.getSource} ${d.getSeverity.toString.toLowerCase()}",
d.getMessageAsString,
)
scribe.info(message)
}
}
result
case None =>
Some(d)
}
}
private def clearDiagnosticsBuffer(): Iterable[AbsolutePath] = {
val toPublish = mutable.Set.empty[AbsolutePath]
var path = diagnosticsBuffer.poll()
while (path != null) {
toPublish.add(path)
path = diagnosticsBuffer.poll()
}
toPublish
}
private def adjustedDiagnosticData(
diagnostic: l.Diagnostic,
edit: TokenEditDistance,
): Option[Object] =
diagnostic match {
case ScalacDiagnostic.ScalaDiagnostic(Left(textEdit)) =>
edit
.toRevised(
textEdit,
shouldAdjustWithinToken(diagnostic),
fallbackToNearest = false,
)
.map(_.toJsonObject)
case ScalacDiagnostic.ScalaDiagnostic(Right(scalaDiagnostic)) =>
edit
.toRevised(
scalaDiagnostic,
shouldAdjustWithinToken(diagnostic),
fallbackToNearest = false,
)
.map(_.toJsonObject)
case _ => Some(diagnostic.getData())
}
private def shouldAdjustWithinToken(diagnostic: l.Diagnostic): Boolean =
diagnostic.getSource() == "scala-cli"
}