Skip to content

Commit 56fa14a

Browse files
authored
Merge pull request #2274 from ahoppen/empty-target
Fix issue that caused the index progress indicator to get stuck if there are no sources in a target
2 parents 8b20ce7 + e758b57 commit 56fa14a

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Sources/SKTestSupport/CustomBuildServerTestProject.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ package final class CustomBuildServerTestProject<BuildServer: CustomBuildServer>
274274
package init(
275275
files: [RelativeFileLocation: String],
276276
buildServer buildServerType: BuildServer.Type,
277+
capabilities: ClientCapabilities = ClientCapabilities(),
277278
options: SourceKitLSPOptions? = nil,
278279
hooks: Hooks = Hooks(),
279280
enableBackgroundIndexing: Bool = false,
280281
pollIndex: Bool = true,
282+
preInitialization: ((TestSourceKitLSPClient) -> Void)? = nil,
281283
testScratchDir: URL? = nil,
282284
testName: String = #function
283285
) async throws {
@@ -290,9 +292,11 @@ package final class CustomBuildServerTestProject<BuildServer: CustomBuildServer>
290292
}
291293
try await super.init(
292294
files: files,
295+
capabilities: capabilities,
293296
options: options,
294297
hooks: hooks,
295298
enableBackgroundIndexing: enableBackgroundIndexing,
299+
preInitialization: preInitialization,
296300
testScratchDir: testScratchDir,
297301
testName: testName
298302
)

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ package final actor SemanticIndexManager {
833833
}
834834

835835
if filesToIndex.isEmpty {
836+
indexProgressStatusDidChange()
836837
// Early exit if there are no files to index.
837838
return Task {}
838839
}

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,106 @@ final class BackgroundIndexingTests: XCTestCase {
26662666
]
26672667
)
26682668
}
2669+
2670+
func testIndexingProgressDoesNotGetStuckIfThereAreNoSourceFilesInTarget() async throws {
2671+
actor BuildServer: CustomBuildServer {
2672+
let inProgressRequestsTracker = CustomBuildServerInProgressRequestTracker()
2673+
private let projectRoot: URL
2674+
2675+
init(projectRoot: URL, connectionToSourceKitLSP: any Connection) {
2676+
self.projectRoot = projectRoot
2677+
}
2678+
2679+
func initializeBuildRequest(_ request: InitializeBuildRequest) async throws -> InitializeBuildResponse {
2680+
return try initializationResponseSupportingBackgroundIndexing(
2681+
projectRoot: projectRoot,
2682+
outputPathsProvider: false
2683+
)
2684+
}
2685+
2686+
func workspaceBuildTargetsRequest(
2687+
_ request: WorkspaceBuildTargetsRequest
2688+
) async throws -> WorkspaceBuildTargetsResponse {
2689+
return WorkspaceBuildTargetsResponse(targets: [
2690+
BuildTarget(
2691+
id: .dummy,
2692+
capabilities: BuildTargetCapabilities(),
2693+
languageIds: [],
2694+
dependencies: []
2695+
)
2696+
])
2697+
}
2698+
2699+
func buildTargetSourcesRequest(_ request: BuildTargetSourcesRequest) throws -> BuildTargetSourcesResponse {
2700+
return BuildTargetSourcesResponse(items: [
2701+
SourcesItem(
2702+
target: .dummy,
2703+
sources: []
2704+
)
2705+
])
2706+
}
2707+
2708+
func textDocumentSourceKitOptionsRequest(
2709+
_ request: TextDocumentSourceKitOptionsRequest
2710+
) -> TextDocumentSourceKitOptionsResponse? {
2711+
var arguments = [request.textDocument.uri.pseudoPath]
2712+
if let defaultSDKPath {
2713+
arguments += ["-sdk", defaultSDKPath]
2714+
}
2715+
return TextDocumentSourceKitOptionsResponse(compilerArguments: arguments)
2716+
}
2717+
2718+
func prepareTarget(_ request: BuildTargetPrepareRequest) async throws -> VoidResponse {
2719+
return VoidResponse()
2720+
}
2721+
}
2722+
2723+
let expectation = self.expectation(description: "Did receive indexing work done progress")
2724+
let hooks = Hooks(
2725+
indexHooks: IndexHooks(buildGraphGenerationDidStart: {
2726+
// Defer build graph generation long enough so the the debouncer has time to start a work done progress for
2727+
// indexing.
2728+
do {
2729+
try await fulfillmentOfOrThrow(expectation)
2730+
} catch {
2731+
XCTFail("\(error)")
2732+
}
2733+
})
2734+
)
2735+
let project = try await CustomBuildServerTestProject(
2736+
files: [
2737+
"Test.swift": """
2738+
func 1️⃣myTestFunc() {}
2739+
"""
2740+
],
2741+
buildServer: BuildServer.self,
2742+
capabilities: ClientCapabilities(window: WindowClientCapabilities(workDoneProgress: true)),
2743+
hooks: hooks,
2744+
enableBackgroundIndexing: true,
2745+
pollIndex: false,
2746+
preInitialization: { testClient in
2747+
testClient.handleMultipleRequests { (request: CreateWorkDoneProgressRequest) in
2748+
return VoidResponse()
2749+
}
2750+
}
2751+
)
2752+
let startIndexing = try await project.testClient.nextNotification(ofType: WorkDoneProgress.self) { notification in
2753+
guard case .begin(let value) = notification.value else {
2754+
return false
2755+
}
2756+
return value.title == "Indexing"
2757+
}
2758+
expectation.fulfill()
2759+
_ = try await project.testClient.nextNotification(ofType: WorkDoneProgress.self) { notification in
2760+
guard notification.token == startIndexing.token else {
2761+
return false
2762+
}
2763+
guard case .end = notification.value else {
2764+
return false
2765+
}
2766+
return true
2767+
}
2768+
}
26692769
}
26702770

26712771
extension HoverResponseContents {

0 commit comments

Comments
 (0)