Skip to content

Commit 37198b5

Browse files
authored
Merge pull request #1083 from ahoppen/ahoppen/no-compiler-args-message
Show message in the client if no compiler arguments could be found for a file
2 parents 755868a + a606908 commit 37198b5

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

Sources/SKTestSupport/TestSourceKitLSPClient.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,28 @@ public final class TestSourceKitLSPClient: MessageHandler {
201201
public func nextDiagnosticsNotification(
202202
timeout: TimeInterval = defaultTimeout
203203
) async throws -> PublishDiagnosticsNotification {
204-
struct CastError: Error, CustomStringConvertible {
205-
let actualType: any NotificationType.Type
204+
return try await nextNotification(ofType: PublishDiagnosticsNotification.self, timeout: timeout)
205+
}
206206

207-
var description: String { "Expected a publish diagnostics notification but got '\(actualType)'" }
208-
}
207+
private struct CastError: Error, CustomStringConvertible {
208+
let expectedType: any NotificationType.Type
209+
let actualType: any NotificationType.Type
210+
211+
var description: String { "Expected a \(expectedType) but got '\(actualType)'" }
212+
}
209213

214+
/// Await the next diagnostic notification sent to the client.
215+
///
216+
/// If the next notification is not of the expected type, this methods throws.
217+
public func nextNotification<ExpectedNotificationType: NotificationType>(
218+
ofType: ExpectedNotificationType.Type,
219+
timeout: TimeInterval = defaultTimeout
220+
) async throws -> ExpectedNotificationType {
210221
let nextNotification = try await nextNotification(timeout: timeout)
211-
guard let diagnostics = nextNotification as? PublishDiagnosticsNotification else {
212-
throw CastError(actualType: type(of: nextNotification))
222+
guard let notification = nextNotification as? ExpectedNotificationType else {
223+
throw CastError(expectedType: ExpectedNotificationType.self, actualType: type(of: nextNotification))
213224
}
214-
return diagnostics
225+
return notification
215226
}
216227

217228
/// Handle the next request that is sent to the client with the given handler.

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ extension SwiftLanguageServer {
341341
return
342342
}
343343

344+
let buildSettings = await self.buildSettings(for: snapshot.uri)
345+
if buildSettings == nil || buildSettings!.isFallback, let fileUrl = note.textDocument.uri.fileURL {
346+
// Do not show this notification for non-file URIs to make sure we don't see this notificaiton for newly created
347+
// files (which get opened as with a `untitled:Unitled-1` URI by VS Code.
348+
await sourceKitServer?.sendNotificationToClient(
349+
ShowMessageNotification(
350+
type: .warning,
351+
message: """
352+
Failed to get compiler arguments for \(fileUrl.lastPathComponent).
353+
Ensure the source file is part of a Swift package or has compiler arguments in compile_commands.json.
354+
Functionality will be limited.
355+
"""
356+
)
357+
)
358+
}
359+
344360
let req = sourcekitd.dictionary([
345361
keys.request: self.requests.editorOpen,
346362
keys.name: note.textDocument.uri.pseudoPath,
@@ -349,7 +365,7 @@ extension SwiftLanguageServer {
349365
keys.enableStructure: 0,
350366
keys.enableDiagnostics: 0,
351367
keys.syntacticOnly: 1,
352-
keys.compilerArgs: await self.buildSettings(for: snapshot.uri)?.compilerArgs as [SKDRequestValue]?,
368+
keys.compilerArgs: buildSettings?.compilerArgs as [SKDRequestValue]?,
353369
])
354370

355371
_ = try? await self.sourcekitd.send(req, fileContents: snapshot.text)

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ final class BuildSystemTests: XCTestCase {
249249
let documentManager = await self.testClient.server._documentManager
250250

251251
testClient.openDocument(text, uri: doc)
252+
_ = try await testClient.nextNotification(ofType: ShowMessageNotification.self)
252253
let openDiags = try await testClient.nextDiagnosticsNotification()
253254
XCTAssertEqual(openDiags.diagnostics.count, 1)
254255
XCTAssertEqual(text, try documentManager.latestSnapshot(doc).text)

0 commit comments

Comments
 (0)