Skip to content
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

Support invoking known SwiftPM subcommands directly, instead of via symlink #1482

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions Sources/swift-driver/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import class TSCBasic.Process
import class TSCBasic.ProcessSet
import protocol TSCBasic.DiagnosticData
import var TSCBasic.localFileSystem
import struct TSCBasic.AbsolutePath

let interruptSignalSource = DispatchSource.makeSignalSource(signal: SIGINT)
let diagnosticsEngine = DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler])
Expand Down Expand Up @@ -74,17 +75,38 @@ do {
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 knownSwiftPMSubcommands: Set<String> = [
"swift-build",
"swift-experimental-sdk",
"swift-test",
"swift-run",
"swift-package-collection",
"swift-package-registry"
]

let subcommandPath: AbsolutePath?
let argv0: String?

if knownSwiftPMSubcommands.contains(subcommand) {
subcommandPath = Process.findExecutable(CommandLine.arguments[0])?.parentDirectory.appending(component: "swift-package")
?? Process.findExecutable("swift-package")
// Set argv[0] to dispatch to the requested SwiftPM subcommand
argv0 = subcommandPath?.parentDirectory.appending(component: subcommand).pathString
} else {
subcommandPath = Process.findExecutable(CommandLine.arguments[0])?.parentDirectory.appending(component: subcommand)
?? Process.findExecutable(subcommand)
argv0 = subcommandPath?.pathString
}

guard let subcommandPath = subcommandPath,
let argv0 = argv0,
localFileSystem.exists(subcommandPath) else {
throw Driver.Error.unknownOrMissingSubcommand(subcommand)
}

// Pass the full path to subcommand executable.
var arguments = arguments
arguments[0] = subcommandPath.pathString
arguments[0] = argv0

// Execute the subcommand.
try exec(path: subcommandPath.pathString, args: arguments)
Expand Down