Skip to content

Reduce usage of callback and dispatch methods in favor of async/await #8744

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 0 additions & 23 deletions Sources/PackageRegistry/ChecksumTOFU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,6 @@ struct PackageVersionChecksumTOFU {
}
}

@available(*, noasync, message: "Use the async alternative")
func validateSourceArchive(
registry: Registry,
package: PackageIdentity.RegistryIdentity,
version: Version,
checksum: String,
timeout: DispatchTimeInterval?,
observabilityScope: ObservabilityScope,
callbackQueue: DispatchQueue,
completion: @escaping @Sendable (Result<Void, Error>) -> Void
) {
callbackQueue.asyncResult(completion) {
try await self.validateSourceArchive(
registry: registry,
package: package,
version: version,
checksum: checksum,
timeout: timeout,
observabilityScope: observabilityScope
)
}
}

private func getExpectedChecksum(
registry: Registry,
package: PackageIdentity.RegistryIdentity,
Expand Down
19 changes: 0 additions & 19 deletions Sources/PackageRegistry/RegistryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,25 +613,6 @@ public final class RegistryClient: AsyncCancellable {
}
}

@available(*, deprecated, message: "Use the async alternative")
public func getAvailableManifests(
package: PackageIdentity,
version: Version,
timeout: DispatchTimeInterval? = .none,
observabilityScope: ObservabilityScope,
callbackQueue: DispatchQueue,
completion: @escaping @Sendable (Result<[String: (toolsVersion: ToolsVersion, content: String?)], Error>) -> Void
) {
callbackQueue.asyncResult(completion) {
try await self.getAvailableManifests(
package: package,
version: version,
timeout: timeout,
observabilityScope: observabilityScope
)
}
}

public func getManifestContent(
package: PackageIdentity,
version: Version,
Expand Down
19 changes: 0 additions & 19 deletions Sources/PackageRegistry/RegistryDownloadsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,6 @@ public class RegistryDownloadsManager: AsyncCancellable {
return try await task.value
}

@available(*, noasync, message: "Use the async alternative")
public func lookup(
package: PackageIdentity,
version: Version,
observabilityScope: ObservabilityScope,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue,
completion: @escaping @Sendable (Result<Basics.AbsolutePath, Error>) -> Void
) {
callbackQueue.asyncResult(completion) {
try await self.lookup(
package: package,
version: version,
observabilityScope: observabilityScope,
delegateQueue: delegateQueue
)
}
}

/// Cancel any outstanding requests
public func cancel(deadline: DispatchTime) async throws {
try await self.registryClient.cancel(deadline: deadline)
Expand Down
30 changes: 0 additions & 30 deletions Sources/PackageRegistry/SignatureValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,36 +350,6 @@ struct SignatureValidation {
return signingEntity;
}


@available(*, noasync, message: "Use the async alternative")
func validate(
registry: Registry,
package: PackageIdentity.RegistryIdentity,
version: Version,
toolsVersion: ToolsVersion?,
manifestContent: String,
configuration: RegistryConfiguration.Security.Signing,
timeout: DispatchTimeInterval?,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
callbackQueue: DispatchQueue,
completion: @escaping @Sendable (Result<SigningEntity?, Error>) -> Void
) {
callbackQueue.asyncResult(completion) {
try await self.validate(
registry: registry,
package: package,
version: version,
toolsVersion: toolsVersion,
manifestContent: manifestContent,
configuration: configuration,
timeout: timeout,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
}
}

private func getAndValidateManifestSignature(
registry: Registry,
package: PackageIdentity.RegistryIdentity,
Expand Down
20 changes: 0 additions & 20 deletions Sources/PackageRegistry/SigningEntityTOFU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,6 @@ struct PackageSigningEntityTOFU {
)
}

@available(*, noasync, message: "Use the async alternative")
func validate(
registry: Registry,
package: PackageIdentity.RegistryIdentity,
version: Version,
signingEntity: SigningEntity?,
observabilityScope: ObservabilityScope,
completion: @escaping @Sendable (Result<Void, Error>) -> Void
) {
DispatchQueue.sharedConcurrent.asyncResult(completion) {
try await self.validate(
registry: registry,
package: package,
version: version,
signingEntity: signingEntity,
observabilityScope: observabilityScope
)
}
}

private func validateSigningEntity(
registry: Registry,
package: PackageIdentity.RegistryIdentity,
Expand Down
5 changes: 2 additions & 3 deletions Sources/SourceControl/RepositoryManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public class RepositoryManager: Cancellable {
repository: RepositorySpecifier,
updateStrategy: RepositoryUpdateStrategy,
observabilityScope: ObservabilityScope,
delegateQueue: DispatchQueue,
callbackQueue: DispatchQueue
delegateQueue: DispatchQueue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#8721 should refactor this so lookup is all async.

) async throws -> RepositoryHandle {
try await withCheckedThrowingContinuation { continuation in
self.lookup(
Expand All @@ -107,7 +106,7 @@ public class RepositoryManager: Cancellable {
updateStrategy: updateStrategy,
observabilityScope: observabilityScope,
delegateQueue: delegateQueue,
callbackQueue: callbackQueue,
callbackQueue: .sharedConcurrent,
completion: { continuation.resume(with: $0) }
)
}
Expand Down
1 change: 0 additions & 1 deletion Sources/Workspace/Workspace+Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import func Basics.os_signpost
import struct Basics.RelativePath
import enum Basics.SignpostName
import class Basics.ThreadSafeKeyValueStore
import class Dispatch.DispatchGroup
import struct Dispatch.DispatchTime
import enum Dispatch.DispatchTimeInterval
import struct PackageGraph.Assignment
Expand Down
3 changes: 1 addition & 2 deletions Sources/Workspace/Workspace+Editing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ extension Workspace {
repository: repository,
updateStrategy: .never,
observabilityScope: observabilityScope,
delegateQueue: .sharedConcurrent,
callbackQueue: .sharedConcurrent
delegateQueue: .sharedConcurrent
)
let repo = try handle.open()

Expand Down
19 changes: 7 additions & 12 deletions Sources/Workspace/Workspace+Manifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import struct Basics.InternalError
import class Basics.ObservabilityScope
import struct Basics.SwiftVersion
import class Basics.ThreadSafeKeyValueStore
import class Dispatch.DispatchGroup
import struct Dispatch.DispatchTime
import struct OrderedCollections.OrderedDictionary
import struct OrderedCollections.OrderedSet
Expand Down Expand Up @@ -1045,17 +1044,13 @@ extension Workspace {
case .fileSystem:
return nil
case .custom:
let container = try await withCheckedThrowingContinuation { continuation in
self.packageContainerProvider.getContainer(
for: package,
updateStrategy: .never,
observabilityScope: observabilityScope,
on: .sharedConcurrent,
completion: {
continuation.resume(with: $0)
}
)
}
let container = try await self.packageContainerProvider.getContainer(
for: package,
updateStrategy: .never,
observabilityScope: observabilityScope,
on: .sharedConcurrent
)

guard let customContainer = container as? CustomPackageContainer else {
observabilityScope.emit(error: "invalid custom dependency container: \(container)")
return nil
Expand Down
3 changes: 1 addition & 2 deletions Sources/Workspace/Workspace+SourceControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ extension Workspace {
repository: repository,
updateStrategy: .never,
observabilityScope: observabilityScope,
delegateQueue: .sharedConcurrent,
callbackQueue: .sharedConcurrent
delegateQueue: .sharedConcurrent
)

// Clone the repository into the checkouts.
Expand Down
18 changes: 0 additions & 18 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1263,24 +1263,6 @@ extension Workspace {
return try builder.construct()
}

/// Loads a single package in the context of a previously loaded graph. This can be useful for incremental loading
/// in a longer-lived program, like an IDE.
@available(*, noasync, message: "Use the async alternative")
public func loadPackage(
with identity: PackageIdentity,
packageGraph: ModulesGraph,
observabilityScope: ObservabilityScope,
completion: @escaping @Sendable (Result<Package, Error>) -> Void
) {
DispatchQueue.sharedConcurrent.asyncResult(completion) {
try await self.loadPackage(
with: identity,
packageGraph: packageGraph,
observabilityScope: observabilityScope
)
}
}

public func changeSigningEntityFromVersion(
package: PackageIdentity,
version: Version,
Expand Down
17 changes: 6 additions & 11 deletions Sources/_InternalTestSupport/MockWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,12 @@ public final class MockWorkspace {
identity: PackageIdentity(url: url),
kind: .remoteSourceControl(url)
)
let container = try await withCheckedThrowingContinuation { continuation in
containerProvider.getContainer(
for: packageRef,
updateStrategy: .never,
observabilityScope: observability.topScope,
on: .sharedConcurrent,
completion: {
continuation.resume(with: $0)
}
)
}
let container = try await containerProvider.getContainer(
for: packageRef,
updateStrategy: .never,
observabilityScope: observability.topScope,
on: .sharedConcurrent
)
guard let customContainer = container as? CustomPackageContainer else {
throw StringError("invalid custom container: \(container)")
}
Expand Down
24 changes: 7 additions & 17 deletions Tests/BasicsTests/AsyncProcessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,21 @@ final class AsyncProcessTests: XCTestCase {
}
}

func testPopenLegacyAsync() throws {
func testPopenLegacyAsync() async throws {
#if os(Windows)
let args = ["where.exe", "where"]
let answer = "C:\\Windows\\System32\\where.exe"
#else
let args = ["whoami"]
let answer = NSUserName()
#endif
var popenResult: Result<AsyncProcessResult, Error>?
let group = DispatchGroup()
group.enter()
AsyncProcess.popen(arguments: args) { result in
popenResult = result
group.leave()
}
group.wait()
switch popenResult {
case .success(let processResult):
let output = try processResult.utf8Output()
XCTAssertTrue(output.hasPrefix(answer))
case .failure(let error):
XCTFail("error = \(error)")
case nil:
XCTFail()
let processResult = try await withCheckedThrowingContinuation { continuation in
AsyncProcess.popen(arguments: args) { result in
continuation.resume(with: result)
}
}
let output = try processResult.utf8Output()
XCTAssert(output.hasPrefix(answer))
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
Expand Down
11 changes: 3 additions & 8 deletions Tests/FunctionalTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,7 @@ final class PluginTests: XCTestCase {
)

let toolSearchDirectories = [try UserToolchain.default.swiftCompilerPath.parentDirectory]
let success = try await withCheckedThrowingContinuation { continuation in
plugin.invoke(
let success = try await plugin.invoke(
action: .performCommand(package: package, arguments: arguments),
buildEnvironment: BuildEnvironment(platform: .macOS, configuration: .debug),
scriptRunner: scriptRunner,
Expand All @@ -568,12 +567,8 @@ final class PluginTests: XCTestCase {
modulesGraph: packageGraph,
observabilityScope: observability.topScope,
callbackQueue: delegateQueue,
delegate: delegate,
completion: {
continuation.resume(with: $0)
}
)
}
delegate: delegate
)
if expectFailure {
XCTAssertFalse(success, "expected command to fail, but it succeeded", file: file, line: line)
}
Expand Down
Loading