From c52714c64a2c59af4e0c9c0130539634745768f8 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 f16055ede..2c9f805a1 100644 --- a/Sources/swift-driver/main.swift +++ b/Sources/swift-driver/main.swift @@ -28,11 +28,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 @@ -84,12 +86,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 {