Use workspace/didChangeWatchedFiles from BSP to communicate file changes to the build system

This commit is contained in:
Alex Hoppen
2024-09-06 14:58:27 -07:00
parent b4d04ce983
commit e38d37e01c
11 changed files with 31 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
add_library(BuildServerProtocol STATIC
BuildTargets.swift
DidChangeWatchedFilesNotification.swift
FileOptions.swift
InitializeBuild.swift
Messages.swift

View File

@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import LanguageServerProtocol
/// Notification sent from SourceKit-LSP to the build system to indicate that files within the project have been modified.
public typealias DidChangeWatchedFilesNotification = LanguageServerProtocol.DidChangeWatchedFilesNotification

View File

@@ -332,7 +332,7 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
}
}
package func filesDidChange(_ events: [FileEvent]) {}
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {}
package func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
guard

View File

@@ -199,7 +199,7 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable {
func unregisterForChangeNotifications(for: DocumentURI) async
/// Called when files in the project change.
func filesDidChange(_ events: [FileEvent]) async
func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async
func fileHandlingCapability(for uri: DocumentURI) async -> FileHandlingCapability

View File

@@ -101,7 +101,7 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
}
package func filesDidChange(_ events: [FileEvent]) async {
await self.buildSystem?.underlyingBuildSystem.filesDidChange(events)
await self.buildSystem?.send(BuildServerProtocol.DidChangeWatchedFilesNotification(changes: events))
}
/// Implementation of `MessageHandler`, handling notifications from the build system.

View File

@@ -79,6 +79,8 @@ actor BuiltInBuildSystemAdapter: BuiltInBuildSystemMessageHandler {
// sent. We can only do this once all requests to the build system have been migrated and we can implement proper
// dependency management between the BSP messages
switch notification {
case let notification as DidChangeWatchedFilesNotification:
await self.underlyingBuildSystem.didChangeWatchedFiles(notification: notification)
default:
logger.error("Ignoring unknown notification \(type(of: notification).method) from SourceKit-LSP")
}

View File

@@ -212,8 +212,8 @@ extension CompilationDatabaseBuildSystem: BuiltInBuildSystem {
}
}
package func filesDidChange(_ events: [FileEvent]) async {
if events.contains(where: { self.fileEventShouldTriggerCompilationDatabaseReload(event: $0) }) {
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async {
if notification.changes.contains(where: { self.fileEventShouldTriggerCompilationDatabaseReload(event: $0) }) {
await self.reloadCompilationDatabase()
}
}

View File

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

View File

@@ -509,7 +509,7 @@ class ManualBuildSystem: BuiltInBuildSystem {
var indexStorePath: AbsolutePath? { nil }
var indexDatabasePath: AbsolutePath? { nil }
func filesDidChange(_ events: [FileEvent]) {}
func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {}
package func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
if map[uri] != nil {

View File

@@ -99,7 +99,7 @@ actor TestBuildSystem: BuiltInBuildSystem {
watchedFiles.remove(uri)
}
func filesDidChange(_ events: [FileEvent]) {}
func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) async {}
func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
if buildSettingsByFile[uri] != nil {

View File

@@ -38,7 +38,7 @@ final class CompilationDatabaseTests: XCTestCase {
let (mainUri, positions) = try project.openDocument("main.cpp")
// Do a sanity check and verify that we get the expected result from a hover response before modifing the compile commands.
// Do a sanity check and verify that we get the expected result from a hover response before modifying the compile commands.
let highlightRequest = DocumentHighlightRequest(
textDocument: TextDocumentIdentifier(mainUri),