Skip to content

Commit 0eacd29

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 0eacd29

18 files changed

+693
-950
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+Dependencies.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ extension Workspace {
507507
// Ensure the cache path exists and validate that edited dependencies.
508508
self.createCacheDirectories(observabilityScope: observabilityScope)
509509

510-
// FIXME: this should not block
511510
// Load the root manifests and currently checked out manifests.
512511
let rootManifests = try await self.loadRootManifests(
513512
packages: root.packages,

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: 64 additions & 69 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.
@@ -936,63 +931,63 @@ extension Workspace {
936931
}
937932

938933
var manifestLoadingDiagnostics = [Diagnostic]()
934+
defer { manifestLoadingScope.emit(manifestLoadingDiagnostics) }
939935

940936
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
937+
let manifest: Manifest
938+
do {
939+
manifest = try await self.manifestLoader.load(
940+
packagePath: packagePath,
941+
packageIdentity: packageIdentity,
942+
packageKind: packageKind,
943+
packageLocation: packageLocation,
944+
packageVersion: packageVersion.map { (version: $0, revision: nil) },
945+
currentToolsVersion: self.currentToolsVersion,
946+
identityResolver: self.identityResolver,
947+
dependencyMapper: self.dependencyMapper,
948+
fileSystem: fileSystem,
949+
observabilityScope: manifestLoadingScope,
950+
delegateQueue: .sharedConcurrent
951+
)
952+
} catch {
955953
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-
)
992-
}
993-
manifestLoadingScope.emit(manifestLoadingDiagnostics)
994-
completion(result)
954+
manifestLoadingDiagnostics.append(.error(error))
955+
self.delegate?.didLoadManifest(
956+
packageIdentity: packageIdentity,
957+
packagePath: packagePath,
958+
url: packageLocation,
959+
version: packageVersion,
960+
packageKind: packageKind,
961+
manifest: nil,
962+
diagnostics: manifestLoadingDiagnostics,
963+
duration: duration
964+
)
965+
throw error
995966
}
967+
968+
let duration = start.distance(to: .now())
969+
let validator = ManifestValidator(
970+
manifest: manifest,
971+
sourceControlValidator: self.repositoryManager,
972+
fileSystem: self.fileSystem
973+
)
974+
let validationIssues = validator.validate()
975+
if !validationIssues.isEmpty {
976+
// Diagnostics.fatalError indicates that a more specific diagnostic has already been added.
977+
manifestLoadingDiagnostics.append(contentsOf: validationIssues)
978+
throw Diagnostics.fatalError
979+
}
980+
self.delegate?.didLoadManifest(
981+
packageIdentity: packageIdentity,
982+
packagePath: packagePath,
983+
url: packageLocation,
984+
version: packageVersion,
985+
packageKind: packageKind,
986+
manifest: manifest,
987+
diagnostics: manifestLoadingDiagnostics,
988+
duration: duration
989+
)
990+
return manifest
996991
}
997992

998993
/// Validates that all the edited dependencies are still present in the file system.

0 commit comments

Comments
 (0)