Skip to content

Commit db98fad

Browse files
authored
Remove temp_await [part 2] (#7845)
1 parent f5407d7 commit db98fad

File tree

56 files changed

+883
-749
lines changed

Some content is hidden

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

56 files changed

+883
-749
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct Example {
2626

2727
let package = try await workspace.loadRootPackage(at: packagePath, observabilityScope: observability.topScope)
2828

29-
let graph = try workspace.loadPackageGraph(rootPath: packagePath, observabilityScope: observability.topScope)
30-
29+
let graph = try await workspace.loadPackageGraph(rootPath: packagePath, observabilityScope: observability.topScope)
30+
3131
// EXAMPLES
3232
// ========
3333

Sources/Basics/Graph/GraphAlgorithms.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,34 @@ public func depthFirstSearch<T: Hashable>(
5555
}
5656
}
5757
}
58+
59+
public func depthFirstSearch<T: Hashable>(
60+
_ nodes: [T],
61+
successors: (T) async throws -> [T],
62+
onUnique: (T) -> Void,
63+
onDuplicate: (T, T) -> Void
64+
) async rethrows {
65+
var stack = OrderedSet<T>()
66+
var visited = Set<T>()
67+
68+
for node in nodes {
69+
precondition(stack.isEmpty)
70+
stack.append(node)
71+
72+
while !stack.isEmpty {
73+
let curr = stack.removeLast()
74+
75+
let visitResult = visited.insert(curr)
76+
if visitResult.inserted {
77+
onUnique(curr)
78+
} else {
79+
onDuplicate(visitResult.memberAfterInsert, curr)
80+
continue
81+
}
82+
83+
for succ in try await successors(curr) {
84+
stack.append(succ)
85+
}
86+
}
87+
}
88+
}

Sources/Basics/Observability.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ extension DiagnosticsEmitterProtocol {
250250
}
251251
}
252252

253+
public func trap<T>(_ closure: () async throws -> T) async -> T? {
254+
do {
255+
return try await closure()
256+
} catch Diagnostics.fatalError {
257+
// FIXME: (diagnostics) deprecate this with Diagnostics.fatalError
258+
return nil
259+
} catch {
260+
self.emit(error)
261+
return nil
262+
}
263+
}
264+
253265
/// trap a throwing closure, emitting diagnostics on error and returning boolean representing success
254266
@discardableResult
255267
public func trap(_ closure: () throws -> Void) -> Bool {
@@ -265,6 +277,20 @@ extension DiagnosticsEmitterProtocol {
265277
}
266278
}
267279

280+
@discardableResult
281+
public func trap(_ closure: () async throws -> Void) async -> Bool {
282+
do {
283+
try await closure()
284+
return true
285+
} catch Diagnostics.fatalError {
286+
// FIXME: (diagnostics) deprecate this with Diagnostics.fatalError
287+
return false
288+
} catch {
289+
self.emit(error)
290+
return false
291+
}
292+
}
293+
268294
/// If `underlyingError` is not `nil`, its human-readable description is interpolated with `message`,
269295
/// otherwise `message` itself is returned.
270296
private func makeMessage(from message: String, underlyingError: Error?) -> String {

Sources/Build/BuildOperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
266266
self.pkgConfigDirectories = pkgConfigDirectories
267267
}
268268

269+
@available(*, noasync, message: "This must only be called from a dispatch queue")
269270
public func getPackageGraph() throws -> ModulesGraph {
270271
try self.packageGraph.memoize {
271272
try self.packageGraphLoader()

Sources/Commands/PackageCommands/APIDiff.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct DeprecatedAPIDiff: ParsableCommand {
3232
}
3333
}
3434

35-
struct APIDiff: SwiftCommand {
35+
struct APIDiff: AsyncSwiftCommand {
3636
static let configuration = CommandConfiguration(
3737
commandName: "diagnose-api-breaking-changes",
3838
abstract: "Diagnose API-breaking changes to Swift modules in a package",
@@ -77,7 +77,7 @@ struct APIDiff: SwiftCommand {
7777
@Flag(help: "Regenerate the API baseline, even if an existing one is available.")
7878
var regenerateBaseline: Bool = false
7979

80-
func run(_ swiftCommandState: SwiftCommandState) throws {
80+
func run(_ swiftCommandState: SwiftCommandState) async throws {
8181
let apiDigesterPath = try swiftCommandState.getTargetToolchain().getSwiftAPIDigester()
8282
let apiDigesterTool = SwiftAPIDigester(fileSystem: swiftCommandState.fileSystem, tool: apiDigesterPath)
8383

@@ -111,7 +111,7 @@ struct APIDiff: SwiftCommand {
111111
observabilityScope: swiftCommandState.observabilityScope
112112
)
113113

114-
let baselineDir = try baselineDumper.emitAPIBaseline(
114+
let baselineDir = try await baselineDumper.emitAPIBaseline(
115115
for: modulesToDiff,
116116
at: overrideBaselineDir,
117117
force: regenerateBaseline,

Sources/Commands/PackageCommands/ArchiveSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension SwiftPackageCommand {
3838
if let output {
3939
archivePath = output
4040
} else {
41-
let graph = try swiftCommandState.loadPackageGraph()
41+
let graph = try await swiftCommandState.loadPackageGraph()
4242
let packageName = graph.rootPackages[graph.rootPackages.startIndex].manifest.displayName // TODO: use identity instead?
4343
archivePath = packageDirectory.appending("\(packageName).zip")
4444
}

Sources/Commands/PackageCommands/CompletionCommand.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import CoreCommands
1616
import var TSCBasic.stdoutStream
1717

1818
extension SwiftPackageCommand {
19-
struct CompletionCommand: SwiftCommand {
19+
struct CompletionCommand: AsyncSwiftCommand {
2020
static let configuration = CommandConfiguration(
2121
commandName: "completion-tool",
2222
abstract: "Completion command (for shell completions)"
@@ -52,7 +52,7 @@ extension SwiftPackageCommand {
5252
@Argument(help: "generate-bash-script | generate-zsh-script |\ngenerate-fish-script | list-dependencies | list-executables")
5353
var mode: Mode
5454

55-
func run(_ swiftCommandState: SwiftCommandState) throws {
55+
func run(_ swiftCommandState: SwiftCommandState) async throws {
5656
switch mode {
5757
case .generateBashScript:
5858
let script = SwiftCommand.completionScript(for: .bash)
@@ -64,7 +64,7 @@ extension SwiftPackageCommand {
6464
let script = SwiftCommand.completionScript(for: .fish)
6565
print(script)
6666
case .listDependencies:
67-
let graph = try swiftCommandState.loadPackageGraph()
67+
let graph = try await swiftCommandState.loadPackageGraph()
6868
// command's result output goes on stdout
6969
// ie "swift package list-dependencies" should output to stdout
7070
ShowDependencies.dumpDependenciesOf(
@@ -74,14 +74,14 @@ extension SwiftPackageCommand {
7474
on: TSCBasic.stdoutStream
7575
)
7676
case .listExecutables:
77-
let graph = try swiftCommandState.loadPackageGraph()
77+
let graph = try await swiftCommandState.loadPackageGraph()
7878
let package = graph.rootPackages[graph.rootPackages.startIndex].underlying
7979
let executables = package.modules.filter { $0.type == .executable }
8080
for executable in executables {
8181
print(executable.name)
8282
}
8383
case .listSnippets:
84-
let graph = try swiftCommandState.loadPackageGraph()
84+
let graph = try await swiftCommandState.loadPackageGraph()
8585
let package = graph.rootPackages[graph.rootPackages.startIndex].underlying
8686
let executables = package.modules.filter { $0.type == .snippet }
8787
for executable in executables {

Sources/Commands/PackageCommands/DumpCommands.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct DumpPackage: AsyncSwiftCommand {
131131
}
132132
}
133133

134-
struct DumpPIF: SwiftCommand {
134+
struct DumpPIF: AsyncSwiftCommand {
135135
// hides this command from CLI `--help` output
136136
static let configuration = CommandConfiguration(shouldDisplay: false)
137137

@@ -141,8 +141,8 @@ struct DumpPIF: SwiftCommand {
141141
@Flag(help: "Preserve the internal structure of PIF")
142142
var preserveStructure: Bool = false
143143

144-
func run(_ swiftCommandState: SwiftCommandState) throws {
145-
let graph = try swiftCommandState.loadPackageGraph()
144+
func run(_ swiftCommandState: SwiftCommandState) async throws {
145+
let graph = try await swiftCommandState.loadPackageGraph()
146146
let pif = try PIFBuilder.generatePIF(
147147
buildParameters: swiftCommandState.productsBuildParameters,
148148
packageGraph: graph,

Sources/Commands/PackageCommands/EditCommands.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import CoreCommands
1616
import SourceControl
1717

1818
extension SwiftPackageCommand {
19-
struct Edit: SwiftCommand {
19+
struct Edit: AsyncSwiftCommand {
2020
static let configuration = CommandConfiguration(
2121
abstract: "Put a package in editable mode")
2222

@@ -35,12 +35,12 @@ extension SwiftPackageCommand {
3535
@Argument(help: "The name of the package to edit")
3636
var packageName: String
3737

38-
func run(_ swiftCommandState: SwiftCommandState) throws {
39-
try swiftCommandState.resolve()
38+
func run(_ swiftCommandState: SwiftCommandState) async throws {
39+
try await swiftCommandState.resolve()
4040
let workspace = try swiftCommandState.getActiveWorkspace()
4141

4242
// Put the dependency in edit mode.
43-
workspace.edit(
43+
await workspace.edit(
4444
packageName: packageName,
4545
path: path,
4646
revision: revision,
@@ -50,7 +50,7 @@ extension SwiftPackageCommand {
5050
}
5151
}
5252

53-
struct Unedit: SwiftCommand {
53+
struct Unedit: AsyncSwiftCommand {
5454
static let configuration = CommandConfiguration(
5555
abstract: "Remove a package from editable mode")
5656

@@ -64,11 +64,11 @@ extension SwiftPackageCommand {
6464
@Argument(help: "The name of the package to unedit")
6565
var packageName: String
6666

67-
func run(_ swiftCommandState: SwiftCommandState) throws {
68-
try swiftCommandState.resolve()
67+
func run(_ swiftCommandState: SwiftCommandState) async throws {
68+
try await swiftCommandState.resolve()
6969
let workspace = try swiftCommandState.getActiveWorkspace()
7070

71-
try workspace.unedit(
71+
try await workspace.unedit(
7272
packageName: packageName,
7373
forceRemove: shouldForceRemove,
7474
root: swiftCommandState.getWorkspaceRoot(),

Sources/Commands/PackageCommands/Install.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import PackageModel
1818
import TSCBasic
1919

2020
extension SwiftPackageCommand {
21-
struct Install: SwiftCommand {
21+
struct Install: AsyncSwiftCommand {
2222
static let configuration = CommandConfiguration(
2323
commandName: "experimental-install",
2424
abstract: "Offers the ability to install executable products of the current package."
@@ -30,7 +30,7 @@ extension SwiftPackageCommand {
3030
@Option(help: "The name of the executable product to install")
3131
var product: String?
3232

33-
func run(_ commandState: SwiftCommandState) throws {
33+
func run(_ commandState: SwiftCommandState) async throws {
3434
let swiftpmBinDir = try commandState.fileSystem.getOrCreateSwiftPMInstalledBinariesDirectory()
3535

3636
let env = Environment.current
@@ -49,7 +49,7 @@ extension SwiftPackageCommand {
4949
let workspace = try commandState.getActiveWorkspace()
5050
let packageRoot = try commandState.getPackageRoot()
5151

52-
let packageGraph = try workspace.loadPackageGraph(
52+
let packageGraph = try await workspace.loadPackageGraph(
5353
rootPath: packageRoot,
5454
observabilityScope: commandState.observabilityScope
5555
)

0 commit comments

Comments
 (0)