Skip to content

Commit 7f2234c

Browse files
authored
Merge pull request #1078 from ahoppen/ahoppen/dont-check-document-out-of-date
Only check if a document has been modified while loading diagnostics during publishDiagnostics
2 parents 37198b5 + 1379c52 commit 7f2234c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,6 @@ actor DiagnosticReportManager {
110110
let dict = try await self.sourcekitd.send(skreq, fileContents: snapshot.text)
111111

112112
try Task.checkCancellation()
113-
guard (try? documentManager.latestSnapshot(snapshot.uri).id) == snapshot.id else {
114-
// Check that the document wasn't modified while we were getting diagnostics. This could happen because we are
115-
// calling `fullDocumentDiagnosticReport` from `publishDiagnosticsIfNeeded` outside of `messageHandlingQueue`
116-
// and thus a concurrent edit is possible while we are waiting for the sourcekitd request to return a result.
117-
throw ResponseError.unknown("Document was modified while loading diagnostics")
118-
}
119113

120114
let diagnostics: [Diagnostic] =
121115
dict[keys.diagnostics]?.compactMap({ diag in

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,20 @@ extension SwiftLanguageServer {
436436
for: snapshot,
437437
buildSettings: buildSettings
438438
)
439+
let latestSnapshotID = try? await documentManager.latestSnapshot(snapshot.uri).id
440+
if latestSnapshotID != snapshot.id {
441+
// Check that the document wasn't modified while we were getting diagnostics. This could happen because we are
442+
// calling `publishDiagnosticsIfNeeded` outside of `messageHandlingQueue` and thus a concurrent edit is
443+
// possible while we are waiting for the sourcekitd request to return a result.
444+
logger.log(
445+
"""
446+
Document was modified while loading diagnostics. \
447+
Loaded diagnostics for \(snapshot.id.version, privacy: .public), \
448+
latest snapshot is \((latestSnapshotID?.version).map(String.init) ?? "<nil>", privacy: .public)
449+
"""
450+
)
451+
throw CancellationError()
452+
}
439453

440454
await sourceKitServer.sendNotificationToClient(
441455
PublishDiagnosticsNotification(

0 commit comments

Comments
 (0)