Skip to content

Commit 690fd92

Browse files
committed
Extract target out of the FileIndexInfo used by UpdateIndexStoreTaskDescription
This way we can guarantee that all files passed to `UpdateIndexStoreTaskDescription` belong to the same target, which will simplify multi-file indexing.
1 parent dddc983 commit 690fd92

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ private struct InProgressIndexStore {
9191
var fileModificationDate: Date?
9292
}
9393

94+
/// The information that's needed to index a file within a given target.
95+
package struct FileIndexInfo: Sendable, Hashable {
96+
package let file: FileToIndex
97+
package let target: BuildTargetIdentifier
98+
package let outputPath: OutputPath
99+
}
100+
94101
/// Status of document indexing / target preparation in `inProgressIndexAndPreparationTasks`.
95102
package enum IndexTaskStatus: Comparable {
96103
case scheduled
@@ -722,16 +729,18 @@ package final actor SemanticIndexManager {
722729
}
723730
}
724731

725-
/// Update the index store for the given files, assuming that their targets have already been prepared.
732+
/// Update the index store for the given files, assuming that their targets has already been prepared.
726733
private func updateIndexStore(
727-
for filesAndTargets: [FileIndexInfo],
734+
for fileAndOutputPaths: [FileAndOutputPath],
735+
target: BuildTargetIdentifier,
728736
indexFilesWithUpToDateUnit: Bool,
729737
preparationTaskID: UUID,
730738
priority: TaskPriority?
731739
) async {
732740
let taskDescription = AnyIndexTaskDescription(
733741
UpdateIndexStoreTaskDescription(
734-
filesToIndex: filesAndTargets,
742+
filesToIndex: fileAndOutputPaths,
743+
target: target,
735744
buildServerManager: self.buildServerManager,
736745
index: index,
737746
indexStoreUpToDateTracker: indexStoreUpToDateTracker,
@@ -747,7 +756,12 @@ package final actor SemanticIndexManager {
747756
self.indexProgressStatusDidChange()
748757
return
749758
}
750-
for fileAndTarget in filesAndTargets {
759+
for fileAndOutputPath in fileAndOutputPaths {
760+
let fileAndTarget = FileIndexInfo(
761+
file: fileAndOutputPath.file,
762+
target: target,
763+
outputPath: fileAndOutputPath.outputPath
764+
)
751765
switch self.inProgressIndexTasks[fileAndTarget]?.state {
752766
case .updatingIndexStore(let registeredTask, _):
753767
if registeredTask == OpaqueQueuedIndexTask(task) {
@@ -763,7 +777,12 @@ package final actor SemanticIndexManager {
763777
}
764778
self.indexProgressStatusDidChange()
765779
}
766-
for fileAndTarget in filesAndTargets {
780+
for fileAndOutputPath in fileAndOutputPaths {
781+
let fileAndTarget = FileIndexInfo(
782+
file: fileAndOutputPath.file,
783+
target: target,
784+
outputPath: fileAndOutputPath.outputPath
785+
)
767786
switch inProgressIndexTasks[fileAndTarget]?.state {
768787
case .waitingForPreparation(preparationTaskID, let indexTask), .preparing(preparationTaskID, let indexTask):
769788
inProgressIndexTasks[fileAndTarget]?.state = .updatingIndexStore(
@@ -938,8 +957,18 @@ package final actor SemanticIndexManager {
938957
// (https://github.com/swiftlang/sourcekit-lsp/issues/1268)
939958
for fileBatch in filesByTarget[target]!.partition(intoBatchesOfSize: 1) {
940959
taskGroup.addTask {
960+
let fileAndOutputPaths: [FileAndOutputPath] = fileBatch.compactMap {
961+
guard $0.target == target else {
962+
logger.fault(
963+
"FileIndexInfo refers to different target than should be indexed \($0.target.forLogging) vs \(target.forLogging)"
964+
)
965+
return nil
966+
}
967+
return FileAndOutputPath(file: $0.file, outputPath: $0.outputPath)
968+
}
941969
await self.updateIndexStore(
942-
for: fileBatch,
970+
for: fileAndOutputPaths,
971+
target: target,
943972
indexFilesWithUpToDateUnit: indexFilesWithUpToDateUnit,
944973
preparationTaskID: preparationTaskID,
945974
priority: priority

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ package enum FileToIndex: CustomLogStringConvertible, Hashable {
7979
}
8080
}
8181

82-
/// The information that's needed to index a file within a given target.
83-
package struct FileIndexInfo: Sendable, Hashable {
82+
/// A source file to index and the output path that should be used for indexing.
83+
package struct FileAndOutputPath: Sendable, Hashable {
8484
package let file: FileToIndex
85-
package let target: BuildTargetIdentifier
8685
package let outputPath: OutputPath
86+
87+
fileprivate var mainFile: DocumentURI { file.mainFile }
88+
fileprivate var sourceFile: DocumentURI { file.sourceFile }
8789
}
8890

8991
/// Describes a task to index a set of source files.
@@ -94,7 +96,10 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
9496
package let id = updateIndexStoreIDForLogging.fetchAndIncrement()
9597

9698
/// The files that should be indexed.
97-
package let filesToIndex: [FileIndexInfo]
99+
package let filesToIndex: [FileAndOutputPath]
100+
101+
/// The target in whose context the files should be indexed.
102+
package let target: BuildTargetIdentifier
98103

99104
/// The build server manager that is used to get the toolchain and build settings for the files to index.
100105
private let buildServerManager: BuildServerManager
@@ -143,7 +148,8 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
143148
}
144149

145150
init(
146-
filesToIndex: [FileIndexInfo],
151+
filesToIndex: [FileAndOutputPath],
152+
target: BuildTargetIdentifier,
147153
buildServerManager: BuildServerManager,
148154
index: UncheckedIndex,
149155
indexStoreUpToDateTracker: UpToDateTracker<DocumentURI, BuildTargetIdentifier>,
@@ -156,6 +162,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
156162
hooks: IndexHooks
157163
) {
158164
self.filesToIndex = filesToIndex
165+
self.target = target
159166
self.buildServerManager = buildServerManager
160167
self.index = index
161168
self.indexStoreUpToDateTracker = indexStoreUpToDateTracker
@@ -185,11 +192,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
185192
// TODO: Once swiftc supports it, we should group files by target and index files within the same target together
186193
// in one swiftc invocation. (https://github.com/swiftlang/sourcekit-lsp/issues/1268)
187194
for fileIndexInfo in filesToIndex {
188-
await updateIndexStore(
189-
forSingleFile: fileIndexInfo.file,
190-
in: fileIndexInfo.target,
191-
outputPath: fileIndexInfo.outputPath
192-
)
195+
await updateIndexStore(forSingleFile: fileIndexInfo.file, outputPath: fileIndexInfo.outputPath)
193196
}
194197
// If we know the output paths, make sure that we load their units into indexstore-db. We would eventually also
195198
// pick the units up through file watching but that would leave a short time period in which we think that
@@ -230,11 +233,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
230233
}
231234
}
232235

233-
private func updateIndexStore(
234-
forSingleFile file: FileToIndex,
235-
in target: BuildTargetIdentifier,
236-
outputPath: OutputPath
237-
) async {
236+
private func updateIndexStore(forSingleFile file: FileToIndex, outputPath: OutputPath) async {
238237
guard await !indexStoreUpToDateTracker.isUpToDate(file.sourceFile, target) else {
239238
// If we know that the file is up-to-date without having ot hit the index, do that because it's fastest.
240239
return

Tests/SourceKitLSPTests/ExpectedIndexTaskTracker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ actor ExpectedIndexTaskTracker {
264264
}
265265
}
266266

267-
fileprivate extension FileIndexInfo {
267+
fileprivate extension FileAndOutputPath {
268268
var sourceFileName: String? {
269269
return self.file.sourceFile.fileURL?.lastPathComponent
270270
}

0 commit comments

Comments
 (0)