Skip to content

[6.0🍒] Resolve main executable symlink when looking up driver-adjacent subcommand binaries #1606

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
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
15 changes: 11 additions & 4 deletions Sources/swift-driver/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down