Skip to content

Don’t catch CancellationError for document diagnostics #1381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Sources/SKTestSupport/TestSourceKitLSPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,16 @@ public final class TestSourceKitLSPClient: MessageHandler {
///
/// This version of the `send` function should only be used if some action needs to be performed after the request is
/// sent but before it returns a result.
public func send<R: RequestType>(_ request: R, completionHandler: @escaping (LSPResult<R.Response>) -> Void) {
server.handle(request, id: .number(Int(nextRequestID.fetchAndIncrement()))) { result in
@discardableResult
public func send<R: RequestType>(
_ request: R,
completionHandler: @escaping (LSPResult<R.Response>) -> Void
) -> RequestID {
let requestID = RequestID.number(Int(nextRequestID.fetchAndIncrement()))
server.handle(request, id: requestID) { result in
completionHandler(result)
}
return requestID
}

/// Send the notification to `server`.
Expand Down
2 changes: 2 additions & 0 deletions Sources/SourceKitLSP/Swift/SwiftLanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ extension SwiftLanguageService {
buildSettings: buildSettings
)
return .full(diagnosticReport)
} catch let error as CancellationError {
throw error
} catch {
// VS Code does not request diagnostics again for a document if the diagnostics request failed.
// Since sourcekit-lsp usually recovers from failures (e.g. after sourcekitd crashes), this is undesirable.
Expand Down
28 changes: 28 additions & 0 deletions Tests/SourceKitLSPTests/PullDiagnosticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,32 @@ final class PullDiagnosticsTests: XCTestCase {
diagnosticRequestSent.value = true
try await fulfillmentOfOrThrow([receivedDiagnostics])
}

func testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled() async throws {
let diagnosticRequestCancelled = self.expectation(description: "diagnostic request cancelled")
var serverOptions = SourceKitLSPServer.Options.testDefault
serverOptions.indexTestHooks.preparationTaskDidStart = { _ in
await self.fulfillment(of: [diagnosticRequestCancelled], timeout: defaultTimeout)
}
let project = try await SwiftPMTestProject(
files: [
"Lib.swift": "let x: String = 1"
],
serverOptions: serverOptions,
enableBackgroundIndexing: true,
pollIndex: false
)
let (uri, _) = try project.openDocument("Lib.swift")

let diagnosticResponseReceived = self.expectation(description: "Received diagnostic response")
let requestID = project.testClient.send(
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
) { result in
XCTAssertEqual(result.failure?.code, .cancelled)
diagnosticResponseReceived.fulfill()
}
project.testClient.send(CancelRequestNotification(id: requestID))
diagnosticRequestCancelled.fulfill()
try await fulfillmentOfOrThrow([diagnosticResponseReceived])
}
}