Skip to content

Commit 6dbb879

Browse files
authored
Add toolchain swift stdlib to env when swift run. (#8364)
1 parent 0193d5f commit 6dbb879

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

Sources/Commands/SwiftRunCommand.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import ArgumentParser
14-
import Basics
14+
@_spi(SwiftPMInternal) import Basics
1515
import CoreCommands
1616
import Foundation
1717
import PackageGraph
@@ -233,7 +233,9 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
233233
let runnerPath: AbsolutePath
234234
let arguments: [String]
235235

236-
if let debugger = try swiftCommandState.getTargetToolchain().swiftSDK.toolset.knownTools[.debugger],
236+
let toolchain = try swiftCommandState.getTargetToolchain()
237+
238+
if let debugger = toolchain.swiftSDK.toolset.knownTools[.debugger],
237239
let debuggerPath = debugger.path {
238240
runnerPath = debuggerPath
239241
arguments = debugger.extraCLIOptions + [productAbsolutePath.pathString] + options.arguments
@@ -242,11 +244,23 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
242244
arguments = options.arguments
243245
}
244246

247+
// For Linux, need to point LD_LIBRARY_PATH at the swift runtime
248+
let environment: [String: String]?
249+
if toolchain.targetTriple.isLinux() {
250+
var current = Environment.current
251+
let pathVar: EnvironmentKey = "LD_LIBRARY_PATH"
252+
current.prependPath(key: pathVar, value: try toolchain.linuxSwiftStdlib.pathString)
253+
environment = .init(current)
254+
} else {
255+
environment = nil
256+
}
257+
245258
try self.run(
246259
fileSystem: swiftCommandState.fileSystem,
247260
executablePath: runnerPath,
248261
originalWorkingDirectory: swiftCommandState.originalWorkingDirectory,
249-
arguments: arguments
262+
arguments: arguments,
263+
environment: environment
250264
)
251265
} catch Diagnostics.fatalError {
252266
throw ExitCode.failure
@@ -294,7 +308,8 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
294308
fileSystem: FileSystem,
295309
executablePath: AbsolutePath,
296310
originalWorkingDirectory: AbsolutePath,
297-
arguments: [String]
311+
arguments: [String],
312+
environment: [String: String]? = nil
298313
) throws {
299314
// Make sure we are running from the original working directory.
300315
let cwd: AbsolutePath? = fileSystem.currentWorkingDirectory
@@ -303,7 +318,7 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
303318
}
304319

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

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

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

376+
if let env {
377+
// set the env before we exec.
378+
// TODO: we should really use execve here.
379+
// Though, Windows doesn't really exec anyway.
380+
try env.forEach { try ProcessEnv.setVar($0, value: $1) }
381+
}
382+
361383
try TSCBasic.exec(path: path, args: args)
362384
}
363385

Sources/PackageModel/Toolchain.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ extension Toolchain {
8888
}
8989
}
9090

91+
public var linuxSwiftStdlib: AbsolutePath {
92+
get throws {
93+
try Self.toolchainLibDir(swiftCompilerPath: self.swiftCompilerPath).appending(
94+
components: ["swift", "linux"]
95+
)
96+
}
97+
}
98+
9199
public var toolchainLibDir: AbsolutePath {
92100
get throws {
93101
// FIXME: Not sure if it's better to base this off of Swift compiler or our own binary.

0 commit comments

Comments
 (0)