Fix a race condition that could cause the build graph to not be generated when doing initial background indexing

We were making the initial `generateBuildGraph` call in `SourceKitLSPServer` from a `Task`. This means that `generateBuildGraph` could be executed after `waitForUpToDateBuildGraph` was called by `SemanticIndexManager`. Thus `waitForUpToDateBuildGraph` returned immediately and no files were background indexed.

Make `generateBuildGraph` immediately schedule a package reload for SwiftPM build systems instead of doing the hop through a `Task`, fixing the race condition.

rdar://135551812
This commit is contained in:
Alex Hoppen
2024-09-09 11:49:54 -07:00
parent 33e955ab6c
commit 4d1fa7a7ee
9 changed files with 36 additions and 35 deletions

View File

@@ -365,10 +365,11 @@ package actor SwiftPMBuildSystem {
extension SwiftPMBuildSystem {
/// (Re-)load the package settings by parsing the manifest and resolving all the targets and
/// dependencies.
package func reloadPackage() async throws {
try await packageLoadingQueue.asyncThrowing {
@discardableResult
package func schedulePackageReload() -> Task<Void, Swift.Error> {
return packageLoadingQueue.asyncThrowing {
try await self.reloadPackageImpl()
}.valuePropagatingCancellation
}
}
/// - Important: Must only be called on `packageLoadingQueue`.
@@ -549,8 +550,8 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuildSystem {
return []
}
package func generateBuildGraph() async throws {
try await self.reloadPackage()
package func scheduleBuildGraphGeneration() async throws {
self.schedulePackageReload()
}
package func waitForUpToDateBuildGraph() async {
@@ -743,7 +744,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuildSystem {
if events.contains(where: { self.fileEventShouldTriggerPackageReload(event: $0) }) {
logger.log("Reloading package because of file change")
await orLog("Reloading package") {
try await self.reloadPackage()
try await self.schedulePackageReload().value
}
}