Skip to content

Commit 387cb78

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 ec430b8 commit 387cb78

File tree

89 files changed

+1023
-906
lines changed

Some content is hidden

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

89 files changed

+1023
-906
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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/ComputeChecksum.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct ComputeChecksum: SwiftCommand {
2727
@Argument(help: "The absolute or relative path to the binary artifact.")
2828
var path: AbsolutePath
2929

30-
func run(_ swiftCommandState: SwiftCommandState) throws {
30+
func run(_ swiftCommandState: SwiftCommandState) async throws {
3131
let checksum = try Workspace.BinaryArtifactsManager.checksum(
3232
forBinaryArtifactAt: self.path,
3333
fileSystem: swiftCommandState.fileSystem

Sources/Commands/PackageCommands/Config.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
@@ -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(
@@ -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,

0 commit comments

Comments
 (0)