Skip to content

Allow overriding SwiftPM's build directory with background indexing #2193

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 2 commits into from
Jul 2, 2025
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
8 changes: 4 additions & 4 deletions Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
throw Error.cannotDetermineHostToolchain
}

var absProjectRoot = try AbsolutePath(validating: projectRoot.filePath)
let absProjectRoot = try AbsolutePath(validating: projectRoot.filePath)
let hostSDK = try SwiftSDK.hostSwiftSDK(AbsolutePath(validating: destinationToolchainBinDir.filePath))
let hostSwiftPMToolchain = try UserToolchain(swiftSDK: hostSDK)

Expand Down Expand Up @@ -237,10 +237,10 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
fileSystem: localFileSystem
)

if options.backgroundIndexingOrDefault {
location.scratchDirectory = absProjectRoot.appending(components: ".build", "index-build")
} else if let scratchDirectory = options.swiftPMOrDefault.scratchPath {
if let scratchDirectory = options.swiftPMOrDefault.scratchPath {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should use an index-build folder within the scratch directory for background indexing. Say a user stores their build directory in .my-build instead of .build for some reason. In that case they might specify .my-build as the scratch path but we‘d probably still want to use a separate build arena for background indexing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was torn on this. Technically there's no reason to match up their build + what's passed to sourcekit-lsp today, since we have a separate arena anyway. So if it is passed... seems weird to then tack on the arena?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess… My opinion isn’t strong enough to argue about this.

location.scratchDirectory = try AbsolutePath(validating: scratchDirectory, relativeTo: absProjectRoot)
} else if options.backgroundIndexingOrDefault {
location.scratchDirectory = absProjectRoot.appending(components: ".build", "index-build")
}

var configuration = WorkspaceConfiguration.default
Expand Down
5 changes: 3 additions & 2 deletions Sources/InProcessClient/InProcessSourceKitLSPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public import Foundation
public import LanguageServerProtocol
import LanguageServerProtocolExtensions
import SKLogging
package import SKOptions
public import SKOptions
package import SourceKitLSP
import SwiftExtensions
import TSCExtensions
Expand All @@ -31,13 +31,14 @@ public final class InProcessSourceKitLSPClient: Sendable {

public convenience init(
toolchainPath: URL?,
options: SourceKitLSPOptions = SourceKitLSPOptions(),
capabilities: ClientCapabilities = ClientCapabilities(),
workspaceFolders: [WorkspaceFolder],
messageHandler: any MessageHandler
) async throws {
try await self.init(
toolchainRegistry: ToolchainRegistry(installPath: toolchainPath),
options: SourceKitLSPOptions(),
options: options,
capabilities: capabilities,
workspaceFolders: workspaceFolders,
messageHandler: messageHandler
Expand Down
7 changes: 4 additions & 3 deletions Sources/SemanticIndex/PreparationTaskDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ package struct PreparationTaskDescription: IndexTaskDescription {
targetsToPrepare: [BuildTargetIdentifier],
buildSystemManager: BuildSystemManager,
preparationUpToDateTracker: UpToDateTracker<BuildTargetIdentifier, DummySecondaryKey>,
logMessageToIndexLog: @escaping @Sendable (
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
) -> Void,
logMessageToIndexLog:
@escaping @Sendable (
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
) -> Void,
hooks: IndexHooks
) {
self.targetsToPrepare = targetsToPrepare
Expand Down
7 changes: 4 additions & 3 deletions Sources/SemanticIndex/SemanticIndexManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ package final actor SemanticIndexManager {
updateIndexStoreTimeout: Duration,
hooks: IndexHooks,
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>,
logMessageToIndexLog: @escaping @Sendable (
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
) -> Void,
logMessageToIndexLog:
@escaping @Sendable (
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
) -> Void,
indexTasksWereScheduled: @escaping @Sendable (Int) -> Void,
indexProgressStatusDidChange: @escaping @Sendable () -> Void
) {
Expand Down
7 changes: 4 additions & 3 deletions Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
index: UncheckedIndex,
indexStoreUpToDateTracker: UpToDateTracker<DocumentURI, BuildTargetIdentifier>,
indexFilesWithUpToDateUnit: Bool,
logMessageToIndexLog: @escaping @Sendable (
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
) -> Void,
logMessageToIndexLog:
@escaping @Sendable (
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
) -> Void,
timeout: Duration,
hooks: IndexHooks
) {
Expand Down
7 changes: 4 additions & 3 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ package actor SourceKitLSPServer {

private func handleRequest<RequestType: TextDocumentRequest>(
for request: RequestAndReply<RequestType>,
requestHandler: @Sendable @escaping (
RequestType, Workspace, LanguageService
) async throws ->
requestHandler:
@Sendable @escaping (
RequestType, Workspace, LanguageService
) async throws ->
RequestType.Response
) async {
await request.reply {
Expand Down