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

Add toolchain swift stdlib to env when swift run. #8364

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 28 additions & 6 deletions Sources/Commands/SwiftRunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

import ArgumentParser
import Basics
@_spi(SwiftPMInternal) import Basics
import CoreCommands
import Foundation
import PackageGraph
Expand Down Expand Up @@ -233,7 +233,9 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
let runnerPath: AbsolutePath
let arguments: [String]

if let debugger = try swiftCommandState.getTargetToolchain().swiftSDK.toolset.knownTools[.debugger],
let toolchain = try swiftCommandState.getTargetToolchain()

if let debugger = toolchain.swiftSDK.toolset.knownTools[.debugger],
let debuggerPath = debugger.path {
runnerPath = debuggerPath
arguments = debugger.extraCLIOptions + [productAbsolutePath.pathString] + options.arguments
Expand All @@ -242,11 +244,23 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
arguments = options.arguments
}

// For Linux, need to point LD_LIBRARY_PATH at the swift runtime
let environment: [String: String]?
if toolchain.targetTriple.isLinux() {
var current = Environment.current
let pathVar: EnvironmentKey = "LD_LIBRARY_PATH"
current.prependPath(key: pathVar, value: try toolchain.linuxSwiftStdlib.pathString)
environment = .init(current)
} else {
environment = nil
}

try self.run(
fileSystem: swiftCommandState.fileSystem,
executablePath: runnerPath,
originalWorkingDirectory: swiftCommandState.originalWorkingDirectory,
arguments: arguments
arguments: arguments,
environment: environment
)
} catch Diagnostics.fatalError {
throw ExitCode.failure
Expand Down Expand Up @@ -294,7 +308,8 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
fileSystem: FileSystem,
executablePath: AbsolutePath,
originalWorkingDirectory: AbsolutePath,
arguments: [String]
arguments: [String],
environment: [String: String]? = nil
) throws {
// Make sure we are running from the original working directory.
let cwd: AbsolutePath? = fileSystem.currentWorkingDirectory
Expand All @@ -303,7 +318,7 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
}

let pathRelativeToWorkingDirectory = executablePath.relative(to: originalWorkingDirectory)
try execute(path: executablePath.pathString, args: [pathRelativeToWorkingDirectory.pathString] + arguments)
try execute(path: executablePath.pathString, args: [pathRelativeToWorkingDirectory.pathString] + arguments, env: environment)
}

/// Determines if a path points to a valid swift file.
Expand All @@ -327,7 +342,7 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
}

/// A safe wrapper of TSCBasic.exec.
private func execute(path: String, args: [String]) throws -> Never {
private func execute(path: String, args: [String], env: [String: String]?) throws -> Never {
#if !os(Windows)
// Dispatch will disable almost all asynchronous signals on its worker threads, and this is called from `async`
// context. To correctly `exec` a freshly built binary, we will need to:
Expand Down Expand Up @@ -358,6 +373,13 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
#endif /* os(FreeBSD) || os(OpenBSD) */
#endif

if let env {
// set the env before we exec.
// TODO: we should really use execve here.
// Though, Windows doesn't really exec anyway.
try env.forEach { try ProcessEnv.setVar($0, value: $1) }
}

try TSCBasic.exec(path: path, args: args)
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ extension Toolchain {
}
}

public var linuxSwiftStdlib: AbsolutePath {
get throws {
try Self.toolchainLibDir(swiftCompilerPath: self.swiftCompilerPath).appending(
components: ["swift", "linux"]
)
}
}

public var toolchainLibDir: AbsolutePath {
get throws {
// FIXME: Not sure if it's better to base this off of Swift compiler or our own binary.
Expand Down