Skip to content

Commit 13d0bee

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 e66a30a commit 13d0bee

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
@@ -459,6 +459,12 @@ public actor SourceKitLSPServer {
459459
/// Initialization can be awaited using `waitUntilInitialized`.
460460
private var initialized: Bool = false
461461

462+
/// Set to `true` after the user has opened a project that doesn't support background indexing while having background
463+
/// indexing enabled.
464+
///
465+
/// This ensures that we only inform the user about background indexing not being supported for these projects once.
466+
private var didSendBackgroundIndexingNotSupportedNotification = false
467+
462468
var options: Options
463469

464470
let toolchainRegistry: ToolchainRegistry
@@ -1275,7 +1281,7 @@ extension SourceKitLSPServer {
12751281
"Created workspace at \(workspaceFolder.uri.forLogging) as \(type(of: buildSystem)) with project root \(projectRoot ?? "<nil>")"
12761282
)
12771283

1278-
return try? await Workspace(
1284+
let workspace = try? await Workspace(
12791285
documentManager: self.documentManager,
12801286
rootUri: workspaceFolder.uri,
12811287
capabilityRegistry: capabilityRegistry,
@@ -1294,6 +1300,21 @@ extension SourceKitLSPServer {
12941300
self?.indexProgressManager.indexStatusDidChange()
12951301
}
12961302
)
1303+
if let workspace, options.indexOptions.enableBackgroundIndexing, workspace.semanticIndexManager == nil,
1304+
!self.didSendBackgroundIndexingNotSupportedNotification
1305+
{
1306+
self.sendNotificationToClient(
1307+
ShowMessageNotification(
1308+
type: .info,
1309+
message: """
1310+
Background indexing is currently only supported for SwiftPM projects. \
1311+
For all other project types, please run a build to update the index.
1312+
"""
1313+
)
1314+
)
1315+
self.didSendBackgroundIndexingNotSupportedNotification = true
1316+
}
1317+
return workspace
12971318
}
12981319

12991320
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
@@ -400,7 +400,7 @@ extension SwiftLanguageService {
400400
if buildSettings == nil || buildSettings!.isFallback, let fileUrl = note.textDocument.uri.fileURL {
401401
// Do not show this notification for non-file URIs to make sure we don't see this notificaiton for newly created
402402
// files (which get opened as with a `untitled:Unitled-1` URI by VS Code.
403-
await sourceKitLSPServer?.sendNotificationToClient(
403+
sourceKitLSPServer?.sendNotificationToClient(
404404
ShowMessageNotification(
405405
type: .warning,
406406
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
@@ -844,4 +844,15 @@ final class BackgroundIndexingTests: XCTestCase {
844844
"No file should exist at \(nestedIndexBuildURL)"
845845
)
846846
}
847+
848+
func testShowMessageWhenOpeningAProjectThatDoesntSupportBackgroundIndexing() async throws {
849+
let project = try await MultiFileTestProject(
850+
files: [
851+
"compile_commands.json": ""
852+
],
853+
serverOptions: backgroundIndexingOptions
854+
)
855+
let message = try await project.testClient.nextNotification(ofType: ShowMessageNotification.self)
856+
XCTAssert(message.message.contains("Background indexing"), "Received unexpected message: \(message.message)")
857+
}
847858
}

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)