Skip to content

Commit 9771138

Browse files
authored
Merge pull request #1886 from ahoppen/sourcekitd-request-diagnostics
When a sourcekitd diagnostics request fails, show the request error as a diagnostic on the source file
2 parents 9a1c75e + a54c709 commit 9771138

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,27 @@ actor DiagnosticReportManager {
110110
keys.compilerArgs: compilerArgs as [SKDRequestValue],
111111
])
112112

113-
let dict = try await self.sourcekitd.send(
114-
skreq,
115-
timeout: options.sourcekitdRequestTimeoutOrDefault,
116-
fileContents: snapshot.text
117-
)
113+
let dict: SKDResponseDictionary
114+
do {
115+
dict = try await self.sourcekitd.send(
116+
skreq,
117+
timeout: options.sourcekitdRequestTimeoutOrDefault,
118+
fileContents: snapshot.text
119+
)
120+
} catch SKDError.requestFailed(let sourcekitdError) {
121+
var errorMessage = sourcekitdError
122+
if errorMessage.hasPrefix("error response (Request Failed): error: ") {
123+
errorMessage = String(errorMessage.dropFirst(40))
124+
}
125+
return RelatedFullDocumentDiagnosticReport(items: [
126+
Diagnostic(
127+
range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0),
128+
severity: .error,
129+
source: "SourceKit",
130+
message: "Internal SourceKit error: \(errorMessage)"
131+
)
132+
])
133+
}
118134

119135
try Task.checkCancellation()
120136

Tests/SourceKitLSPTests/PullDiagnosticsTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,30 @@ final class PullDiagnosticsTests: XCTestCase {
405405
let note = try XCTUnwrap(diagnostic.relatedInformation?.only)
406406
XCTAssertEqual(note.location, try project.location(from: "1️⃣", to: "1️⃣", in: "FileA.swift"))
407407
}
408+
409+
func testDiagnosticsFromSourcekitdRequestError() async throws {
410+
let project = try await MultiFileTestProject(
411+
files: [
412+
"test.swift": """
413+
func test() {}
414+
""",
415+
"compile_flags.txt": "-invalid-argument",
416+
]
417+
)
418+
let (uri, _) = try project.openDocument("test.swift")
419+
let diagnostics = try await project.testClient.send(
420+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
421+
)
422+
XCTAssertEqual(
423+
diagnostics.fullReport?.items,
424+
[
425+
Diagnostic(
426+
range: Range(Position(line: 0, utf16index: 0)),
427+
severity: .error,
428+
source: "SourceKit",
429+
message: "Internal SourceKit error: unknown argument: '-invalid-argument'"
430+
)
431+
]
432+
)
433+
}
408434
}

0 commit comments

Comments
 (0)