Skip to content

Commit 44239e6

Browse files
committed
swift-driver: correct the legacy driver path construction
Use `URL` to compute the new executable name. This is important to ensure that we correctly strip the executable suffix and re-apply it in the proper location if applicable. Without this change, on Windows, the fallback path would generate `swiftc.exe-legacy-driver` rather than `swiftc-legacy-driver.exe` for the basename of the C++ driver.
1 parent 1f5cea2 commit 44239e6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Sources/swift-driver/main.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Android
2525
#endif
2626

2727
import Dispatch
28+
import Foundation
2829

2930
#if os(Windows)
3031
import WinSDK
@@ -73,13 +74,18 @@ do {
7374
// Fallback to legacy driver if forced to
7475
if CommandLine.arguments.contains(Option.disallowForwardingDriver.spelling) ||
7576
ProcessEnv.vars["SWIFT_USE_OLD_DRIVER"] != nil {
76-
if let legacyExecutablePath = Process.findExecutable(CommandLine.arguments[0] + "-legacy-driver"),
77-
localFileSystem.exists(legacyExecutablePath) {
78-
let legacyDriverCommand = [legacyExecutablePath.pathString] +
79-
CommandLine.arguments[1...]
80-
try exec(path: legacyExecutablePath.pathString, args: legacyDriverCommand)
77+
let executable: URL = URL(fileURLWithPath: CommandLine.arguments[0])
78+
let legacyExecutablePath: URL =
79+
executable.deletingLastPathComponent()
80+
.appendingPathComponent("\(executable.deletingPathExtension().lastPathComponent)-legacy-driver")
81+
.appendingPathExtension(executable.pathExtension)
82+
let path: String = legacyExecutablePath.withUnsafeFileSystemRepresentation { String(cString: $0!) }
83+
84+
if localFileSystem.exists(AbsolutePath(path)) {
85+
let legacyDriverCommand = [path] + CommandLine.arguments[1...]
86+
try exec(path: path, args: legacyDriverCommand)
8187
} else {
82-
throw Driver.Error.unknownOrMissingSubcommand(CommandLine.arguments[0] + "-legacy-driver")
88+
throw Driver.Error.unknownOrMissingSubcommand(legacyExecutablePath.lastPathComponent)
8389
}
8490
}
8591

0 commit comments

Comments
 (0)