Skip to content

Improve failure message for backdeployed AsyncParsableCommand #547

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

Merged
merged 1 commit into from
Jan 27, 2023
Merged
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
60 changes: 59 additions & 1 deletion Sources/ArgumentParser/Parsable Types/ParsableCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ extension ParsableCommand {

#if DEBUG
if #available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *) {
checkAsyncHierarchy(self, root: "\(self)")
if let asyncCommand = firstAsyncSubcommand(self) {
if Self() is AsyncParsableCommand {
failAsyncPlatform(rootCommand: self)
} else {
failAsyncHierarchy(rootCommand: self, subCommand: asyncCommand)
}
}
}
#endif

Expand Down Expand Up @@ -194,5 +200,57 @@ extension ParsableCommand {
""".wrapped(to: 70))
}
}

@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
internal static func firstAsyncSubcommand(_ command: ParsableCommand.Type) -> AsyncParsableCommand.Type? {
for sub in command.configuration.subcommands {
if let asyncCommand = sub as? AsyncParsableCommand.Type,
sub.configuration.subcommands.isEmpty
{
return asyncCommand
}

if let asyncCommand = firstAsyncSubcommand(sub) {
return asyncCommand
}
}

return nil
}
#endif
}

// MARK: Async Configuration Errors

func failAsyncHierarchy(
rootCommand: ParsableCommand.Type, subCommand: ParsableCommand.Type
) -> Never {
fatalError("""

--------------------------------------------------------------------
Asynchronous subcommand of a synchronous root.

The asynchronous command `\(subCommand)` is declared as a subcommand of the synchronous root command `\(rootCommand)`.

With this configuration, your asynchronous `run()` method will not be called. To fix this issue, change `\(rootCommand)`'s `ParsableCommand` conformance to `AsyncParsableCommand`.
--------------------------------------------------------------------

""".wrapped(to: 70))
}

func failAsyncPlatform(rootCommand: ParsableCommand.Type) -> Never {
fatalError("""

--------------------------------------------------------------------
Asynchronous root command needs availability annotation.

The asynchronous root command `\(rootCommand)` needs an availability annotation in order to be executed asynchronously. To fix this issue, add the following availability attribute to your `\(rootCommand)` declaration or set the minimum platform in your "Package.swift" file.

""".wrapped(to: 70)
+ """

@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
--------------------------------------------------------------------

""")
}