Skip to content

Commit a54c709

Browse files
committed
When a sourcekitd diagnostics request fails, show the request error as a diagnostic on the source file
The missing diagnostics might be due to an error that the user can fix. Report it to them. Fixes swiftlang#1812 rdar://139514623
1 parent ec461d6 commit a54c709

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
@@ -376,4 +376,30 @@ final class PullDiagnosticsTests: XCTestCase {
376376
let note = try XCTUnwrap(diagnostic.relatedInformation?.only)
377377
XCTAssertEqual(note.location, try project.location(from: "1️⃣", to: "1️⃣", in: "FileA.swift"))
378378
}
379+
380+
func testDiagnosticsFromSourcekitdRequestError() async throws {
381+
let project = try await MultiFileTestProject(
382+
files: [
383+
"test.swift": """
384+
func test() {}
385+
""",
386+
"compile_flags.txt": "-invalid-argument",
387+
]
388+
)
389+
let (uri, _) = try project.openDocument("test.swift")
390+
let diagnostics = try await project.testClient.send(
391+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
392+
)
393+
XCTAssertEqual(
394+
diagnostics.fullReport?.items,
395+
[
396+
Diagnostic(
397+
range: Range(Position(line: 0, utf16index: 0)),
398+
severity: .error,
399+
source: "SourceKit",
400+
message: "Internal SourceKit error: unknown argument: '-invalid-argument'"
401+
)
402+
]
403+
)
404+
}
379405
}

0 commit comments

Comments
 (0)