Skip to content

Commit 1c96822

Browse files
committed
When the build system sends an update for build targets, update the index for the files in those targets
For each file in the changed targets, we still check whether it has an up-to-date unit based on timestamps. The important thing for this change is that we start indexing files for which we only receive build settings after an update from the build server.
1 parent 1a32c4a commit 1c96822

File tree

6 files changed

+109
-19
lines changed

6 files changed

+109
-19
lines changed

Sources/BuildServerProtocol/Messages/InitializeBuildRequest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ public struct SourceKitInitializeBuildResponseData: LSPAnyCodable, Codable, Send
283283
public var sourceKitOptionsProvider: Bool?
284284

285285
public init(
286-
indexDatabasePath: String?,
287-
indexStorePath: String?,
288-
watchers: [FileSystemWatcher]?,
289-
prepareProvider: Bool?,
290-
sourceKitOptionsProvider: Bool?
286+
indexDatabasePath: String? = nil,
287+
indexStorePath: String? = nil,
288+
watchers: [FileSystemWatcher]? = nil,
289+
prepareProvider: Bool? = nil,
290+
sourceKitOptionsProvider: Bool? = nil
291291
) {
292292
self.indexDatabasePath = indexDatabasePath
293293
self.indexStorePath = indexStorePath

Sources/BuildSystemIntegration/TestBuildSystem.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ package actor TestBuildSystem: MessageHandler {
3737
connectionToSourceKitLSP.send(OnBuildTargetDidChangeNotification(changes: nil))
3838
}
3939

40-
package nonisolated var supportsPreparation: Bool { false }
40+
private let initializeData: SourceKitInitializeBuildResponseData
4141

42-
package init(connectionToSourceKitLSP: any Connection) {
42+
package init(
43+
initializeData: SourceKitInitializeBuildResponseData = SourceKitInitializeBuildResponseData(
44+
sourceKitOptionsProvider: true
45+
),
46+
connectionToSourceKitLSP: any Connection
47+
) {
48+
self.initializeData = initializeData
4349
self.connectionToSourceKitLSP = connectionToSourceKitLSP
4450
}
4551

@@ -90,6 +96,8 @@ package actor TestBuildSystem: MessageHandler {
9096
handle(request, using: self.workspaceBuildTargetsRequest)
9197
case let request as WorkspaceWaitForBuildSystemUpdatesRequest:
9298
handle(request, using: self.workspaceWaitForBuildSystemUpdatesRequest)
99+
case let request as BuildTargetPrepareRequest:
100+
handle(request, using: self.prepareTarget)
93101
default:
94102
reply(.failure(ResponseError.methodNotFound(type(of: request).method)))
95103
}
@@ -101,7 +109,8 @@ package actor TestBuildSystem: MessageHandler {
101109
version: "",
102110
bspVersion: "2.2.0",
103111
capabilities: BuildServerCapabilities(),
104-
data: nil
112+
dataKind: .sourceKit,
113+
data: initializeData.encodeToLSPAny()
105114
)
106115
}
107116

@@ -148,6 +157,10 @@ package actor TestBuildSystem: MessageHandler {
148157
return buildSettingsByFile[request.textDocument.uri]
149158
}
150159

160+
func prepareTarget(_ request: BuildTargetPrepareRequest) async throws -> VoidResponse {
161+
return VoidResponse()
162+
}
163+
151164
package func waitForBuildSystemUpdates(request: WorkspaceWaitForBuildSystemUpdatesRequest) async -> VoidResponse {
152165
return VoidResponse()
153166
}

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ package final actor SemanticIndexManager {
673673
/// `indexFilesWithUpToDateUnit` is `true`.
674674
///
675675
/// The returned task finishes when all files are indexed.
676-
private func scheduleIndexing(
676+
package func scheduleIndexing(
677677
of files: some Collection<DocumentURI> & Sendable,
678678
indexFilesWithUpToDateUnit: Bool,
679679
priority: TaskPriority?

Sources/SourceKitLSP/Workspace.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,24 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
435435

436436
package func buildTargetsChanged(_ changes: [BuildTargetEvent]?) async {
437437
await sourceKitLSPServer?.fileHandlingCapabilityChanged()
438-
let testFiles = await orLog("Getting test files") { try await buildSystemManager.testFiles() } ?? []
439-
await syntacticTestIndex.listOfTestFilesDidChange(testFiles)
438+
await orLog("Scheduling re-indexing of changed targets") {
439+
var sourceFiles = try await self.buildSystemManager.sourceFiles(includeNonBuildableFiles: false)
440+
if let changes {
441+
let changedTargets = changes.map(\.target)
442+
sourceFiles = sourceFiles.filter {
443+
!$0.value.targets.intersection(changedTargets).isEmpty
444+
}
445+
}
446+
_ = await semanticIndexManager?.scheduleIndexing(
447+
of: sourceFiles.keys,
448+
indexFilesWithUpToDateUnit: false,
449+
priority: .low
450+
)
451+
}
452+
await orLog("Scheduling syntactic test re-indexing") {
453+
let testFiles = try await buildSystemManager.testFiles()
454+
await syntacticTestIndex.listOfTestFilesDidChange(testFiles)
455+
}
440456
}
441457

442458
package var clientSupportsWorkDoneProgress: Bool {

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import BuildServerProtocol
1314
import BuildSystemIntegration
1415
import LanguageServerProtocol
1516
import LanguageServerProtocolExtensions
@@ -1910,6 +1911,72 @@ final class BackgroundIndexingTests: XCTestCase {
19101911
try await project.testClient.send(IsIndexingRequest()).indexing == false
19111912
}
19121913
}
1914+
1915+
func testIndexFileIfBuildTargetsChange() async throws {
1916+
let testBuildSystem = ThreadSafeBox<TestBuildSystem?>(initialValue: nil)
1917+
let project = try await MultiFileTestProject(
1918+
files: [
1919+
"Test.swift": """
1920+
func 1️⃣myTestFunc() {}
1921+
"""
1922+
],
1923+
hooks: Hooks(
1924+
buildSystemHooks: BuildSystemHooks(injectBuildServer: { projectRoot, connectionToSourceKitLSP in
1925+
assert(testBuildSystem.value == nil, "Build system injector hook can only create a single TestBuildSystem")
1926+
let buildSystem = TestBuildSystem(
1927+
initializeData: SourceKitInitializeBuildResponseData(
1928+
indexDatabasePath: projectRoot.appendingPathComponent("index-db").path,
1929+
indexStorePath: projectRoot.appendingPathComponent("index-store").path,
1930+
prepareProvider: true,
1931+
sourceKitOptionsProvider: true
1932+
),
1933+
connectionToSourceKitLSP: connectionToSourceKitLSP
1934+
)
1935+
testBuildSystem.value = buildSystem
1936+
let connection = LocalConnection(receiverName: "TestBuildSystem")
1937+
connection.start(handler: buildSystem)
1938+
return connection
1939+
})
1940+
),
1941+
enableBackgroundIndexing: true
1942+
)
1943+
let fileUrl = try XCTUnwrap(project.uri(for: "Test.swift").fileURL)
1944+
1945+
var compilerArguments = [fileUrl.path]
1946+
if let defaultSDKPath {
1947+
compilerArguments += ["-sdk", defaultSDKPath]
1948+
}
1949+
1950+
// We don't initially index Test.swift because we don't have build settings for it.
1951+
1952+
try await XCTUnwrap(testBuildSystem.value).setBuildSettings(
1953+
for: DocumentURI(fileUrl),
1954+
to: TextDocumentSourceKitOptionsResponse(compilerArguments: compilerArguments)
1955+
)
1956+
1957+
// But once we get build settings for it, we should index the file.
1958+
1959+
try await repeatUntilExpectedResult {
1960+
let workspaceSymbols = try await project.testClient.send(WorkspaceSymbolsRequest(query: "myTestFunc"))
1961+
guard let workspaceSymbols, !workspaceSymbols.isEmpty else {
1962+
// No results yet, indexing of the file might not have finished.
1963+
return false
1964+
}
1965+
XCTAssertEqual(
1966+
workspaceSymbols,
1967+
[
1968+
.symbolInformation(
1969+
SymbolInformation(
1970+
name: "myTestFunc()",
1971+
kind: .function,
1972+
location: try project.location(from: "1️⃣", to: "1️⃣", in: "Test.swift")
1973+
)
1974+
)
1975+
]
1976+
)
1977+
return true
1978+
}
1979+
}
19131980
}
19141981

19151982
extension HoverResponseContents {

Tests/SourceKitLSPTests/WorkspaceSymbolsTests.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,14 @@ class WorkspaceSymbolsTests: XCTestCase {
7676
SymbolInformation(
7777
name: "afuncFromA()",
7878
kind: .function,
79-
location: Location(
80-
uri: try project.uri(for: "PackageALib.swift"),
81-
range: Range(try project.position(of: "1️⃣", in: "PackageALib.swift"))
82-
)
79+
location: try project.location(from: "1️⃣", to: "1️⃣", in: "PackageALib.swift")
8380
)
8481
),
8582
.symbolInformation(
8683
SymbolInformation(
8784
name: "funcFromB()",
8885
kind: .function,
89-
location: Location(
90-
uri: try project.uri(for: "PackageBLib.swift"),
91-
range: Range(try project.position(of: "2️⃣", in: "PackageBLib.swift"))
92-
)
86+
location: try project.location(from: "2️⃣", to: "2️⃣", in: "PackageBLib.swift")
9387
)
9488
),
9589
]

0 commit comments

Comments
 (0)