Skip to content

Commit 936b5b2

Browse files
authored
Merge pull request #1381 from ahoppen/dont-catch-cancellation-error-for-diags-request
Don’t catch `CancellationError` for document diagnostics
2 parents 0ee2405 + 2beae82 commit 936b5b2

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Sources/SKTestSupport/TestSourceKitLSPClient.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,16 @@ public final class TestSourceKitLSPClient: MessageHandler {
206206
///
207207
/// This version of the `send` function should only be used if some action needs to be performed after the request is
208208
/// sent but before it returns a result.
209-
public func send<R: RequestType>(_ request: R, completionHandler: @escaping (LSPResult<R.Response>) -> Void) {
210-
server.handle(request, id: .number(Int(nextRequestID.fetchAndIncrement()))) { result in
209+
@discardableResult
210+
public func send<R: RequestType>(
211+
_ request: R,
212+
completionHandler: @escaping (LSPResult<R.Response>) -> Void
213+
) -> RequestID {
214+
let requestID = RequestID.number(Int(nextRequestID.fetchAndIncrement()))
215+
server.handle(request, id: requestID) { result in
211216
completionHandler(result)
212217
}
218+
return requestID
213219
}
214220

215221
/// Send the notification to `server`.

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ extension SwiftLanguageService {
888888
buildSettings: buildSettings
889889
)
890890
return .full(diagnosticReport)
891+
} catch let error as CancellationError {
892+
throw error
891893
} catch {
892894
// VS Code does not request diagnostics again for a document if the diagnostics request failed.
893895
// Since sourcekit-lsp usually recovers from failures (e.g. after sourcekitd crashes), this is undesirable.

Tests/SourceKitLSPTests/PullDiagnosticsTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,32 @@ final class PullDiagnosticsTests: XCTestCase {
316316
diagnosticRequestSent.value = true
317317
try await fulfillmentOfOrThrow([receivedDiagnostics])
318318
}
319+
320+
func testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled() async throws {
321+
let diagnosticRequestCancelled = self.expectation(description: "diagnostic request cancelled")
322+
var serverOptions = SourceKitLSPServer.Options.testDefault
323+
serverOptions.indexTestHooks.preparationTaskDidStart = { _ in
324+
await self.fulfillment(of: [diagnosticRequestCancelled], timeout: defaultTimeout)
325+
}
326+
let project = try await SwiftPMTestProject(
327+
files: [
328+
"Lib.swift": "let x: String = 1"
329+
],
330+
serverOptions: serverOptions,
331+
enableBackgroundIndexing: true,
332+
pollIndex: false
333+
)
334+
let (uri, _) = try project.openDocument("Lib.swift")
335+
336+
let diagnosticResponseReceived = self.expectation(description: "Received diagnostic response")
337+
let requestID = project.testClient.send(
338+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
339+
) { result in
340+
XCTAssertEqual(result.failure?.code, .cancelled)
341+
diagnosticResponseReceived.fulfill()
342+
}
343+
project.testClient.send(CancelRequestNotification(id: requestID))
344+
diagnosticRequestCancelled.fulfill()
345+
try await fulfillmentOfOrThrow([diagnosticResponseReceived])
346+
}
319347
}

0 commit comments

Comments
 (0)