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

+2-2
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

+31
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

+26
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

+1
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

+3-3
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

+1-1
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

+5-5
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

+3-3
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

+8-8
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

+3-3
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
)

Sources/Commands/PackageCommands/Learn.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import PackageGraph
1717
import PackageModel
1818

1919
extension SwiftPackageCommand {
20-
struct Learn: SwiftCommand {
20+
struct Learn: AsyncSwiftCommand {
2121

2222
@OptionGroup()
2323
var globalOptions: GlobalOptions
@@ -90,8 +90,8 @@ extension SwiftPackageCommand {
9090
return snippetGroups.filter { !$0.snippets.isEmpty }
9191
}
9292

93-
func run(_ swiftCommandState: SwiftCommandState) throws {
94-
let graph = try swiftCommandState.loadPackageGraph()
93+
func run(_ swiftCommandState: SwiftCommandState) async throws {
94+
let graph = try await swiftCommandState.loadPackageGraph()
9595
let package = graph.rootPackages[graph.rootPackages.startIndex]
9696
print(package.products.map { $0.description })
9797

Sources/Commands/PackageCommands/PluginCommand.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct PluginCommand: AsyncSwiftCommand {
145145

146146
// List the available plugins, if asked to.
147147
if self.listCommands {
148-
let packageGraph = try swiftCommandState.loadPackageGraph()
148+
let packageGraph = try await swiftCommandState.loadPackageGraph()
149149
let allPlugins = PluginCommand.availableCommandPlugins(
150150
in: packageGraph,
151151
limitedTo: self.pluginOptions.packageIdentity
@@ -181,7 +181,7 @@ struct PluginCommand: AsyncSwiftCommand {
181181
swiftCommandState: SwiftCommandState
182182
) async throws {
183183
// Load the workspace and resolve the package graph.
184-
let packageGraph = try swiftCommandState.loadPackageGraph()
184+
let packageGraph = try await swiftCommandState.loadPackageGraph()
185185

186186
swiftCommandState.observabilityScope.emit(info: "Finding plugin for command ‘\(command)")
187187
let matchingPlugins = PluginCommand.findPlugins(matching: command, in: packageGraph, limitedTo: options.packageIdentity)

Sources/Commands/PackageCommands/Resolve.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension SwiftPackageCommand {
2929
var packageName: String?
3030
}
3131

32-
struct Resolve: SwiftCommand {
32+
struct Resolve: AsyncSwiftCommand {
3333
static let configuration = CommandConfiguration(
3434
abstract: "Resolve package dependencies")
3535

@@ -39,11 +39,11 @@ extension SwiftPackageCommand {
3939
@OptionGroup()
4040
var resolveOptions: ResolveOptions
4141

42-
func run(_ swiftCommandState: SwiftCommandState) throws {
42+
func run(_ swiftCommandState: SwiftCommandState) async throws {
4343
// If a package is provided, use that to resolve the dependencies.
4444
if let packageName = resolveOptions.packageName {
4545
let workspace = try swiftCommandState.getActiveWorkspace()
46-
try workspace.resolve(
46+
try await workspace.resolve(
4747
packageName: packageName,
4848
root: swiftCommandState.getWorkspaceRoot(),
4949
version: resolveOptions.version,
@@ -56,12 +56,12 @@ extension SwiftPackageCommand {
5656
}
5757
} else {
5858
// Otherwise, run a normal resolve.
59-
try swiftCommandState.resolve()
59+
try await swiftCommandState.resolve()
6060
}
6161
}
6262
}
6363

64-
struct Fetch: SwiftCommand {
64+
struct Fetch: AsyncSwiftCommand {
6565
static let configuration = CommandConfiguration(shouldDisplay: false)
6666

6767
@OptionGroup(visibility: .hidden)
@@ -70,11 +70,11 @@ extension SwiftPackageCommand {
7070
@OptionGroup()
7171
var resolveOptions: ResolveOptions
7272

73-
func run(_ swiftCommandState: SwiftCommandState) throws {
73+
func run(_ swiftCommandState: SwiftCommandState) async throws {
7474
swiftCommandState.observabilityScope.emit(warning: "'fetch' command is deprecated; use 'resolve' instead")
7575

7676
let resolveCommand = Resolve(globalOptions: _globalOptions, resolveOptions: _resolveOptions)
77-
try resolveCommand.run(swiftCommandState)
77+
try await resolveCommand.run(swiftCommandState)
7878
}
7979
}
8080
}

Sources/Commands/PackageCommands/ShowDependencies.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import protocol TSCBasic.OutputByteStream
2121
import var TSCBasic.stdoutStream
2222

2323
extension SwiftPackageCommand {
24-
struct ShowDependencies: SwiftCommand {
24+
struct ShowDependencies: AsyncSwiftCommand {
2525
static let configuration = CommandConfiguration(
2626
abstract: "Print the resolved dependency graph")
2727

@@ -35,8 +35,8 @@ extension SwiftPackageCommand {
3535
help: "The absolute or relative path to output the resolved dependency graph.")
3636
var outputPath: AbsolutePath?
3737

38-
func run(_ swiftCommandState: SwiftCommandState) throws {
39-
let graph = try swiftCommandState.loadPackageGraph()
38+
func run(_ swiftCommandState: SwiftCommandState) async throws {
39+
let graph = try await swiftCommandState.loadPackageGraph()
4040
// command's result output goes on stdout
4141
// ie "swift package show-dependencies" should output to stdout
4242
let stream: OutputByteStream = try outputPath.map { try LocalFileOutputByteStream($0) } ?? TSCBasic.stdoutStream

0 commit comments

Comments
 (0)