Merge pull request #1714 from ahoppen/background-indexing-on

This commit is contained in:
Alex Hoppen
2024-11-16 21:58:12 -08:00
committed by GitHub
6 changed files with 27 additions and 24 deletions

View File

@@ -1,24 +1,22 @@
# Enable Experimental Background Indexing
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.
> [!IMPORTANT]
> Background indexing is enabled by default in Swift 6.1 toolchains and above. This guide only refers to Swift 6.0 toolchains.
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.
## Behavior Without Background Indexing
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.
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.
## Set Up
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/).
2. Point your editor to the newly installed toolchain.
- In VS Code on macOS this can be done by adding the following to your `settings.json`:
- For open source toolchains `"swift.path": "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin"`
- When installing the Xcode 16 beta `"swift.path": "/Applications/Xcode-beta.app/Library/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"`
- In VS Code on other platforms, you need to set the `swift.path` to the `usr/bin` directory of your toolchains install location.
- Other editors likely also have a way to pick the Swift toolchain, the exact steps vary by your setup.
1. Install the Swift 6.0 toolchain or install [Xcode 16](https://developer.apple.com/xcode/).
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:
```json
{
"backgroundIndexing": true
"backgroundIndexing": true,
"backgroundPreparationMode": "enabled"
}
```

View File

@@ -288,7 +288,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
public var backgroundIndexing: Bool?
public var backgroundIndexingOrDefault: Bool {
return backgroundIndexing ?? false
return backgroundIndexing ?? true
}
public var backgroundPreparationMode: String?
@@ -297,7 +297,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
if let backgroundPreparationMode, let parsed = BackgroundPreparationMode(rawValue: backgroundPreparationMode) {
return parsed
}
return .build
return .enabled
}
/// Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel

View File

@@ -450,7 +450,10 @@ package actor SkipUnless {
],
manifest: SwiftPMTestProject.macroPackageManifest
)
try await SwiftPMTestProject.build(at: project.scratchDirectory)
try await SwiftPMTestProject.build(
at: project.scratchDirectory,
extraArguments: ["--experimental-prepare-for-indexing"]
)
return .featureSupported
} catch {
return .featureUnsupported(

View File

@@ -223,18 +223,19 @@ package class SwiftPMTestProject: MultiFileTestProject {
}
/// Build a SwiftPM package package manifest is located in the directory at `path`.
package static func build(at path: URL) async throws {
package static func build(at path: URL, extraArguments: [String] = []) async throws {
guard let swift = await ToolchainRegistry.forTesting.default?.swift?.asURL else {
throw Error.swiftNotFound
}
var arguments = [
try swift.filePath,
"build",
"--package-path", try path.filePath,
"--build-tests",
"-Xswiftc", "-index-ignore-system-modules",
"-Xcc", "-index-ignore-system-symbols",
]
var arguments =
[
try swift.filePath,
"build",
"--package-path", try path.filePath,
"--build-tests",
"-Xswiftc", "-index-ignore-system-modules",
"-Xcc", "-index-ignore-system-symbols",
] + extraArguments
if let globalModuleCache = try globalModuleCache {
arguments += [
"-Xswiftc", "-module-cache-path", "-Xswiftc", try globalModuleCache.filePath,

View File

@@ -1026,6 +1026,7 @@ private func buildPath(
options: SourceKitLSPOptions.SwiftPMOptions = SourceKitLSPOptions.SwiftPMOptions(),
platform: String
) -> AbsolutePath {
let buildPath = AbsolutePath(validatingOrNil: options.scratchPath) ?? root.appending(component: ".build")
let buildPath =
AbsolutePath(validatingOrNil: options.scratchPath) ?? root.appending(components: ".build", "index-build")
return buildPath.appending(components: platform, "\(options.configuration ?? .debug)")
}

View File

@@ -59,7 +59,7 @@ final class ExpandMacroTests: XCTestCase {
""",
]
for (getReferenceDocument, peekDocuments) in cartesianProduct([true], [true]) {
for (getReferenceDocument, peekDocuments) in cartesianProduct([true, false], [true, false]) {
let project = try await SwiftPMTestProject(
files: files,
manifest: SwiftPMTestProject.macroPackageManifest,