Skip to content

[6.1] Apply toolset's debugger property in swift run #8257

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 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Fixtures/Miscellaneous/EchoExecutable/echo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo "$@"
5 changes: 5 additions & 0 deletions Fixtures/Miscellaneous/EchoExecutable/toolset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"debugger": { "path": "echo.sh" },
"schemaVersion" : "1.0",
"rootPath" : "."
}
43 changes: 35 additions & 8 deletions Sources/Commands/SwiftRunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,28 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
try await buildSystem.build(subset: .product(productName))
}

let executablePath = try swiftCommandState.productsBuildParameters.buildPath.appending(component: productName)
let productRelativePath = try swiftCommandState.productsBuildParameters.executablePath(for: productName)
let productAbsolutePath = try swiftCommandState.productsBuildParameters.buildPath.appending(productRelativePath)

// Make sure we are running from the original working directory.
let cwd: AbsolutePath? = swiftCommandState.fileSystem.currentWorkingDirectory
if cwd == nil || swiftCommandState.originalWorkingDirectory != cwd {
try ProcessEnv.chdir(swiftCommandState.originalWorkingDirectory)
}

let pathRelativeToWorkingDirectory = executablePath.relative(to: swiftCommandState.originalWorkingDirectory)
let lldbPath = try swiftCommandState.getTargetToolchain().getLLDB()
try exec(path: lldbPath.pathString, args: ["--", pathRelativeToWorkingDirectory.pathString] + options.arguments)
if let debugger = try swiftCommandState.getTargetToolchain().swiftSDK.toolset.knownTools[.debugger],
let debuggerPath = debugger.path {
try self.run(
fileSystem: swiftCommandState.fileSystem,
executablePath: debuggerPath,
originalWorkingDirectory: swiftCommandState.originalWorkingDirectory,
arguments: debugger.extraCLIOptions + [productAbsolutePath.pathString] + options.arguments
)
} else {
let pathRelativeToWorkingDirectory = productAbsolutePath.relative(to: swiftCommandState.originalWorkingDirectory)
let lldbPath = try swiftCommandState.getTargetToolchain().getLLDB()
try exec(path: lldbPath.pathString, args: ["--", pathRelativeToWorkingDirectory.pathString] + options.arguments)
}
} catch let error as RunError {
swiftCommandState.observabilityScope.emit(error)
throw ExitCode.failure
Expand Down Expand Up @@ -215,11 +226,27 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
}

let executablePath = try swiftCommandState.productsBuildParameters.buildPath.appending(component: productName)

let productRelativePath = try swiftCommandState.productsBuildParameters.executablePath(for: productName)
let productAbsolutePath = try swiftCommandState.productsBuildParameters.buildPath.appending(productRelativePath)

let runnerPath: AbsolutePath
let arguments: [String]

if let debugger = try swiftCommandState.getTargetToolchain().swiftSDK.toolset.knownTools[.debugger],
let debuggerPath = debugger.path {
runnerPath = debuggerPath
arguments = debugger.extraCLIOptions + [productAbsolutePath.pathString] + options.arguments
} else {
runnerPath = executablePath
arguments = options.arguments
}

try self.run(
fileSystem: swiftCommandState.fileSystem,
executablePath: executablePath,
executablePath: runnerPath,
originalWorkingDirectory: swiftCommandState.originalWorkingDirectory,
arguments: options.arguments
arguments: arguments
)
} catch Diagnostics.fatalError {
throw ExitCode.failure
Expand Down Expand Up @@ -267,8 +294,8 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
fileSystem: FileSystem,
executablePath: AbsolutePath,
originalWorkingDirectory: AbsolutePath,
arguments: [String]) throws
{
arguments: [String]
) throws {
// Make sure we are running from the original working directory.
let cwd: AbsolutePath? = fileSystem.currentWorkingDirectory
if cwd == nil || originalWorkingDirectory != cwd {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public struct BuildParameters: Encodable {
}

/// Returns the path to the executable of a product for the current build parameters.
private func executablePath(for name: String) throws -> RelativePath {
package func executablePath(for name: String) throws -> RelativePath {
try RelativePath(validating: "\(name)\(self.suffix)\(self.triple.executableExtension)")
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/CommandsTests/RunCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ final class RunCommandTests: CommandsTestCase {
XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout)
}

// echo.sh script from the toolset won't work on Windows
#if !os(Windows)
func testToolsetDebugger() async throws {
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
let (stdout, stderr) = try await SwiftPM.Run.execute(
["--toolset", "\(fixturePath)/toolset.json"], packagePath: fixturePath)

// We only expect tool's output on the stdout stream.
XCTAssertMatch(stdout, .contains("""
\(fixturePath)/.build
"""))

// swift-build-tool output should go to stderr.
XCTAssertMatch(stderr, .regex("Compiling"))
XCTAssertMatch(stderr, .contains("Linking"))
}
}
#endif

func testUnknownProductAndArgumentPassing() async throws {
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
let (stdout, stderr) = try await SwiftPM.Run.execute(
Expand Down