Skip to content

Report a better error message when retrieving target info from the frontend fails #195

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
Aug 10, 2020
Merged
Show file tree
Hide file tree
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
41 changes: 27 additions & 14 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public struct Driver {
case integratedReplRemoved
case conflictingOptions(Option, Option)
case unableToLoadOutputFileMap(String)
case unableToDecodeFrontendTargetInfo
case failedToRetrieveFrontendTargetInfo
// Explicit Module Build Failures
case malformedModuleDependency(String, String)
case missingPCMArguments(String)
Expand All @@ -49,6 +51,10 @@ public struct Driver {
return "Compiler-internal integrated REPL has been removed; use the LLDB-enhanced REPL instead."
case .conflictingOptions(let one, let two):
return "conflicting options '\(one.spelling)' and '\(two.spelling)'"
case .unableToDecodeFrontendTargetInfo:
return "could not decode frontend target info; compiler driver and frontend executables may be incompatible"
case .failedToRetrieveFrontendTargetInfo:
return "failed to retrieve frontend target info"
// Explicit Module Build Failures
case .malformedModuleDependency(let moduleName, let errorDescription):
return "Malformed Module Dependency: \(moduleName), \(errorDescription)"
Expand Down Expand Up @@ -1639,7 +1645,8 @@ extension Driver {
diagnosticsEngine: diagnosticsEngine, env: env)

// Query the frontend to for target information.
var info = try executor.execute(
do {
var info = try executor.execute(
job: toolchain.printTargetInfoJob(
target: explicitTarget, targetVariant: explicitTargetVariant,
sdkPath: sdkPath, resourceDirPath: resourceDirPath,
Expand All @@ -1650,21 +1657,27 @@ extension Driver {
forceResponseFiles: false,
recordedInputModificationDates: [:])

// Parse the runtime compatibility version. If present, it will override
// what is reported by the frontend.
if let versionString =
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle {
if let version = SwiftVersion(string: versionString) {
info.target.swiftRuntimeCompatibilityVersion = version
info.targetVariant?.swiftRuntimeCompatibilityVersion = version
} else {
diagnosticsEngine.emit(
.error_invalid_arg_value(
arg: .runtimeCompatibilityVersion, value: versionString))

// Parse the runtime compatibility version. If present, it will override
// what is reported by the frontend.
if let versionString =
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle {
if let version = SwiftVersion(string: versionString) {
info.target.swiftRuntimeCompatibilityVersion = version
info.targetVariant?.swiftRuntimeCompatibilityVersion = version
} else {
diagnosticsEngine.emit(
.error_invalid_arg_value(
arg: .runtimeCompatibilityVersion, value: versionString))
}
}
}

return (toolchain, info, swiftCompilerPrefixArgs)
return (toolchain, info, swiftCompilerPrefixArgs)
} catch is DecodingError {
throw Error.unableToDecodeFrontendTargetInfo
} catch {
throw Error.failedToRetrieveFrontendTargetInfo
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,37 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertTrue(job.commandLine.contains(.flag("-resource-dir")))
}

do {
struct MockExecutor: DriverExecutor {
func execute(job: Job, forceResponseFiles: Bool, recordedInputModificationDates: [TypedVirtualPath : Date]) throws -> ProcessResult {
return ProcessResult(arguments: [], environment: [:], exitStatus: .terminated(code: 0), output: .success(Array("bad JSON".utf8)), stderrOutput: .success([]))
}
func execute(jobs: [Job], delegate: JobExecutionDelegate, numParallelJobs: Int, forceResponseFiles: Bool, recordedInputModificationDates: [TypedVirtualPath : Date]) throws {
fatalError()
}
func checkNonZeroExit(args: String..., environment: [String : String]) throws -> String {
return try Process.checkNonZeroExit(arguments: args, environment: environment)
}
func description(of job: Job, forceResponseFiles: Bool) throws -> String {
fatalError()
}
}

XCTAssertThrowsError(try Driver(args: ["swift", "-print-target-info"],
executor: MockExecutor())) {
error in
XCTAssertEqual(error as? Driver.Error, .unableToDecodeFrontendTargetInfo)
}
}

do {
XCTAssertThrowsError(try Driver(args: ["swift", "-print-target-info"],
env: ["SWIFT_DRIVER_SWIFT_FRONTEND_EXEC": "/bad/path/to/swift-frontend"])) {
error in
XCTAssertEqual(error as? Driver.Error, .failedToRetrieveFrontendTargetInfo)
}
}

do {
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"])
let plannedJobs = try driver.planBuild()
Expand Down