Skip to content

Commit 48e15d9

Browse files
committed
Use workspace/didChangeWatchedFiles from BSP to communicate file changes to the build system
1 parent 6004015 commit 48e15d9

11 files changed

+31
-12
lines changed

Sources/BuildServerProtocol/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_library(BuildServerProtocol STATIC
22
BuildTargets.swift
3+
DidChangeWatchedFilesNotification.swift
34
FileOptions.swift
45
InitializeBuild.swift
56
Messages.swift
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 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+
import LanguageServerProtocol
14+
15+
/// Notification sent from SourceKit-LSP to the build system to indicate that files within the project have been modified.
16+
public typealias DidChangeWatchedFilesNotification = LanguageServerProtocol.DidChangeWatchedFilesNotification

Sources/BuildSystemIntegration/BuildServerBuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
332332
}
333333
}
334334

335-
package func filesDidChange(_ events: [FileEvent]) {}
335+
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {}
336336

337337
package func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
338338
guard

Sources/BuildSystemIntegration/BuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable {
197197
func unregisterForChangeNotifications(for: DocumentURI) async
198198

199199
/// Called when files in the project change.
200-
func filesDidChange(_ events: [FileEvent]) async
200+
func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async
201201

202202
func fileHandlingCapability(for uri: DocumentURI) async -> FileHandlingCapability
203203

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
101101
}
102102

103103
package func filesDidChange(_ events: [FileEvent]) async {
104-
await self.buildSystem?.underlyingBuildSystem.filesDidChange(events)
104+
await self.buildSystem?.send(BuildServerProtocol.DidChangeWatchedFilesNotification(changes: events))
105105
}
106106

107107
/// Implementation of `MessageHandler`, handling notifications from the build system.

Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ actor BuiltInBuildSystemAdapter: BuiltInBuildSystemMessageHandler {
7979
// sent. We can only do this once all requests to the build system have been migrated and we can implement proper
8080
// dependency management between the BSP messages
8181
switch notification {
82+
case let notification as DidChangeWatchedFilesNotification:
83+
await self.underlyingBuildSystem.didChangeWatchedFiles(notification: notification)
8284
default:
8385
logger.error("Ignoring unknown notification \(type(of: notification).method) from SourceKit-LSP")
8486
}

Sources/BuildSystemIntegration/CompilationDatabaseBuildSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ extension CompilationDatabaseBuildSystem: BuiltInBuildSystem {
212212
}
213213
}
214214

215-
package func filesDidChange(_ events: [FileEvent]) async {
216-
if events.contains(where: { self.fileEventShouldTriggerCompilationDatabaseReload(event: $0) }) {
215+
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async {
216+
if notification.changes.contains(where: { self.fileEventShouldTriggerCompilationDatabaseReload(event: $0) }) {
217217
await self.reloadCompilationDatabase()
218218
}
219219
}

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
745745
}
746746
}
747747

748-
package func filesDidChange(_ events: [FileEvent]) async {
749-
if events.contains(where: { self.fileEventShouldTriggerPackageReload(event: $0) }) {
748+
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async {
749+
if notification.changes.contains(where: { self.fileEventShouldTriggerPackageReload(event: $0) }) {
750750
logger.log("Reloading package because of file change")
751751
await orLog("Reloading package") {
752752
try await self.reloadPackage()
@@ -756,7 +756,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
756756
var filesWithUpdatedDependencies: Set<DocumentURI> = []
757757
// If a Swift file within a target is updated, reload all the other files within the target since they might be
758758
// referring to a function in the updated file.
759-
for event in events {
759+
for event in notification.changes {
760760
guard event.uri.fileURL?.pathExtension == "swift", let targets = fileToTargets[event.uri] else {
761761
continue
762762
}
@@ -774,7 +774,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
774774
// directory outside the source tree.
775775
// If we have background indexing enabled, this is not necessary because we call `fileDependenciesUpdated` when
776776
// preparation of a target finishes.
777-
if !isForIndexBuild, events.contains(where: { $0.uri.fileURL?.pathExtension == "swiftmodule" }) {
777+
if !isForIndexBuild, notification.changes.contains(where: { $0.uri.fileURL?.pathExtension == "swiftmodule" }) {
778778
filesWithUpdatedDependencies.formUnion(self.fileToTargets.keys)
779779
}
780780
await self.fileDependenciesUpdatedDebouncer.scheduleCall(filesWithUpdatedDependencies)

Tests/BuildSystemIntegrationTests/BuildSystemManagerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ class ManualBuildSystem: BuiltInBuildSystem {
509509
var indexStorePath: AbsolutePath? { nil }
510510
var indexDatabasePath: AbsolutePath? { nil }
511511

512-
func filesDidChange(_ events: [FileEvent]) {}
512+
func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {}
513513

514514
package func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
515515
if map[uri] != nil {

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ actor TestBuildSystem: BuiltInBuildSystem {
9999
watchedFiles.remove(uri)
100100
}
101101

102-
func filesDidChange(_ events: [FileEvent]) {}
102+
func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async {}
103103

104104
func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
105105
if buildSettingsByFile[uri] != nil {

Tests/SourceKitLSPTests/CompilationDatabaseTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class CompilationDatabaseTests: XCTestCase {
3838

3939
let (mainUri, positions) = try project.openDocument("main.cpp")
4040

41-
// Do a sanity check and verify that we get the expected result from a hover response before modifing the compile commands.
41+
// Do a sanity check and verify that we get the expected result from a hover response before modifying the compile commands.
4242

4343
let highlightRequest = DocumentHighlightRequest(
4444
textDocument: TextDocumentIdentifier(mainUri),

0 commit comments

Comments
 (0)