diff --git a/Documentation/Enable Experimental Background Indexing.md b/Documentation/Enable Experimental Background Indexing.md index 1075daa0..adb5c1c5 100644 --- a/Documentation/Enable Experimental Background Indexing.md +++ b/Documentation/Enable Experimental Background Indexing.md @@ -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 toolchain’s 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" } ``` diff --git a/Sources/SKOptions/SourceKitLSPOptions.swift b/Sources/SKOptions/SourceKitLSPOptions.swift index a2a60ec6..12445258 100644 --- a/Sources/SKOptions/SourceKitLSPOptions.swift +++ b/Sources/SKOptions/SourceKitLSPOptions.swift @@ -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 diff --git a/Sources/SKTestSupport/SkipUnless.swift b/Sources/SKTestSupport/SkipUnless.swift index b2f1bea9..53cf2d24 100644 --- a/Sources/SKTestSupport/SkipUnless.swift +++ b/Sources/SKTestSupport/SkipUnless.swift @@ -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( diff --git a/Sources/SKTestSupport/SwiftPMTestProject.swift b/Sources/SKTestSupport/SwiftPMTestProject.swift index 92c94926..5abe722f 100644 --- a/Sources/SKTestSupport/SwiftPMTestProject.swift +++ b/Sources/SKTestSupport/SwiftPMTestProject.swift @@ -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, diff --git a/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift b/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift index b11017c1..4764d84f 100644 --- a/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift +++ b/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift @@ -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)") } diff --git a/Tests/SourceKitLSPTests/ExpandMacroTests.swift b/Tests/SourceKitLSPTests/ExpandMacroTests.swift index d379a73f..f2aba2fa 100644 --- a/Tests/SourceKitLSPTests/ExpandMacroTests.swift +++ b/Tests/SourceKitLSPTests/ExpandMacroTests.swift @@ -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,