Skip to content

Commit 5d75e14

Browse files
authored
Merge pull request #1352 from ahoppen/dont-reindex
Don’t re-index files in languages that cannot be indexed
2 parents 84007ca + 193ab77 commit 5d75e14

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,19 @@ public final actor SemanticIndexManager {
470470
// schedule two indexing jobs for the same file in quick succession, only the first one actually updates the index
471471
// store and the second one will be a no-op once it runs.
472472
let outOfDateFiles = await filesToIndex(toCover: files).asyncFilter {
473-
return await !indexStoreUpToDateStatus.isUpToDate($0.sourceFile)
473+
if await indexStoreUpToDateStatus.isUpToDate($0.sourceFile) {
474+
return false
475+
}
476+
guard let language = await buildSystemManager.defaultLanguage(for: $0.mainFile),
477+
UpdateIndexStoreTaskDescription.canIndex(language: language)
478+
else {
479+
return false
480+
}
481+
return true
474482
}
475483
// sort files to get deterministic indexing order
476484
.sorted(by: { $0.sourceFile.stringValue < $1.sourceFile.stringValue })
485+
logger.debug("Scheduling indexing of \(outOfDateFiles.map(\.sourceFile.stringValue).joined(separator: ", "))")
477486

478487
// Sort the targets in topological order so that low-level targets get built before high-level targets, allowing us
479488
// to index the low-level targets ASAP.

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ public struct FileAndTarget: Sendable {
7676
public let target: ConfiguredTarget
7777
}
7878

79+
private enum IndexKind {
80+
case clang
81+
case swift
82+
83+
init?(language: Language) {
84+
switch language {
85+
case .swift:
86+
self = .swift
87+
case .c, .cpp, .objective_c, .objective_cpp:
88+
self = .clang
89+
default:
90+
return nil
91+
}
92+
}
93+
}
94+
7995
/// Describes a task to index a set of source files.
8096
///
8197
/// This task description can be scheduled in a `TaskScheduler`.
@@ -114,6 +130,10 @@ public struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
114130
return "update-indexstore-\(id)"
115131
}
116132

133+
static func canIndex(language: Language) -> Bool {
134+
return IndexKind(language: language) != nil
135+
}
136+
117137
init(
118138
filesToIndex: [FileAndTarget],
119139
buildSystemManager: BuildSystemManager,
@@ -216,7 +236,7 @@ public struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
216236
return
217237
}
218238
let startDate = Date()
219-
switch language {
239+
switch IndexKind(language: language) {
220240
case .swift:
221241
do {
222242
try await updateIndexStore(
@@ -228,7 +248,7 @@ public struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
228248
logger.error("Updating index store for \(file.forLogging) failed: \(error.forLogging)")
229249
BuildSettingsLogger.log(settings: buildSettings, for: file.mainFile)
230250
}
231-
case .c, .cpp, .objective_c, .objective_cpp:
251+
case .clang:
232252
do {
233253
try await updateIndexStore(
234254
forClangFile: file.mainFile,
@@ -239,7 +259,7 @@ public struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
239259
logger.error("Updating index store for \(file) failed: \(error.forLogging)")
240260
BuildSettingsLogger.log(settings: buildSettings, for: file.mainFile)
241261
}
242-
default:
262+
case nil:
243263
logger.error(
244264
"Not updating index store for \(file) because it is a language that is not supported by background indexing"
245265
)

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,4 +774,51 @@ final class BackgroundIndexingTests: XCTestCase {
774774
cleanUp: { expectedIndexTaskTracker.keepAlive() }
775775
)
776776
}
777+
778+
func testNoIndexingHappensWhenPackageIsReopened() async throws {
779+
let project = try await SwiftPMTestProject(
780+
files: [
781+
"SwiftLib/NonEmptySwiftFile.swift": """
782+
func test() {}
783+
""",
784+
"CLib/include/EmptyHeader.h": "",
785+
"CLib/Assembly.S": "",
786+
"CLib/EmptyC.c": "",
787+
"CLib/NonEmptyC.c": """
788+
void test() {}
789+
""",
790+
],
791+
manifest: """
792+
// swift-tools-version: 5.7
793+
794+
import PackageDescription
795+
796+
let package = Package(
797+
name: "MyLibrary",
798+
targets: [
799+
.target(name: "SwiftLib"),
800+
.target(name: "CLib"),
801+
]
802+
)
803+
""",
804+
serverOptions: backgroundIndexingOptions
805+
)
806+
807+
var otherClientOptions = backgroundIndexingOptions
808+
otherClientOptions.indexTestHooks = IndexTestHooks(
809+
preparationTaskDidStart: { taskDescription in
810+
XCTFail("Did not expect any target preparation, got \(taskDescription.targetsToPrepare)")
811+
},
812+
updateIndexStoreTaskDidStart: { taskDescription in
813+
XCTFail("Did not expect any indexing tasks, got \(taskDescription.filesToIndex)")
814+
}
815+
)
816+
let otherClient = try await TestSourceKitLSPClient(
817+
serverOptions: otherClientOptions,
818+
workspaceFolders: [
819+
WorkspaceFolder(uri: DocumentURI(project.scratchDirectory))
820+
]
821+
)
822+
_ = try await otherClient.send(PollIndexRequest())
823+
}
777824
}

0 commit comments

Comments
 (0)