From ddec3979d48b535ac647aec8b03d380cea0fc26a Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Tue, 16 Apr 2024 12:25:58 -0700 Subject: [PATCH] Resolve main executable symlink when looking up driver-adjacent subcommand binaries Resolves https://github.com/apple/swift/issues/70932 --- Sources/swift-driver/main.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/swift-driver/main.swift b/Sources/swift-driver/main.swift index 5f0e31fd2..e1f2f56f0 100644 --- a/Sources/swift-driver/main.swift +++ b/Sources/swift-driver/main.swift @@ -30,11 +30,13 @@ import Dispatch import WinSDK #endif -import enum TSCBasic.ProcessEnv +import struct TSCBasic.AbsolutePath import func TSCBasic.exec +import enum TSCBasic.ProcessEnv import class TSCBasic.DiagnosticsEngine import class TSCBasic.Process import class TSCBasic.ProcessSet +import func TSCBasic.resolveSymlinks import protocol TSCBasic.DiagnosticData import var TSCBasic.localFileSystem @@ -86,12 +88,17 @@ do { } let (mode, arguments) = try Driver.invocationRunMode(forArgs: CommandLine.arguments) - if case .subcommand(let subcommand) = mode { // We are running as a subcommand, try to find the subcommand adjacent to the executable we are running as. // If we didn't find the tool there, let the OS search for it. - let subcommandPath = Process.findExecutable(CommandLine.arguments[0])?.parentDirectory.appending(component: subcommand) - ?? Process.findExecutable(subcommand) + let subcommandPath: AbsolutePath? + if let executablePath = Process.findExecutable(CommandLine.arguments[0]) { + // Attempt to resolve the executable symlink in order to be able to + // resolve compiler-adjacent library locations. + subcommandPath = try TSCBasic.resolveSymlinks(executablePath).parentDirectory.appending(component: subcommand) + } else { + subcommandPath = Process.findExecutable(subcommand) + } guard let subcommandPath = subcommandPath, localFileSystem.exists(subcommandPath) else {