Skip to content

Commit 996df8c

Browse files
committed
Introduce a BarrierRequest that ensures all messages are handled before it replies
We could get into a race condition in `testAddFile` where the `DidChangeWatchedFilesNotification` would get handled after we try getting completion results that rely on it. In a real-world use case, this is OK. Completion might still be incorrect until `DidChangeWatchedFilesNotification` gets handled but it will catch up eventually - usually earlier than later because in real-world scenarios the `DidChangeWatchedFilesNotification` and completion request are more than a few milliseconds apart. In test, however, we need to guarantee deterministic ordering. Introduce a `BarrierRequest` that has `TaskMetadata.globalConfigurationChange` and thus ensures that all notifications and requests before it have finished before returning. We can run this fake request after sending the `DidChangeWatchedFilesNotification` to make sure that it is handled. An alternative would be to mark `DidChangeWatchedFilesNotification` as `TaskMetadata.globalConfigurationChange`. But I would really like to avoid introducing a global ordering barrier between requests for a notification that is, for example, sent whenever a `.swift` file in the `.build` directory changes (e.g. on every package update).
1 parent 79b1ab2 commit 996df8c

File tree

7 files changed

+35
-0
lines changed

7 files changed

+35
-0
lines changed

Sources/LanguageServerProtocol/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_library(LanguageServerProtocol STATIC
2727
Notifications/WorkDoneProgress.swift
2828

2929
Requests/ApplyEditRequest.swift
30+
Requests/BarrierRequest.swift
3031
Requests/CallHierarchyIncomingCallsRequest.swift
3132
Requests/CallHierarchyOutgoingCallsRequest.swift
3233
Requests/CallHierarchyPrepareRequest.swift

Sources/LanguageServerProtocol/Messages.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/// `MessageRegistry._register()` which allows you to avoid bloating the real server implementation.
1818
public let builtinRequests: [_RequestType.Type] = [
1919
ApplyEditRequest.self,
20+
BarrierRequest.self,
2021
CallHierarchyIncomingCallsRequest.self,
2122
CallHierarchyOutgoingCallsRequest.self,
2223
CallHierarchyPrepareRequest.self,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// A no-op request that ensures all previous notifications and requests have been handled before any message
14+
/// after the barrier request is handled.
15+
public struct BarrierRequest: RequestType {
16+
public static var method: String = "workspace/_barrier"
17+
public typealias Response = VoidResponse
18+
19+
public init() {}
20+
}

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ fileprivate enum TaskMetadata: DependencyTracker {
215215
self = .globalConfigurationChange
216216
case is WorkspaceSymbolsRequest:
217217
self = .freestanding
218+
case is BarrierRequest:
219+
self = .globalConfigurationChange
218220
case is PollIndexRequest:
219221
self = .globalConfigurationChange
220222
case let request as ExecuteCommandRequest:
@@ -734,6 +736,8 @@ extension SourceKitServer: MessageHandler {
734736
await self.handleRequest(request, handler: self.workspaceSymbols)
735737
case let request as Request<PollIndexRequest>:
736738
await self.handleRequest(request, handler: self.pollIndex)
739+
case let request as Request<BarrierRequest>:
740+
await self.handleRequest(request, handler: { _ in VoidResponse() })
737741
case let request as Request<ExecuteCommandRequest>:
738742
await self.handleRequest(request, handler: self.executeCommand)
739743
case let request as Request<CallHierarchyIncomingCallsRequest>:

Tests/SourceKitLSPTests/CompilationDatabaseTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ final class CompilationDatabaseTests: XCTestCase {
6464
])
6565
)
6666

67+
// Ensure that the DidChangeWatchedFilesNotification is handled before we continue.
68+
_ = try await ws.testClient.send(BarrierRequest())
69+
6770
// DocumentHighlight should now point to the definition in the `#else` block.
6871

6972
let expectedPostEditHighlight = [

Tests/SourceKitLSPTests/SwiftPMIntegration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ final class SwiftPMIntegrationTests: XCTestCase {
134134
])
135135
)
136136

137+
// Ensure that the DidChangeWatchedFilesNotification is handled before we continue.
138+
_ = try await ws.testClient.send(BarrierRequest())
139+
137140
let completions = try await ws.testClient.send(
138141
CompletionRequest(textDocument: TextDocumentIdentifier(newFileUri), position: newFilePositions["2️⃣"])
139142
)

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ final class WorkspaceTests: XCTestCase {
272272
])
273273
)
274274

275+
// Ensure that the DidChangeWatchedFilesNotification is handled before we continue.
276+
_ = try await ws.testClient.send(BarrierRequest())
277+
275278
// After updating Package.swift in PackageB, PackageB can provide proper build settings for MyExec/main.swift and
276279
// thus workspace membership should switch to PackageB.
277280

0 commit comments

Comments
 (0)