Skip to content

Commit bce0993

Browse files
authored
Merge pull request #1714 from ahoppen/background-indexing-on
2 parents 396aa3c + 84fc3fe commit bce0993

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

Documentation/Enable Experimental Background Indexing.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
# Enable Experimental Background Indexing
22

3-
Background indexing in SourceKit-LSP is available as an experimental feature. This guide shows how to set up background indexing and which caveats to expect.
3+
> [!IMPORTANT]
4+
> Background indexing is enabled by default in Swift 6.1 toolchains and above. This guide only refers to Swift 6.0 toolchains.
5+
6+
Background indexing in SourceKit-LSP is enabled by default in Swift 6.1 toolchains and is available as an experimental feature for Swift 6.0 toolchains.
47

58
## Behavior Without Background Indexing
69

7-
By default SourceKit-LSP does not update its global index in the background or build Swift modules in the background. Thus, a lot of cross-module or global functionality is limited if the project hasn't been built recently. For example consider two modules: `Lib` and `Exec`, where `Exec` depends on `Lib`: Without background indexing, if a function is added to `Lib`, completion/jump to definition/etc in `Exec` would not be able to see that function until after a build. Background indexing solves that issue.
10+
With background indexing disabled SourceKit-LSP does not update its global index in the background or build Swift modules in the background. Thus, a lot of cross-module or global functionality is limited if the project hasn't been built recently. For example consider two modules: `Lib` and `Exec`, where `Exec` depends on `Lib`: Without background indexing, if a function is added to `Lib`, completion/jump to definition/etc in `Exec` would not be able to see that function until after a build. Background indexing solves that issue.
811

912
## Set Up
1013

11-
1. Install a `main` or `release/6.0` Swift Development Snapshot from https://www.swift.org/install or install the [Xcode 16 beta](https://developer.apple.com/xcode/).
12-
2. Point your editor to the newly installed toolchain.
13-
- In VS Code on macOS this can be done by adding the following to your `settings.json`:
14-
- For open source toolchains `"swift.path": "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin"`
15-
- When installing the Xcode 16 beta `"swift.path": "/Applications/Xcode-beta.app/Library/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"`
16-
- In VS Code on other platforms, you need to set the `swift.path` to the `usr/bin` directory of your toolchain’s install location.
17-
- Other editors likely also have a way to pick the Swift toolchain, the exact steps vary by your setup.
14+
1. Install the Swift 6.0 toolchain or install [Xcode 16](https://developer.apple.com/xcode/).
1815
3. Enable the experimental `background-indexing` feature by creating a [configuration file](Configuration%20File.md) with the following contents at `~/.sourcekit-lsp/config.json` with the following contents:
1916
```json
2017
{
21-
"backgroundIndexing": true
18+
"backgroundIndexing": true,
19+
"backgroundPreparationMode": "enabled"
2220
}
2321
```
2422

Sources/SKOptions/SourceKitLSPOptions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
288288
public var backgroundIndexing: Bool?
289289

290290
public var backgroundIndexingOrDefault: Bool {
291-
return backgroundIndexing ?? false
291+
return backgroundIndexing ?? true
292292
}
293293

294294
public var backgroundPreparationMode: String?
@@ -297,7 +297,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
297297
if let backgroundPreparationMode, let parsed = BackgroundPreparationMode(rawValue: backgroundPreparationMode) {
298298
return parsed
299299
}
300-
return .build
300+
return .enabled
301301
}
302302

303303
/// Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,10 @@ package actor SkipUnless {
450450
],
451451
manifest: SwiftPMTestProject.macroPackageManifest
452452
)
453-
try await SwiftPMTestProject.build(at: project.scratchDirectory)
453+
try await SwiftPMTestProject.build(
454+
at: project.scratchDirectory,
455+
extraArguments: ["--experimental-prepare-for-indexing"]
456+
)
454457
return .featureSupported
455458
} catch {
456459
return .featureUnsupported(

Sources/SKTestSupport/SwiftPMTestProject.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,19 @@ package class SwiftPMTestProject: MultiFileTestProject {
223223
}
224224

225225
/// Build a SwiftPM package package manifest is located in the directory at `path`.
226-
package static func build(at path: URL) async throws {
226+
package static func build(at path: URL, extraArguments: [String] = []) async throws {
227227
guard let swift = await ToolchainRegistry.forTesting.default?.swift?.asURL else {
228228
throw Error.swiftNotFound
229229
}
230-
var arguments = [
231-
try swift.filePath,
232-
"build",
233-
"--package-path", try path.filePath,
234-
"--build-tests",
235-
"-Xswiftc", "-index-ignore-system-modules",
236-
"-Xcc", "-index-ignore-system-symbols",
237-
]
230+
var arguments =
231+
[
232+
try swift.filePath,
233+
"build",
234+
"--package-path", try path.filePath,
235+
"--build-tests",
236+
"-Xswiftc", "-index-ignore-system-modules",
237+
"-Xcc", "-index-ignore-system-symbols",
238+
] + extraArguments
238239
if let globalModuleCache = try globalModuleCache {
239240
arguments += [
240241
"-Xswiftc", "-module-cache-path", "-Xswiftc", try globalModuleCache.filePath,

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ private func buildPath(
10261026
options: SourceKitLSPOptions.SwiftPMOptions = SourceKitLSPOptions.SwiftPMOptions(),
10271027
platform: String
10281028
) -> AbsolutePath {
1029-
let buildPath = AbsolutePath(validatingOrNil: options.scratchPath) ?? root.appending(component: ".build")
1029+
let buildPath =
1030+
AbsolutePath(validatingOrNil: options.scratchPath) ?? root.appending(components: ".build", "index-build")
10301031
return buildPath.appending(components: platform, "\(options.configuration ?? .debug)")
10311032
}

Tests/SourceKitLSPTests/ExpandMacroTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ final class ExpandMacroTests: XCTestCase {
5959
""",
6060
]
6161

62-
for (getReferenceDocument, peekDocuments) in cartesianProduct([true], [true]) {
62+
for (getReferenceDocument, peekDocuments) in cartesianProduct([true, false], [true, false]) {
6363
let project = try await SwiftPMTestProject(
6464
files: files,
6565
manifest: SwiftPMTestProject.macroPackageManifest,

0 commit comments

Comments
 (0)