Skip to content

Commit d6d3d24

Browse files
committed
Report a better error message when retrieving frontend target info fails
1 parent fba6877 commit d6d3d24

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public struct Driver {
2525
case integratedReplRemoved
2626
case conflictingOptions(Option, Option)
2727
case unableToLoadOutputFileMap(String)
28+
case unableToDecodeFrontendTargetInfo
29+
case failedToRetrieveFrontendTargetInfo
2830
// Explicit Module Build Failures
2931
case malformedModuleDependency(String, String)
3032
case missingPCMArguments(String)
@@ -49,6 +51,10 @@ public struct Driver {
4951
return "Compiler-internal integrated REPL has been removed; use the LLDB-enhanced REPL instead."
5052
case .conflictingOptions(let one, let two):
5153
return "conflicting options '\(one.spelling)' and '\(two.spelling)'"
54+
case .unableToDecodeFrontendTargetInfo:
55+
return "could not decode frontend target info; compiler driver and frontend executables may be incompatible"
56+
case .failedToRetrieveFrontendTargetInfo:
57+
return "failed to retrieve frontend target info"
5258
// Explicit Module Build Failures
5359
case .malformedModuleDependency(let moduleName, let errorDescription):
5460
return "Malformed Module Dependency: \(moduleName), \(errorDescription)"
@@ -1639,7 +1645,8 @@ extension Driver {
16391645
diagnosticsEngine: diagnosticsEngine, env: env)
16401646

16411647
// Query the frontend to for target information.
1642-
var info = try executor.execute(
1648+
do {
1649+
var info = try executor.execute(
16431650
job: toolchain.printTargetInfoJob(
16441651
target: explicitTarget, targetVariant: explicitTargetVariant,
16451652
sdkPath: sdkPath, resourceDirPath: resourceDirPath,
@@ -1650,21 +1657,27 @@ extension Driver {
16501657
forceResponseFiles: false,
16511658
recordedInputModificationDates: [:])
16521659

1653-
// Parse the runtime compatibility version. If present, it will override
1654-
// what is reported by the frontend.
1655-
if let versionString =
1656-
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle {
1657-
if let version = SwiftVersion(string: versionString) {
1658-
info.target.swiftRuntimeCompatibilityVersion = version
1659-
info.targetVariant?.swiftRuntimeCompatibilityVersion = version
1660-
} else {
1661-
diagnosticsEngine.emit(
1662-
.error_invalid_arg_value(
1663-
arg: .runtimeCompatibilityVersion, value: versionString))
1660+
1661+
// Parse the runtime compatibility version. If present, it will override
1662+
// what is reported by the frontend.
1663+
if let versionString =
1664+
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle {
1665+
if let version = SwiftVersion(string: versionString) {
1666+
info.target.swiftRuntimeCompatibilityVersion = version
1667+
info.targetVariant?.swiftRuntimeCompatibilityVersion = version
1668+
} else {
1669+
diagnosticsEngine.emit(
1670+
.error_invalid_arg_value(
1671+
arg: .runtimeCompatibilityVersion, value: versionString))
1672+
}
16641673
}
1665-
}
16661674

1667-
return (toolchain, info, swiftCompilerPrefixArgs)
1675+
return (toolchain, info, swiftCompilerPrefixArgs)
1676+
} catch is DecodingError {
1677+
throw Error.unableToDecodeFrontendTargetInfo
1678+
} catch {
1679+
throw Error.failedToRetrieveFrontendTargetInfo
1680+
}
16681681
}
16691682
}
16701683

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,37 @@ final class SwiftDriverTests: XCTestCase {
17471747
XCTAssertTrue(job.commandLine.contains(.flag("-resource-dir")))
17481748
}
17491749

1750+
do {
1751+
struct MockExecutor: DriverExecutor {
1752+
func execute(job: Job, forceResponseFiles: Bool, recordedInputModificationDates: [TypedVirtualPath : Date]) throws -> ProcessResult {
1753+
return ProcessResult(arguments: [], environment: [:], exitStatus: .terminated(code: 0), output: .success(Array("bad JSON".utf8)), stderrOutput: .success([]))
1754+
}
1755+
func execute(jobs: [Job], delegate: JobExecutionDelegate, numParallelJobs: Int, forceResponseFiles: Bool, recordedInputModificationDates: [TypedVirtualPath : Date]) throws {
1756+
fatalError()
1757+
}
1758+
func checkNonZeroExit(args: String..., environment: [String : String]) throws -> String {
1759+
return try Process.checkNonZeroExit(arguments: args, environment: environment)
1760+
}
1761+
func description(of job: Job, forceResponseFiles: Bool) throws -> String {
1762+
fatalError()
1763+
}
1764+
}
1765+
1766+
XCTAssertThrowsError(try Driver(args: ["swift", "-print-target-info"],
1767+
executor: MockExecutor())) {
1768+
error in
1769+
XCTAssertEqual(error as? Driver.Error, .unableToDecodeFrontendTargetInfo)
1770+
}
1771+
}
1772+
1773+
do {
1774+
XCTAssertThrowsError(try Driver(args: ["swift", "-print-target-info"],
1775+
env: ["SWIFT_DRIVER_SWIFT_FRONTEND_EXEC": "/bad/path/to/swift-frontend"])) {
1776+
error in
1777+
XCTAssertEqual(error as? Driver.Error, .failedToRetrieveFrontendTargetInfo)
1778+
}
1779+
}
1780+
17501781
do {
17511782
var driver = try Driver(args: ["swift", "-print-target-info", "-target", "x86_64-apple-ios13.0-macabi", "-target-variant", "x86_64-apple-macosx10.14", "-sdk", "bar", "-resource-dir", "baz"])
17521783
let plannedJobs = try driver.planBuild()

0 commit comments

Comments
 (0)