Skip to content

Commit 015853b

Browse files
committed
Use async methods to when loading manifests
Switch to using async methods when loading manifests, converting synchronous code to the simpler async equivalents and converting sync API calls to their synchronous versions.
1 parent 6b256f3 commit 015853b

17 files changed

+692
-947
lines changed

Sources/Commands/PackageCommands/ResetCommands.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ extension SwiftPackageCommand {
2727
}
2828
}
2929

30-
struct PurgeCache: SwiftCommand {
30+
struct PurgeCache: AsyncSwiftCommand {
3131
static let configuration = CommandConfiguration(
3232
abstract: "Purge the global repository cache.")
3333

3434
@OptionGroup(visibility: .hidden)
3535
var globalOptions: GlobalOptions
3636

37-
func run(_ swiftCommandState: SwiftCommandState) throws {
38-
try swiftCommandState.getActiveWorkspace().purgeCache(observabilityScope: swiftCommandState.observabilityScope)
37+
func run(_ swiftCommandState: SwiftCommandState) async throws {
38+
try await swiftCommandState.getActiveWorkspace().purgeCache(observabilityScope: swiftCommandState.observabilityScope)
3939
}
4040
}
4141

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 396 additions & 585 deletions
Large diffs are not rendered by default.

Sources/PackageRegistry/RegistryClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2352,7 +2352,7 @@ extension Result {
23522352
}
23532353

23542354
extension DispatchQueue {
2355-
func asyncResult<T>(_ callback: @escaping (Result<T, Error>) -> Void, _ closure: @escaping () async throws -> T) {
2355+
public func asyncResult<T>(_ callback: @escaping (Result<T, Error>) -> Void, _ closure: @escaping () async throws -> T) {
23562356
let completion: (Result<T, Error>) -> Void = { result in self.async { callback(result) } }
23572357
Task {
23582358
do {

Sources/Workspace/PackageContainer/FileSystemPackageContainer.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,19 @@ public struct FileSystemPackageContainer: PackageContainer {
7878
}
7979

8080
// Load the manifest.
81-
// FIXME: this should not block
82-
return try await withCheckedThrowingContinuation { continuation in
83-
manifestLoader.load(
84-
packagePath: packagePath,
85-
packageIdentity: self.package.identity,
86-
packageKind: self.package.kind,
87-
packageLocation: self.package.locationString,
88-
packageVersion: nil,
89-
currentToolsVersion: self.currentToolsVersion,
90-
identityResolver: self.identityResolver,
91-
dependencyMapper: self.dependencyMapper,
92-
fileSystem: self.fileSystem,
93-
observabilityScope: self.observabilityScope,
94-
delegateQueue: .sharedConcurrent,
95-
callbackQueue: .sharedConcurrent,
96-
completion: {
97-
continuation.resume(with: $0)
98-
}
99-
)
100-
}
81+
return try await manifestLoader.load(
82+
packagePath: packagePath,
83+
packageIdentity: self.package.identity,
84+
packageKind: self.package.kind,
85+
packageLocation: self.package.locationString,
86+
packageVersion: nil,
87+
currentToolsVersion: self.currentToolsVersion,
88+
identityResolver: self.identityResolver,
89+
dependencyMapper: self.dependencyMapper,
90+
fileSystem: self.fileSystem,
91+
observabilityScope: self.observabilityScope,
92+
delegateQueue: .sharedConcurrent
93+
)
10194
}
10295
}
10396

Sources/Workspace/PackageContainer/RegistryPackageContainer.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ public class RegistryPackageContainer: PackageContainer {
148148
dependencyMapper: self.dependencyMapper,
149149
fileSystem: result.fileSystem,
150150
observabilityScope: self.observabilityScope,
151-
delegateQueue: .sharedConcurrent,
152-
callbackQueue: .sharedConcurrent
151+
delegateQueue: .sharedConcurrent
153152
)
154153
}
155154

Sources/Workspace/PackageContainer/SourceControlPackageContainer.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,26 +397,19 @@ internal final class SourceControlPackageContainer: PackageContainer, CustomStri
397397

398398
private func loadManifest(fileSystem: FileSystem, version: Version?, revision: String) async throws -> Manifest {
399399
// Load the manifest.
400-
// FIXME: this should not block
401-
return try await withCheckedThrowingContinuation { continuation in
402-
self.manifestLoader.load(
403-
packagePath: .root,
404-
packageIdentity: self.package.identity,
405-
packageKind: self.package.kind,
406-
packageLocation: self.package.locationString,
407-
packageVersion: (version: version, revision: revision),
408-
currentToolsVersion: self.currentToolsVersion,
409-
identityResolver: self.identityResolver,
410-
dependencyMapper: self.dependencyMapper,
411-
fileSystem: fileSystem,
412-
observabilityScope: self.observabilityScope,
413-
delegateQueue: .sharedConcurrent,
414-
callbackQueue: .sharedConcurrent,
415-
completion: {
416-
continuation.resume(with: $0)
417-
}
418-
)
419-
}
400+
return try await self.manifestLoader.load(
401+
packagePath: .root,
402+
packageIdentity: self.package.identity,
403+
packageKind: self.package.kind,
404+
packageLocation: self.package.locationString,
405+
packageVersion: (version: version, revision: revision),
406+
currentToolsVersion: self.currentToolsVersion,
407+
identityResolver: self.identityResolver,
408+
dependencyMapper: self.dependencyMapper,
409+
fileSystem: fileSystem,
410+
observabilityScope: self.observabilityScope,
411+
delegateQueue: .sharedConcurrent
412+
)
420413
}
421414

422415
public func getEnabledTraits(traitConfiguration: TraitConfiguration?, at revision: String?, version: Version?) async throws -> Set<String> {

Sources/Workspace/Workspace+Editing.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,13 @@ extension Workspace {
6666
// If there is something present at the destination, we confirm it has
6767
// a valid manifest with name canonical location as the package we are trying to edit.
6868
if fileSystem.exists(destination) {
69-
// FIXME: this should not block
70-
let manifest = try await withCheckedThrowingContinuation { continuation in
71-
self.loadManifest(
72-
packageIdentity: dependency.packageRef.identity,
73-
packageKind: .fileSystem(destination),
74-
packagePath: destination,
75-
packageLocation: dependency.packageRef.locationString,
76-
observabilityScope: observabilityScope,
77-
completion: {
78-
continuation.resume(with: $0)
79-
}
80-
)
81-
}
69+
let manifest = try await self.loadManifest(
70+
packageIdentity: dependency.packageRef.identity,
71+
packageKind: .fileSystem(destination),
72+
packagePath: destination,
73+
packageLocation: dependency.packageRef.locationString,
74+
observabilityScope: observabilityScope
75+
)
8276

8377
guard dependency.packageRef.canonicalLocation == manifest.canonicalPackageLocation else {
8478
return observabilityScope

Sources/Workspace/Workspace+Manifests.swift

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -892,19 +892,15 @@ extension Workspace {
892892
}
893893

894894
// Load and return the manifest.
895-
return await withCheckedContinuation { continuation in
896-
self.loadManifest(
897-
packageIdentity: managedDependency.packageRef.identity,
898-
packageKind: packageKind,
899-
packagePath: packagePath,
900-
packageLocation: managedDependency.packageRef.locationString,
901-
packageVersion: packageVersion,
902-
fileSystem: fileSystem,
903-
observabilityScope: observabilityScope
904-
) { result in
905-
continuation.resume(returning: try? result.get())
906-
}
907-
}
895+
return try? await self.loadManifest(
896+
packageIdentity: managedDependency.packageRef.identity,
897+
packageKind: packageKind,
898+
packagePath: packagePath,
899+
packageLocation: managedDependency.packageRef.locationString,
900+
packageVersion: packageVersion,
901+
fileSystem: fileSystem,
902+
observabilityScope: observabilityScope
903+
)
908904
}
909905

910906
/// Load the manifest at a given path.
@@ -917,9 +913,8 @@ extension Workspace {
917913
packageLocation: String,
918914
packageVersion: Version? = nil,
919915
fileSystem: FileSystem? = nil,
920-
observabilityScope: ObservabilityScope,
921-
completion: @escaping (Result<Manifest, Error>) -> Void
922-
) {
916+
observabilityScope: ObservabilityScope
917+
) async throws -> Manifest {
923918
let fileSystem = fileSystem ?? self.fileSystem
924919

925920
// Load the manifest, bracketed by the calls to the delegate callbacks.
@@ -938,60 +933,61 @@ extension Workspace {
938933
var manifestLoadingDiagnostics = [Diagnostic]()
939934

940935
let start = DispatchTime.now()
941-
self.manifestLoader.load(
942-
packagePath: packagePath,
943-
packageIdentity: packageIdentity,
944-
packageKind: packageKind,
945-
packageLocation: packageLocation,
946-
packageVersion: packageVersion.map { (version: $0, revision: nil) },
947-
currentToolsVersion: self.currentToolsVersion,
948-
identityResolver: self.identityResolver,
949-
dependencyMapper: self.dependencyMapper,
950-
fileSystem: fileSystem,
951-
observabilityScope: manifestLoadingScope,
952-
delegateQueue: .sharedConcurrent,
953-
callbackQueue: .sharedConcurrent
954-
) { result in
936+
do {
937+
let manifest = try await self.manifestLoader.load(
938+
packagePath: packagePath,
939+
packageIdentity: packageIdentity,
940+
packageKind: packageKind,
941+
packageLocation: packageLocation,
942+
packageVersion: packageVersion.map { (version: $0, revision: nil) },
943+
currentToolsVersion: self.currentToolsVersion,
944+
identityResolver: self.identityResolver,
945+
dependencyMapper: self.dependencyMapper,
946+
fileSystem: fileSystem,
947+
observabilityScope: manifestLoadingScope,
948+
delegateQueue: .sharedConcurrent
949+
)
955950
let duration = start.distance(to: .now())
956-
var result = result
957-
switch result {
958-
case .failure(let error):
959-
manifestLoadingDiagnostics.append(.error(error))
960-
self.delegate?.didLoadManifest(
961-
packageIdentity: packageIdentity,
962-
packagePath: packagePath,
963-
url: packageLocation,
964-
version: packageVersion,
965-
packageKind: packageKind,
966-
manifest: nil,
967-
diagnostics: manifestLoadingDiagnostics,
968-
duration: duration
969-
)
970-
case .success(let manifest):
971-
let validator = ManifestValidator(
972-
manifest: manifest,
973-
sourceControlValidator: self.repositoryManager,
974-
fileSystem: self.fileSystem
975-
)
976-
let validationIssues = validator.validate()
977-
if !validationIssues.isEmpty {
978-
// Diagnostics.fatalError indicates that a more specific diagnostic has already been added.
979-
result = .failure(Diagnostics.fatalError)
980-
manifestLoadingDiagnostics.append(contentsOf: validationIssues)
981-
}
982-
self.delegate?.didLoadManifest(
983-
packageIdentity: packageIdentity,
984-
packagePath: packagePath,
985-
url: packageLocation,
986-
version: packageVersion,
987-
packageKind: packageKind,
988-
manifest: manifest,
989-
diagnostics: manifestLoadingDiagnostics,
990-
duration: duration
991-
)
951+
let validator = ManifestValidator(
952+
manifest: manifest,
953+
sourceControlValidator: self.repositoryManager,
954+
fileSystem: self.fileSystem
955+
)
956+
let validationIssues = validator.validate()
957+
if !validationIssues.isEmpty {
958+
// Diagnostics.fatalError indicates that a more specific diagnostic has already been added.
959+
960+
manifestLoadingDiagnostics.append(contentsOf: validationIssues)
961+
manifestLoadingScope.emit(manifestLoadingDiagnostics)
962+
throw Diagnostics.fatalError
992963
}
964+
self.delegate?.didLoadManifest(
965+
packageIdentity: packageIdentity,
966+
packagePath: packagePath,
967+
url: packageLocation,
968+
version: packageVersion,
969+
packageKind: packageKind,
970+
manifest: manifest,
971+
diagnostics: manifestLoadingDiagnostics,
972+
duration: duration
973+
)
974+
manifestLoadingScope.emit(manifestLoadingDiagnostics)
975+
return manifest
976+
} catch {
977+
let duration = start.distance(to: .now())
978+
manifestLoadingDiagnostics.append(.error(error))
979+
self.delegate?.didLoadManifest(
980+
packageIdentity: packageIdentity,
981+
packagePath: packagePath,
982+
url: packageLocation,
983+
version: packageVersion,
984+
packageKind: packageKind,
985+
manifest: nil,
986+
diagnostics: manifestLoadingDiagnostics,
987+
duration: duration
988+
)
993989
manifestLoadingScope.emit(manifestLoadingDiagnostics)
994-
completion(result)
990+
throw error
995991
}
996992
}
997993

0 commit comments

Comments
 (0)