Skip to content

Commit e87d9e8

Browse files
committed
Show message if background indexing is enabled but the workspace doesn’t support background indexing
If the user has enabled background indexing in sourcekit-lsp but opens a project that doesn’t support background indexing (compilation database, build server), we should show a message after opening the workspace, informing the user that background indexing is only supported in SwiftPM projects at the moment. Fixes #1255 rdar://127474711
1 parent 715796f commit e87d9e8

11 files changed

+55
-3
lines changed

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ private func readReponseDataKey(data: LSPAny?, key: String) -> String? {
259259
}
260260

261261
extension BuildServerBuildSystem: BuildSystem {
262+
public nonisolated var supportsPreparation: Bool { false }
263+
262264
/// The build settings for the given file.
263265
///
264266
/// Returns `nil` if no build settings have been received from the build

Sources/SKCore/BuildSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public protocol BuildSystem: AnyObject, Sendable {
121121
/// context.
122122
func setDelegate(_ delegate: BuildSystemDelegate?) async
123123

124+
/// Whether the build system is capable of preparing a target for indexing, ie. if the `prepare` methods has been
125+
/// implemented.
126+
var supportsPreparation: Bool { get }
127+
124128
/// Retrieve build settings for the given document with the given source
125129
/// language.
126130
///

Sources/SKCore/BuildSystemManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public actor BuildSystemManager {
6868
}
6969
}
7070

71+
public var supportsPreparation: Bool {
72+
return buildSystem?.supportsPreparation ?? false
73+
}
74+
7175
/// Create a BuildSystemManager that wraps the given build system. The new
7276
/// manager will modify the delegate of the underlying build system.
7377
public init(

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public actor CompilationDatabaseBuildSystem {
9393
}
9494

9595
extension CompilationDatabaseBuildSystem: BuildSystem {
96+
public nonisolated var supportsPreparation: Bool { false }
97+
9698
public var indexDatabasePath: AbsolutePath? {
9799
indexStorePath?.parentDirectory.appending(component: "IndexDatabase")
98100
}

Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ extension SwiftPMBuildSystem {
342342
}
343343

344344
extension SwiftPMBuildSystem: SKCore.BuildSystem {
345+
public nonisolated var supportsPreparation: Bool { true }
345346

346347
public var buildPath: TSCAbsolutePath {
347348
return TSCAbsolutePath(buildParameters.buildPath)

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public actor SourceKitLSPServer {
9999
/// Initialization can be awaited using `waitUntilInitialized`.
100100
private var initialized: Bool = false
101101

102+
/// Set to `true` after the user has opened a project that doesn't support background indexing while having background
103+
/// indexing enabled.
104+
///
105+
/// This ensures that we only inform the user about background indexing not being supported for these projects once.
106+
private var didSendBackgroundIndexingNotSupportedNotification = false
107+
102108
var options: Options
103109

104110
let toolchainRegistry: ToolchainRegistry
@@ -916,7 +922,7 @@ extension SourceKitLSPServer {
916922
"Created workspace at \(workspaceFolder.uri.forLogging) as \(type(of: buildSystem)) with project root \(projectRoot ?? "<nil>")"
917923
)
918924

919-
return try? await Workspace(
925+
let workspace = try? await Workspace(
920926
documentManager: self.documentManager,
921927
rootUri: workspaceFolder.uri,
922928
capabilityRegistry: capabilityRegistry,
@@ -935,6 +941,21 @@ extension SourceKitLSPServer {
935941
self?.indexProgressManager.indexProgressStatusDidChange()
936942
}
937943
)
944+
if let workspace, options.indexOptions.enableBackgroundIndexing, workspace.semanticIndexManager == nil,
945+
!self.didSendBackgroundIndexingNotSupportedNotification
946+
{
947+
self.sendNotificationToClient(
948+
ShowMessageNotification(
949+
type: .info,
950+
message: """
951+
Background indexing is currently only supported for SwiftPM projects. \
952+
For all other project types, please run a build to update the index.
953+
"""
954+
)
955+
)
956+
self.didSendBackgroundIndexingNotSupportedNotification = true
957+
}
958+
return workspace
938959
}
939960

940961
func initialize(_ req: InitializeRequest) async throws -> InitializeResult {

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ extension SwiftLanguageService {
405405
if buildSettings == nil || buildSettings!.isFallback, let fileUrl = note.textDocument.uri.fileURL {
406406
// Do not show this notification for non-file URIs to make sure we don't see this notificaiton for newly created
407407
// files (which get opened as with a `untitled:Unitled-1` URI by VS Code.
408-
await sourceKitLSPServer?.sendNotificationToClient(
408+
sourceKitLSPServer?.sendNotificationToClient(
409409
ShowMessageNotification(
410410
type: .warning,
411411
message: """

Sources/SourceKitLSP/Workspace.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ public final class Workspace: Sendable {
108108
mainFilesProvider: uncheckedIndex,
109109
toolchainRegistry: toolchainRegistry
110110
)
111-
if let uncheckedIndex, options.indexOptions.enableBackgroundIndexing {
111+
if options.indexOptions.enableBackgroundIndexing,
112+
let uncheckedIndex,
113+
await buildSystemManager.supportsPreparation
114+
{
112115
self.semanticIndexManager = SemanticIndexManager(
113116
index: uncheckedIndex,
114117
buildSystemManager: buildSystemManager,

Tests/SKCoreTests/BuildSystemManagerTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ class ManualBuildSystem: BuildSystem {
455455
self.delegate = delegate
456456
}
457457

458+
public nonisolated var supportsPreparation: Bool { false }
459+
458460
func buildSettings(for uri: DocumentURI, in buildTarget: ConfiguredTarget, language: Language) -> FileBuildSettings? {
459461
return map[uri]
460462
}

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,4 +862,15 @@ final class BackgroundIndexingTests: XCTestCase {
862862
"No file should exist at \(nestedIndexBuildURL)"
863863
)
864864
}
865+
866+
func testShowMessageWhenOpeningAProjectThatDoesntSupportBackgroundIndexing() async throws {
867+
let project = try await MultiFileTestProject(
868+
files: [
869+
"compile_commands.json": ""
870+
],
871+
enableBackgroundIndexing: true
872+
)
873+
let message = try await project.testClient.nextNotification(ofType: ShowMessageNotification.self)
874+
XCTAssert(message.message.contains("Background indexing"), "Received unexpected message: \(message.message)")
875+
}
865876
}

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ actor TestBuildSystem: BuildSystem {
4343
buildSettingsByFile[uri] = buildSettings
4444
}
4545

46+
public nonisolated var supportsPreparation: Bool { false }
47+
4648
func buildSettings(
4749
for document: DocumentURI,
4850
in buildTarget: ConfiguredTarget,

0 commit comments

Comments
 (0)