Skip to content

Commit a286293

Browse files
committed
swift-package-migrate: Emit feature validation errors through the diagnostic emitter
These errors do not represent issues with parsing command-line arguments, so throwing a `ValidationError` is not suitable — we don't want to present the user with a command usage hint in these cases.
1 parent 8486081 commit a286293

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Sources/Commands/PackageCommands/Migrate.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ extension SwiftPackageCommand {
6565

6666
public func run(_ swiftCommandState: SwiftCommandState) async throws {
6767
// First, validate and resolve the requested feature names.
68-
let features = try self.resolveRequestedFeatures(swiftCommandState)
68+
guard let features = try self.resolveRequestedFeatures(swiftCommandState) else {
69+
return
70+
}
6971

7072
let buildSystem = try await createBuildSystem(
7173
swiftCommandState,
@@ -166,9 +168,12 @@ extension SwiftPackageCommand {
166168
}
167169

168170
/// Resolves the requested feature names.
171+
///
172+
/// - Returns: An array of resolved features, or `nil` if an error was
173+
/// emitted.
169174
private func resolveRequestedFeatures(
170175
_ swiftCommandState: SwiftCommandState
171-
) throws -> [SwiftCompilerFeature] {
176+
) throws -> [SwiftCompilerFeature]? {
172177
let toolchain = try swiftCommandState.productsBuildParameters.toolchain
173178

174179
// Query the compiler for supported features.
@@ -188,13 +193,15 @@ extension SwiftPackageCommand {
188193
.sorted()
189194
.joined(separator: ", ")
190195

191-
throw ValidationError(
192-
"Unsupported feature '\(name)'. Available features: \(migratableCommaSeparatedFeatures)"
196+
swiftCommandState.observabilityScope.emit(
197+
error: "Unsupported feature '\(name)'. Available features: \(migratableCommaSeparatedFeatures)"
193198
)
199+
return nil
194200
}
195201

196202
guard feature.migratable else {
197-
throw ValidationError("Feature '\(name)' is not migratable")
203+
swiftCommandState.observabilityScope.emit(error: "Feature '\(name)' is not migratable")
204+
return nil
198205
}
199206

200207
resolvedFeatures.append(feature)

0 commit comments

Comments
 (0)