Skip to content

Show message if background indexing is enabled but the workspace doesn’t support background indexing #1360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/SKCore/BuildServerBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ private func readReponseDataKey(data: LSPAny?, key: String) -> String? {
}

extension BuildServerBuildSystem: BuildSystem {
public nonisolated var supportsPreparation: Bool { false }

/// The build settings for the given file.
///
/// Returns `nil` if no build settings have been received from the build
Expand Down
4 changes: 4 additions & 0 deletions Sources/SKCore/BuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public protocol BuildSystem: AnyObject, Sendable {
/// context.
func setDelegate(_ delegate: BuildSystemDelegate?) async

/// Whether the build system is capable of preparing a target for indexing, ie. if the `prepare` methods has been
/// implemented.
var supportsPreparation: Bool { get }

/// Retrieve build settings for the given document with the given source
/// language.
///
Expand Down
4 changes: 4 additions & 0 deletions Sources/SKCore/BuildSystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public actor BuildSystemManager {
}
}

public var supportsPreparation: Bool {
return buildSystem?.supportsPreparation ?? false
}

/// Create a BuildSystemManager that wraps the given build system. The new
/// manager will modify the delegate of the underlying build system.
public init(
Expand Down
2 changes: 2 additions & 0 deletions Sources/SKCore/CompilationDatabaseBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public actor CompilationDatabaseBuildSystem {
}

extension CompilationDatabaseBuildSystem: BuildSystem {
public nonisolated var supportsPreparation: Bool { false }

public var indexDatabasePath: AbsolutePath? {
indexStorePath?.parentDirectory.appending(component: "IndexDatabase")
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ extension SwiftPMBuildSystem {
}

extension SwiftPMBuildSystem: SKCore.BuildSystem {
public nonisolated var supportsPreparation: Bool { true }

public var buildPath: TSCAbsolutePath {
return TSCAbsolutePath(buildParameters.buildPath)
Expand Down
23 changes: 22 additions & 1 deletion Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public actor SourceKitLSPServer {
/// Initialization can be awaited using `waitUntilInitialized`.
private var initialized: Bool = false

/// Set to `true` after the user has opened a project that doesn't support background indexing while having background
/// indexing enabled.
///
/// This ensures that we only inform the user about background indexing not being supported for these projects once.
private var didSendBackgroundIndexingNotSupportedNotification = false

var options: Options

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

return try? await Workspace(
let workspace = try? await Workspace(
documentManager: self.documentManager,
rootUri: workspaceFolder.uri,
capabilityRegistry: capabilityRegistry,
Expand All @@ -935,6 +941,21 @@ extension SourceKitLSPServer {
self?.indexProgressManager.indexProgressStatusDidChange()
}
)
if let workspace, options.indexOptions.enableBackgroundIndexing, workspace.semanticIndexManager == nil,
!self.didSendBackgroundIndexingNotSupportedNotification
{
self.sendNotificationToClient(
ShowMessageNotification(
type: .info,
message: """
Background indexing is currently only supported for SwiftPM projects. \
For all other project types, please run a build to update the index.
"""
)
)
self.didSendBackgroundIndexingNotSupportedNotification = true
}
return workspace
}

func initialize(_ req: InitializeRequest) async throws -> InitializeResult {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SourceKitLSP/Swift/SwiftLanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ extension SwiftLanguageService {
if buildSettings == nil || buildSettings!.isFallback, let fileUrl = note.textDocument.uri.fileURL {
// Do not show this notification for non-file URIs to make sure we don't see this notificaiton for newly created
// files (which get opened as with a `untitled:Unitled-1` URI by VS Code.
await sourceKitLSPServer?.sendNotificationToClient(
sourceKitLSPServer?.sendNotificationToClient(
ShowMessageNotification(
type: .warning,
message: """
Expand Down
5 changes: 4 additions & 1 deletion Sources/SourceKitLSP/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public final class Workspace: Sendable {
mainFilesProvider: uncheckedIndex,
toolchainRegistry: toolchainRegistry
)
if let uncheckedIndex, options.indexOptions.enableBackgroundIndexing {
if options.indexOptions.enableBackgroundIndexing,
let uncheckedIndex,
await buildSystemManager.supportsPreparation
{
self.semanticIndexManager = SemanticIndexManager(
index: uncheckedIndex,
buildSystemManager: buildSystemManager,
Expand Down
2 changes: 2 additions & 0 deletions Tests/SKCoreTests/BuildSystemManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ class ManualBuildSystem: BuildSystem {
self.delegate = delegate
}

public nonisolated var supportsPreparation: Bool { false }

func buildSettings(for uri: DocumentURI, in buildTarget: ConfiguredTarget, language: Language) -> FileBuildSettings? {
return map[uri]
}
Expand Down
11 changes: 11 additions & 0 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,15 @@ final class BackgroundIndexingTests: XCTestCase {
"No file should exist at \(nestedIndexBuildURL)"
)
}

func testShowMessageWhenOpeningAProjectThatDoesntSupportBackgroundIndexing() async throws {
let project = try await MultiFileTestProject(
files: [
"compile_commands.json": ""
],
enableBackgroundIndexing: true
)
let message = try await project.testClient.nextNotification(ofType: ShowMessageNotification.self)
XCTAssert(message.message.contains("Background indexing"), "Received unexpected message: \(message.message)")
}
}
2 changes: 2 additions & 0 deletions Tests/SourceKitLSPTests/BuildSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ actor TestBuildSystem: BuildSystem {
buildSettingsByFile[uri] = buildSettings
}

public nonisolated var supportsPreparation: Bool { false }

func buildSettings(
for document: DocumentURI,
in buildTarget: ConfiguredTarget,
Expand Down