Skip to content

Commit ff43faa

Browse files
committed
Add async versions of UserToolchain.init and SwiftSDK.hostSwiftSDK
Both methods previously made calls to the version of `AsyncProcess.checkNonZeroExit` that blocks the calling thread, forcing consumers to either block or implement workarounds to hop off the calling thread. This patch adds async versions of these methods and ports most usages to use them, marking the required call sites as async and awaiting them.
1 parent b16bb8f commit ff43faa

File tree

88 files changed

+1036
-914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1036
-914
lines changed

Examples/package-info/Sources/package-info/example.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Example {
2020

2121
let observability = ObservabilitySystem({ print("\($0): \($1)") })
2222

23-
let workspace = try Workspace(forRootPackage: packagePath)
23+
let workspace = try await Workspace(forRootPackage: packagePath)
2424

2525
let manifest = try await workspace.loadRootManifest(at: packagePath, observabilityScope: observability.topScope)
2626

Sources/Commands/PackageCommands/APIDiff.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ struct APIDiff: AsyncSwiftCommand {
8484
let repository = GitRepository(path: packageRoot)
8585
let baselineRevision = try repository.resolveRevision(identifier: treeish)
8686

87-
let baselineDir = try overrideBaselineDir?.appending(component: baselineRevision.identifier) ?? swiftCommandState.productsBuildParameters.apiDiff.appending(component: "\(baselineRevision.identifier)-baselines")
87+
let baselineDir: Basics.AbsolutePath
88+
if let overrideBaselineDir {
89+
baselineDir = overrideBaselineDir.appending(component: baselineRevision.identifier)
90+
} else {
91+
baselineDir = try await swiftCommandState.productsBuildParameters.apiDiff.appending(component: "\(baselineRevision.identifier)-baselines")
92+
}
8893
let packageGraph = try await swiftCommandState.loadPackageGraph()
8994
let modulesToDiff = try Self.determineModulesToDiff(
9095
packageGraph: packageGraph,
@@ -115,7 +120,7 @@ struct APIDiff: AsyncSwiftCommand {
115120
}
116121

117122
private func runWithSwiftPMCoordinatedDiffing(_ swiftCommandState: SwiftCommandState, buildSystem: any BuildSystem, baselineRevision: Revision, modulesToDiff: Set<String>) async throws {
118-
let apiDigesterPath = try swiftCommandState.getTargetToolchain().getSwiftAPIDigester()
123+
let apiDigesterPath = try await swiftCommandState.getTargetToolchain().getSwiftAPIDigester()
119124
let apiDigesterTool = SwiftAPIDigester(fileSystem: swiftCommandState.fileSystem, tool: apiDigesterPath)
120125

121126
// Build the current package.
@@ -198,7 +203,7 @@ struct APIDiff: AsyncSwiftCommand {
198203
let modulesWithBaselines = try await generateAPIBaselineUsingIntegratedAPIDigesterSupport(swiftCommandState, baselineRevision: baselineRevision, baselineDir: baselineDir, modulesNeedingBaselines: modulesToDiff)
199204

200205
// Build the package and run a comparison agains the baselines.
201-
var productsBuildParameters = try swiftCommandState.productsBuildParameters
206+
var productsBuildParameters = try await swiftCommandState.productsBuildParameters
202207
productsBuildParameters.apiDigesterMode = .compareToBaselines(
203208
baselinesDirectory: baselineDir,
204209
modulesToCompare: modulesWithBaselines,
@@ -258,7 +263,7 @@ struct APIDiff: AsyncSwiftCommand {
258263

259264
private func generateAPIBaselineUsingIntegratedAPIDigesterSupport(_ swiftCommandState: SwiftCommandState, baselineRevision: Revision, baselineDir: Basics.AbsolutePath, modulesNeedingBaselines: Set<String>) async throws -> Set<String> {
260265
// Setup a temporary directory where we can checkout and build the baseline treeish.
261-
let baselinePackageRoot = try swiftCommandState.productsBuildParameters.apiDiff.appending("\(baselineRevision.identifier)-checkout")
266+
let baselinePackageRoot = try await swiftCommandState.productsBuildParameters.apiDiff.appending("\(baselineRevision.identifier)-checkout")
262267
if swiftCommandState.fileSystem.exists(baselinePackageRoot) {
263268
try swiftCommandState.fileSystem.removeFileTree(baselinePackageRoot)
264269
}
@@ -279,7 +284,7 @@ struct APIDiff: AsyncSwiftCommand {
279284
try workingCopy.checkout(revision: baselineRevision)
280285

281286
// Create the workspace for this package.
282-
let workspace = try Workspace(
287+
let workspace = try await Workspace(
283288
forRootPackage: baselinePackageRoot,
284289
cancellator: swiftCommandState.cancellator
285290
)
@@ -307,7 +312,7 @@ struct APIDiff: AsyncSwiftCommand {
307312
}
308313

309314
// Update the data path input build parameters so it's built in the sandbox.
310-
var productsBuildParameters = try swiftCommandState.productsBuildParameters
315+
var productsBuildParameters = try await swiftCommandState.productsBuildParameters
311316
productsBuildParameters.dataPath = workspace.location.scratchDirectory
312317
productsBuildParameters.apiDigesterMode = .generateBaselines(baselinesDirectory: baselineDir, modulesRequestingBaselines: modulesNeedingBaselines)
313318

Sources/Commands/PackageCommands/AddDependency.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Workspace
2525
import class PackageModel.Manifest
2626

2727
extension SwiftPackageCommand {
28-
struct AddDependency: SwiftCommand {
28+
struct AddDependency: AsyncSwiftCommand {
2929
package static let configuration = CommandConfiguration(
3030
abstract: "Add a package dependency to the manifest."
3131
)
@@ -63,8 +63,8 @@ extension SwiftPackageCommand {
6363
case registry
6464
}
6565

66-
func run(_ swiftCommandState: SwiftCommandState) throws {
67-
let workspace = try swiftCommandState.getActiveWorkspace()
66+
func run(_ swiftCommandState: SwiftCommandState) async throws {
67+
let workspace = try await swiftCommandState.getActiveWorkspace()
6868
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
6969
throw StringError("unknown package")
7070
}

Sources/Commands/PackageCommands/AddProduct.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import TSCUtility
2323
import Workspace
2424

2525
extension SwiftPackageCommand {
26-
struct AddProduct: SwiftCommand {
26+
struct AddProduct: AsyncSwiftCommand {
2727
/// The package product type used for the command-line. This is a
2828
/// subset of `ProductType` that expands out the library types.
2929
enum CommandProductType: String, Codable, ExpressibleByArgument, CaseIterable {
@@ -52,8 +52,8 @@ extension SwiftPackageCommand {
5252
)
5353
var targets: [String] = []
5454

55-
func run(_ swiftCommandState: SwiftCommandState) throws {
56-
let workspace = try swiftCommandState.getActiveWorkspace()
55+
func run(_ swiftCommandState: SwiftCommandState) async throws {
56+
let workspace = try await swiftCommandState.getActiveWorkspace()
5757

5858
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
5959
throw StringError("unknown package")

Sources/Commands/PackageCommands/AddSetting.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import TSCUtility
2525
import Workspace
2626

2727
extension SwiftPackageCommand {
28-
struct AddSetting: SwiftCommand {
28+
struct AddSetting: AsyncSwiftCommand {
2929
/// The Swift language setting that can be specified on the command line.
3030
enum SwiftSetting: String, Codable, ExpressibleByArgument, CaseIterable {
3131
case experimentalFeature
@@ -68,9 +68,9 @@ extension SwiftPackageCommand {
6868
}
6969
}
7070

71-
func run(_ swiftCommandState: SwiftCommandState) throws {
71+
func run(_ swiftCommandState: SwiftCommandState) async throws {
7272
if !self._swiftSettings.isEmpty {
73-
try Self.editSwiftSettings(
73+
try await Self.editSwiftSettings(
7474
of: self.target,
7575
using: swiftCommandState,
7676
self.swiftSettings,
@@ -84,8 +84,8 @@ extension SwiftPackageCommand {
8484
using swiftCommandState: SwiftCommandState,
8585
_ settings: [(SwiftSetting, String)],
8686
verbose: Bool = false
87-
) throws {
88-
let workspace = try swiftCommandState.getActiveWorkspace()
87+
) async throws {
88+
let workspace = try await swiftCommandState.getActiveWorkspace()
8989
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
9090
throw StringError("unknown package")
9191
}

Sources/Commands/PackageCommands/AddTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extension SwiftPackageCommand {
7575
var testingLibrary: AddPackageTarget.TestHarness = .default
7676

7777
func run(_ swiftCommandState: SwiftCommandState) async throws {
78-
let workspace = try swiftCommandState.getActiveWorkspace()
78+
let workspace = try await swiftCommandState.getActiveWorkspace()
7979

8080
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
8181
throw StringError("unknown package")

Sources/Commands/PackageCommands/AddTargetDependency.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import TSCUtility
2424
import Workspace
2525

2626
extension SwiftPackageCommand {
27-
struct AddTargetDependency: SwiftCommand {
27+
struct AddTargetDependency: AsyncSwiftCommand {
2828
package static let configuration = CommandConfiguration(
2929
abstract: "Add a new target dependency to the manifest.")
3030

@@ -40,8 +40,8 @@ extension SwiftPackageCommand {
4040
@Option(help: "The package in which the dependency resides.")
4141
var package: String?
4242

43-
func run(_ swiftCommandState: SwiftCommandState) throws {
44-
let workspace = try swiftCommandState.getActiveWorkspace()
43+
func run(_ swiftCommandState: SwiftCommandState) async throws {
44+
let workspace = try await swiftCommandState.getActiveWorkspace()
4545

4646
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
4747
throw StringError("unknown package")

Sources/Commands/PackageCommands/AuditBinaryArtifact.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct AuditBinaryArtifact: AsyncSwiftCommand {
3434
var path: AbsolutePath
3535

3636
func run(_ swiftCommandState: SwiftCommandState) async throws {
37-
let hostToolchain = try swiftCommandState.getHostToolchain()
37+
let hostToolchain = try await swiftCommandState.getHostToolchain()
3838
let clang = try hostToolchain.getClangCompiler()
3939
let objdump = try hostToolchain.getLLVMObjdump()
4040
let hostTriple = try Triple.getVersionedHostTriple(

Sources/Commands/PackageCommands/Config.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension SwiftPackageCommand {
2828
}
2929

3030
extension SwiftPackageCommand.Config {
31-
struct SetMirror: SwiftCommand {
31+
struct SetMirror: AsyncSwiftCommand {
3232
static let configuration = CommandConfiguration(
3333
abstract: "Set a mirror for a dependency."
3434
)
@@ -51,8 +51,8 @@ extension SwiftPackageCommand.Config {
5151
@Option(help: "The mirror url or identity.")
5252
var mirror: String?
5353

54-
func run(_ swiftCommandState: SwiftCommandState) throws {
55-
let config = try getMirrorsConfig(swiftCommandState)
54+
func run(_ swiftCommandState: SwiftCommandState) async throws {
55+
let config = try await getMirrorsConfig(swiftCommandState)
5656

5757
if self._deprecate_packageURL != nil {
5858
swiftCommandState.observabilityScope.emit(
@@ -86,7 +86,7 @@ extension SwiftPackageCommand.Config {
8686
}
8787
}
8888

89-
struct UnsetMirror: SwiftCommand {
89+
struct UnsetMirror: AsyncSwiftCommand {
9090
static let configuration = CommandConfiguration(
9191
abstract: "Remove an existing mirror."
9292
)
@@ -109,8 +109,8 @@ extension SwiftPackageCommand.Config {
109109
@Option(help: "The mirror url or identity.")
110110
var mirror: String?
111111

112-
func run(_ swiftCommandState: SwiftCommandState) throws {
113-
let config = try getMirrorsConfig(swiftCommandState)
112+
func run(_ swiftCommandState: SwiftCommandState) async throws {
113+
let config = try await getMirrorsConfig(swiftCommandState)
114114

115115
if self._deprecate_packageURL != nil {
116116
swiftCommandState.observabilityScope.emit(
@@ -141,7 +141,7 @@ extension SwiftPackageCommand.Config {
141141
}
142142
}
143143

144-
struct GetMirror: SwiftCommand {
144+
struct GetMirror: AsyncSwiftCommand {
145145
static let configuration = CommandConfiguration(
146146
abstract: "Print mirror configuration for the given package dependency."
147147
)
@@ -157,8 +157,8 @@ extension SwiftPackageCommand.Config {
157157
@Option(help: "The original url or identity.")
158158
var original: String?
159159

160-
func run(_ swiftCommandState: SwiftCommandState) throws {
161-
let config = try getMirrorsConfig(swiftCommandState)
160+
func run(_ swiftCommandState: SwiftCommandState) async throws {
161+
let config = try await getMirrorsConfig(swiftCommandState)
162162

163163
if self._deprecate_packageURL != nil {
164164
swiftCommandState.observabilityScope.emit(
@@ -186,8 +186,8 @@ extension SwiftPackageCommand.Config {
186186
}
187187
}
188188

189-
static func getMirrorsConfig(_ swiftCommandState: SwiftCommandState) throws -> Workspace.Configuration.Mirrors {
190-
let workspace = try swiftCommandState.getActiveWorkspace()
189+
static func getMirrorsConfig(_ swiftCommandState: SwiftCommandState) async throws -> Workspace.Configuration.Mirrors {
190+
let workspace = try await swiftCommandState.getActiveWorkspace()
191191
return try .init(
192192
fileSystem: swiftCommandState.fileSystem,
193193
localMirrorsFile: workspace.location.localMirrorsConfigurationFile,

Sources/Commands/PackageCommands/Describe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension SwiftPackageCommand {
3232
var type: DescribeMode = .text
3333

3434
func run(_ swiftCommandState: SwiftCommandState) async throws {
35-
let workspace = try swiftCommandState.getActiveWorkspace()
35+
let workspace = try await swiftCommandState.getActiveWorkspace()
3636

3737
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
3838
throw StringError("unknown package")

0 commit comments

Comments
 (0)