Skip to content

Commit b468491

Browse files
committed
Guarantee that all files in a UpdateIndexStoreTaskDescription have the same language
It doesn’t make sense to try and index files with different langauges in a single compiler invocation.
1 parent 5c4f1ca commit b468491

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private struct InProgressIndexStore {
9494
/// The information that's needed to index a file within a given target.
9595
package struct FileIndexInfo: Sendable, Hashable {
9696
package let file: FileToIndex
97+
package let language: Language
9798
package let target: BuildTargetIdentifier
9899
package let outputPath: OutputPath
99100
}
@@ -507,10 +508,13 @@ package final actor SemanticIndexManager {
507508
continue
508509
}
509510
// If this is a source file, just index it.
511+
guard let language = await buildServerManager.defaultLanguage(for: uri, in: target) else {
512+
continue
513+
}
510514
didFindTargetToIndex = true
511515
filesToReIndex.append(
512516
(
513-
FileIndexInfo(file: .indexableFile(uri), target: target, outputPath: outputPath),
517+
FileIndexInfo(file: .indexableFile(uri), language: language, target: target, outputPath: outputPath),
514518
modifiedFilesIndex.modificationDate(of: uri)
515519
)
516520
)
@@ -550,10 +554,14 @@ package final actor SemanticIndexManager {
550554
{
551555
continue
552556
}
557+
guard let language = await buildServerManager.defaultLanguage(for: uri, in: targetAndOutputPath.key) else {
558+
continue
559+
}
553560
filesToReIndex.append(
554561
(
555562
FileIndexInfo(
556563
file: .headerFile(header: uri, mainFile: mainFile),
564+
language: language,
557565
target: targetAndOutputPath.key,
558566
outputPath: outputPath
559567
),
@@ -733,6 +741,7 @@ package final actor SemanticIndexManager {
733741
private func updateIndexStore(
734742
for fileAndOutputPaths: [FileAndOutputPath],
735743
target: BuildTargetIdentifier,
744+
language: Language,
736745
indexFilesWithUpToDateUnit: Bool,
737746
preparationTaskID: UUID,
738747
priority: TaskPriority?
@@ -741,6 +750,7 @@ package final actor SemanticIndexManager {
741750
UpdateIndexStoreTaskDescription(
742751
filesToIndex: fileAndOutputPaths,
743752
target: target,
753+
language: language,
744754
buildServerManager: self.buildServerManager,
745755
index: index,
746756
indexStoreUpToDateTracker: indexStoreUpToDateTracker,
@@ -759,6 +769,7 @@ package final actor SemanticIndexManager {
759769
for fileAndOutputPath in fileAndOutputPaths {
760770
let fileAndTarget = FileIndexInfo(
761771
file: fileAndOutputPath.file,
772+
language: language,
762773
target: target,
763774
outputPath: fileAndOutputPath.outputPath
764775
)
@@ -780,6 +791,7 @@ package final actor SemanticIndexManager {
780791
for fileAndOutputPath in fileAndOutputPaths {
781792
let fileAndTarget = FileIndexInfo(
782793
file: fileAndOutputPath.file,
794+
language: language,
783795
target: target,
784796
outputPath: fileAndOutputPath.outputPath
785797
)
@@ -873,13 +885,7 @@ package final actor SemanticIndexManager {
873885
var newIndexTasks = 0
874886

875887
for (fileIndexInfo, fileModificationDate) in filesToIndex {
876-
guard
877-
let language = await buildServerManager.defaultLanguage(
878-
for: fileIndexInfo.file.mainFile,
879-
in: fileIndexInfo.target
880-
),
881-
UpdateIndexStoreTaskDescription.canIndex(language: language)
882-
else {
888+
guard UpdateIndexStoreTaskDescription.canIndex(language: fileIndexInfo.language) else {
883889
continue
884890
}
885891

@@ -952,27 +958,34 @@ package final actor SemanticIndexManager {
952958
// And after preparation is done, index the files in the targets.
953959
await withTaskGroup(of: Void.self) { taskGroup in
954960
for target in targetsBatch {
955-
// TODO: Once swiftc supports indexing of multiple files in a single invocation, increase the batch size to
956-
// allow it to share AST builds between multiple files within a target.
957-
// (https://github.com/swiftlang/sourcekit-lsp/issues/1268)
958-
for fileBatch in filesByTarget[target]!.partition(intoBatchesOfSize: 1) {
959-
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
961+
var filesByLanguage: [Language: [FileIndexInfo]] = [:]
962+
for fileInfo in filesByTarget[target]! {
963+
filesByLanguage[fileInfo.language, default: []].append(fileInfo)
964+
}
965+
for (language, fileInfos) in filesByLanguage {
966+
// TODO: Once swiftc supports indexing of multiple files in a single invocation, increase the batch size to
967+
// allow it to share AST builds between multiple files within a target.
968+
// (https://github.com/swiftlang/sourcekit-lsp/issues/1268)
969+
for fileBatch in fileInfos.partition(intoBatchesOfSize: 1) {
970+
taskGroup.addTask {
971+
let fileAndOutputPaths: [FileAndOutputPath] = fileBatch.compactMap {
972+
guard $0.target == target else {
973+
logger.fault(
974+
"FileIndexInfo refers to different target than should be indexed \($0.target.forLogging) vs \(target.forLogging)"
975+
)
976+
return nil
977+
}
978+
return FileAndOutputPath(file: $0.file, outputPath: $0.outputPath)
966979
}
967-
return FileAndOutputPath(file: $0.file, outputPath: $0.outputPath)
980+
await self.updateIndexStore(
981+
for: fileAndOutputPaths,
982+
target: target,
983+
language: language,
984+
indexFilesWithUpToDateUnit: indexFilesWithUpToDateUnit,
985+
preparationTaskID: preparationTaskID,
986+
priority: priority
987+
)
968988
}
969-
await self.updateIndexStore(
970-
for: fileAndOutputPaths,
971-
target: target,
972-
indexFilesWithUpToDateUnit: indexFilesWithUpToDateUnit,
973-
preparationTaskID: preparationTaskID,
974-
priority: priority
975-
)
976989
}
977990
}
978991
}

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
101101
/// The target in whose context the files should be indexed.
102102
package let target: BuildTargetIdentifier
103103

104+
/// The common language of all main files in `filesToIndex`.
105+
package let language: Language
106+
104107
/// The build server manager that is used to get the toolchain and build settings for the files to index.
105108
private let buildServerManager: BuildServerManager
106109

@@ -150,6 +153,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
150153
init(
151154
filesToIndex: [FileAndOutputPath],
152155
target: BuildTargetIdentifier,
156+
language: Language,
153157
buildServerManager: BuildServerManager,
154158
index: UncheckedIndex,
155159
indexStoreUpToDateTracker: UpToDateTracker<DocumentURI, BuildTargetIdentifier>,
@@ -163,6 +167,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
163167
) {
164168
self.filesToIndex = filesToIndex
165169
self.target = target
170+
self.language = language
166171
self.buildServerManager = buildServerManager
167172
self.index = index
168173
self.indexStoreUpToDateTracker = indexStoreUpToDateTracker
@@ -260,10 +265,6 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
260265
}
261266

262267
for fileInfo in fileInfos {
263-
guard let language = await buildServerManager.defaultLanguage(for: fileInfo.mainFile, in: target) else {
264-
logger.error("Not indexing \(fileInfo.file.forLogging) because its language could not be determined")
265-
continue
266-
}
267268
let buildSettings = await buildServerManager.buildSettings(
268269
for: fileInfo.mainFile,
269270
in: target,

0 commit comments

Comments
 (0)