Skip to content

APIDiff.swift: Address warning by using task group instead of semaphore #7959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions Sources/Commands/PackageCommands/APIDiff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Dispatch
import PackageGraph
import PackageModel
import SourceControl
import _Concurrency

struct DeprecatedAPIDiff: ParsableCommand {
static let configuration = CommandConfiguration(commandName: "experimental-api-diff",
Expand Down Expand Up @@ -119,50 +120,53 @@ struct APIDiff: AsyncSwiftCommand {
swiftCommandState: swiftCommandState
)

let results = ThreadSafeArrayStore<SwiftAPIDigester.ComparisonResult>()
let group = DispatchGroup()
let semaphore = DispatchSemaphore(value: Int(try buildSystem.buildPlan.destinationBuildParameters.workers))
var skippedModules: Set<String> = []

for module in modulesToDiff {
let moduleBaselinePath = baselineDir.appending("\(module).json")
guard swiftCommandState.fileSystem.exists(moduleBaselinePath) else {
print("\nSkipping \(module) because it does not exist in the baseline")
skippedModules.insert(module)
continue
}
semaphore.wait()
DispatchQueue.sharedConcurrent.async(group: group) {
do {
if let comparisonResult = try apiDigesterTool.compareAPIToBaseline(
at: moduleBaselinePath,
for: module,
buildPlan: try buildSystem.buildPlan,
except: breakageAllowlistPath
) {
results.append(comparisonResult)
let results = await withTaskGroup(of: SwiftAPIDigester.ComparisonResult?.self, returning: [SwiftAPIDigester.ComparisonResult].self) { taskGroup in

for module in modulesToDiff {
let moduleBaselinePath = baselineDir.appending("\(module).json")
guard swiftCommandState.fileSystem.exists(moduleBaselinePath) else {
print("\nSkipping \(module) because it does not exist in the baseline")
skippedModules.insert(module)
continue
}
taskGroup.addTask {
do {
if let comparisonResult = try apiDigesterTool.compareAPIToBaseline(
at: moduleBaselinePath,
for: module,
buildPlan: try buildSystem.buildPlan,
except: breakageAllowlistPath
) {
return comparisonResult
}
} catch {
swiftCommandState.observabilityScope.emit(error: "failed to compare API to baseline", underlyingError: error)
}
} catch {
swiftCommandState.observabilityScope.emit(error: "failed to compare API to baseline", underlyingError: error)
return nil
}
semaphore.signal()
}
var results = [SwiftAPIDigester.ComparisonResult]()
for await result in taskGroup {
guard let result else { continue }
results.append(result)
}
return results
}

group.wait()

let failedModules = modulesToDiff
.subtracting(skippedModules)
.subtracting(results.map(\.moduleName))
for failedModule in failedModules {
swiftCommandState.observabilityScope.emit(error: "failed to read API digester output for \(failedModule)")
}

for result in results.get() {
for result in results {
try self.printComparisonResult(result, observabilityScope: swiftCommandState.observabilityScope)
}

guard failedModules.isEmpty && results.get().allSatisfy(\.hasNoAPIBreakingChanges) else {
guard failedModules.isEmpty && results.allSatisfy(\.hasNoAPIBreakingChanges) else {
throw ExitCode.failure
}
}
Expand Down