Skip to content

Commit c4f010c

Browse files
authored
Merge pull request #1191 from ddunbar/SR-4910
[Commands] Fix host destination resolution w.r.t. relative launch path.
2 parents a6cf331 + 09e9e6c commit c4f010c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Sources/Commands/Destination.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public struct Destination {
5555
public let extraCPPFlags: [String]
5656

5757
/// Returns the bin directory for the host.
58-
private static func hostBinDir() -> AbsolutePath {
58+
///
59+
/// - Parameter originalWorkingDirectory: The working directory when the program was launched.
60+
private static func hostBinDir(
61+
originalWorkingDirectory: AbsolutePath = currentWorkingDirectory
62+
) -> AbsolutePath {
5963
#if Xcode
6064
// For Xcode, set bin directory to the build directory containing the fake
6165
// toolchain created during bootstraping. This is obviously not production ready
@@ -70,14 +74,18 @@ public struct Destination {
7074
.parentDirectory.parentDirectory.appending(components: ".build", "debug")
7175
#else
7276
return AbsolutePath(
73-
CommandLine.arguments[0], relativeTo: currentWorkingDirectory).parentDirectory
77+
CommandLine.arguments[0], relativeTo: originalWorkingDirectory).parentDirectory
7478
#endif
7579
}
7680

7781
/// The destination describing the host OS.
78-
public static func hostDestination(_ binDir: AbsolutePath? = nil) throws -> Destination {
82+
public static func hostDestination(
83+
_ binDir: AbsolutePath? = nil,
84+
originalWorkingDirectory: AbsolutePath = currentWorkingDirectory
85+
) throws -> Destination {
7986
// Select the correct binDir.
80-
let binDir = binDir ?? Destination.hostBinDir()
87+
let binDir = binDir ?? Destination.hostBinDir(
88+
originalWorkingDirectory: originalWorkingDirectory)
8189

8290
#if os(macOS)
8391
// Get the SDK.

Sources/Commands/SwiftTool.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ private class ToolWorkspaceDelegate: WorkspaceDelegate {
6565
}
6666

6767
public class SwiftTool<Options: ToolOptions> {
68+
/// The original working directory.
69+
let originalWorkingDirectory: AbsolutePath
70+
6871
/// The options of this tool.
6972
let options: Options
7073

@@ -104,7 +107,9 @@ public class SwiftTool<Options: ToolOptions> {
104107
///
105108
/// - parameter args: The command line arguments to be passed to this tool.
106109
public init(toolName: String, usage: String, overview: String, args: [String]) {
107-
110+
// Capture the original working directory ASAP.
111+
originalWorkingDirectory = currentWorkingDirectory
112+
108113
// Create the parser.
109114
parser = ArgumentParser(
110115
commandName: "swift \(toolName)",
@@ -400,7 +405,8 @@ public class SwiftTool<Options: ToolOptions> {
400405
/// Lazily compute the host toolchain used to compile the package description.
401406
private lazy var _hostToolchain: Result<UserToolchain, AnyError> = {
402407
return Result(anyError: {
403-
try UserToolchain(destination: Destination.hostDestination())
408+
try UserToolchain(destination: Destination.hostDestination(
409+
originalWorkingDirectory: self.originalWorkingDirectory))
404410
})
405411
}()
406412

0 commit comments

Comments
 (0)