Skip to content

Commit 097e3e6

Browse files
committed
Call indexTaskDidFinish from SemanticIndexManager
This fixes a bug where `indexTaskDidFinish` would also get called when a task is cancelled to be rescheduled.
1 parent 4429e41 commit 097e3e6

File tree

3 files changed

+17
-43
lines changed

3 files changed

+17
-43
lines changed

Sources/SemanticIndex/PreparationTaskDescription.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ public struct PreparationTaskDescription: IndexTaskDescription {
3535
/// The build system manager that is used to get the toolchain and build settings for the files to index.
3636
private let buildSystemManager: BuildSystemManager
3737

38-
/// A callback that is called when the task finishes.
39-
///
40-
/// Intended for testing purposes.
41-
private let didFinishCallback: @Sendable (PreparationTaskDescription) -> Void
42-
4338
/// The task is idempotent because preparing the same target twice produces the same result as preparing it once.
4439
public var isIdempotent: Bool { true }
4540

@@ -55,18 +50,13 @@ public struct PreparationTaskDescription: IndexTaskDescription {
5550

5651
init(
5752
targetsToPrepare: [ConfiguredTarget],
58-
buildSystemManager: BuildSystemManager,
59-
didFinishCallback: @escaping @Sendable (PreparationTaskDescription) -> Void
53+
buildSystemManager: BuildSystemManager
6054
) {
6155
self.targetsToPrepare = targetsToPrepare
6256
self.buildSystemManager = buildSystemManager
63-
self.didFinishCallback = didFinishCallback
6457
}
6558

6659
public func execute() async {
67-
defer {
68-
didFinishCallback(self)
69-
}
7060
// Only use the last two digits of the preparation ID for the logging scope to avoid creating too many scopes.
7161
// See comment in `withLoggingScope`.
7262
// The last 2 digits should be sufficient to differentiate between multiple concurrently running preparation operations

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,38 +131,30 @@ public final actor SemanticIndexManager {
131131

132132
/// Prepare the given targets for indexing
133133
private func prepare(targets: [ConfiguredTarget], priority: TaskPriority?) async {
134-
await self.indexTaskScheduler.schedule(
135-
priority: priority,
136-
AnyIndexTaskDescription(
137-
PreparationTaskDescription(
138-
targetsToPrepare: targets,
139-
buildSystemManager: self.buildSystemManager,
140-
didFinishCallback: { [weak self] taskDescription in
141-
self?.indexTaskDidFinish?(AnyIndexTaskDescription(taskDescription))
142-
}
143-
)
134+
let taskDescription = AnyIndexTaskDescription(
135+
PreparationTaskDescription(
136+
targetsToPrepare: targets,
137+
buildSystemManager: self.buildSystemManager
144138
)
145-
).value
139+
)
140+
await self.indexTaskScheduler.schedule(priority: priority, taskDescription).value
141+
self.indexTaskDidFinish?(taskDescription)
146142
}
147143

148144
/// Update the index store for the given files, assuming that their targets have already been prepared.
149145
private func updateIndexStore(for files: [DocumentURI], priority: TaskPriority?) async {
150-
await self.indexTaskScheduler.schedule(
151-
priority: priority,
152-
AnyIndexTaskDescription(
153-
UpdateIndexStoreTaskDescription(
154-
filesToIndex: Set(files),
155-
buildSystemManager: self.buildSystemManager,
156-
index: self.index,
157-
didFinishCallback: { [weak self] taskDescription in
158-
self?.indexTaskDidFinish?(AnyIndexTaskDescription(taskDescription))
159-
}
160-
)
146+
let taskDescription = AnyIndexTaskDescription(
147+
UpdateIndexStoreTaskDescription(
148+
filesToIndex: Set(files),
149+
buildSystemManager: self.buildSystemManager,
150+
index: self.index
161151
)
162-
).value
152+
)
153+
await self.indexTaskScheduler.schedule(priority: priority, taskDescription).value
163154
for file in files {
164155
self.indexStatus[file] = .upToDate
165156
}
157+
self.indexTaskDidFinish?(taskDescription)
166158
}
167159

168160
/// Index the given set of files at the given priority.

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
3939
/// case we don't need to index it again.
4040
private let index: CheckedIndex
4141

42-
/// A callback that is called when the index task finishes
43-
private let didFinishCallback: @Sendable (UpdateIndexStoreTaskDescription) -> Void
44-
4542
/// The task is idempotent because indexing the same file twice produces the same result as indexing it once.
4643
public var isIdempotent: Bool { true }
4744

@@ -58,19 +55,14 @@ public struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
5855
init(
5956
filesToIndex: Set<DocumentURI>,
6057
buildSystemManager: BuildSystemManager,
61-
index: CheckedIndex,
62-
didFinishCallback: @escaping @Sendable (UpdateIndexStoreTaskDescription) -> Void
58+
index: CheckedIndex
6359
) {
6460
self.filesToIndex = filesToIndex
6561
self.buildSystemManager = buildSystemManager
6662
self.index = index
67-
self.didFinishCallback = didFinishCallback
6863
}
6964

7065
public func execute() async {
71-
defer {
72-
didFinishCallback(self)
73-
}
7466
// Only use the last two digits of the indexing ID for the logging scope to avoid creating too many scopes.
7567
// See comment in `withLoggingScope`.
7668
// The last 2 digits should be sufficient to differentiate between multiple concurrently running indexing operation.

0 commit comments

Comments
 (0)