Skip to content

Commit 0ee9351

Browse files
authored
Merge pull request #1288 from ahoppen/background-index-build-plugin
Take run destinations into account for SwiftPM build targets
2 parents d7953d2 + 02512a2 commit 0ee9351

12 files changed

+181
-93
lines changed

Documentation/Background Indexing.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ Next, point your editor to use the just-built copy of SourceKit-LSP and enable b
2222
"swift.sourcekit-lsp.serverArguments": [ "--experimental-feature", "background-indexing" ],
2323
```
2424

25+
Background indexing requires a Swift 6 toolchain. You can download Swift 6 nightly toolchains from https://www.swift.org/download/#swift-60-development.
26+
2527
## Known issues
2628

27-
- The only supported toolchain for background indexing are currently [Swift 6.0 nightly toolchain snapshots](https://www.swift.org/download/#swift-60-development). Older toolchains are not supported and the nightly toolchains from `main` are having issues because building a target non-deterministically builds for tools or the destination [#1288](https://github.com/apple/sourcekit-lsp/pull/1288#issuecomment-2111400459) [rdar://128100158](rdar://128100158)
2829
- Not really a background indexing related issue but Swift nightly toolchain snapshots are crashing on macOS 14.4 and 14.5 (swift#73327)[https://github.com/apple/swift/issues/73327]
2930
- Workaround: Run the toolchains on an older version of macOS, if possible
3031
- Background Indexing is only supported for SwiftPM projects [#1269](https://github.com/apple/sourcekit-lsp/issues/1269), [#1271](https://github.com/apple/sourcekit-lsp/issues/1271)

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ extension BuildServerBuildSystem: BuildSystem {
278278
return nil
279279
}
280280

281+
public func toolchain(for uri: DocumentURI, _ language: Language) async -> SKCore.Toolchain? {
282+
return nil
283+
}
284+
281285
public func configuredTargets(for document: DocumentURI) async -> [ConfiguredTarget] {
282286
return [ConfiguredTarget(targetID: "dummy", runDestinationID: "dummy")]
283287
}

Sources/SKCore/BuildSystem.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public protocol BuildSystem: AnyObject, Sendable {
179179
/// If `nil` is returned, the language based on the file's extension.
180180
func defaultLanguage(for document: DocumentURI) async -> Language?
181181

182+
/// The toolchain that should be used to open the given document.
183+
///
184+
/// If `nil` is returned, then the default toolchain for the given language is used.
185+
func toolchain(for uri: DocumentURI, _ language: Language) async -> Toolchain?
186+
182187
/// Register the given file for build-system level change notifications, such
183188
/// as command line flag changes, dependency changes, etc.
184189
///

Sources/SKCore/BuildSystemManager.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,18 @@ extension BuildSystemManager {
104104

105105
/// Returns the toolchain that should be used to process the given document.
106106
public func toolchain(for uri: DocumentURI, _ language: Language) async -> Toolchain? {
107-
// To support multiple toolchains within a single workspace, we need to ask the build system which toolchain to use
108-
// for this document.
109-
return await toolchainRegistry.defaultToolchain(for: language)
107+
if let toolchain = await buildSystem?.toolchain(for: uri, language) {
108+
return toolchain
109+
}
110+
111+
switch language {
112+
case .swift:
113+
return await toolchainRegistry.preferredToolchain(containing: [\.sourcekitd, \.swift, \.swiftc])
114+
case .c, .cpp, .objective_c, .objective_cpp:
115+
return await toolchainRegistry.preferredToolchain(containing: [\.clang, \.clangd])
116+
default:
117+
return nil
118+
}
110119
}
111120

112121
/// - Note: Needed so we can set the delegate from a different isolation context.

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
119119
return nil
120120
}
121121

122+
public func toolchain(for uri: DocumentURI, _ language: Language) async -> SKCore.Toolchain? {
123+
return nil
124+
}
125+
122126
public func configuredTargets(for document: DocumentURI) async -> [ConfiguredTarget] {
123127
return [ConfiguredTarget(targetID: "dummy", runDestinationID: "dummy")]
124128
}

Sources/SKCore/ToolchainRegistry.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,26 +246,14 @@ public final actor ToolchainRegistry {
246246
return darwinToolchainOverride ?? ToolchainRegistry.darwinDefaultToolchainIdentifier
247247
}
248248

249-
/// The toolchain to use for a document in the given language if the build system doesn't override it.
250-
func defaultToolchain(for language: Language) -> Toolchain? {
251-
let supportsLang = { (toolchain: Toolchain) -> Bool in
252-
// FIXME: the fact that we're looking at clangd/sourcekitd instead of the compiler indicates this method needs a parameter stating what kind of tool we're looking for.
253-
switch language {
254-
case .swift:
255-
return toolchain.sourcekitd != nil
256-
case .c, .cpp, .objective_c, .objective_cpp:
257-
return toolchain.clangd != nil
258-
default:
259-
return false
260-
}
261-
}
262-
263-
if let toolchain = self.default, supportsLang(toolchain) {
249+
/// Returns the preferred toolchain that contains all the tools at the given key paths.
250+
public func preferredToolchain(containing requiredTools: [KeyPath<Toolchain, AbsolutePath?>]) -> Toolchain? {
251+
if let toolchain = self.default, requiredTools.allSatisfy({ toolchain[keyPath: $0] != nil }) {
264252
return toolchain
265253
}
266254

267255
for toolchain in toolchains {
268-
if supportsLang(toolchain) {
256+
if requiredTools.allSatisfy({ toolchain[keyPath: $0] != nil }) {
269257
return toolchain
270258
}
271259
}

0 commit comments

Comments
 (0)