Skip to content

Commit fb32460

Browse files
committed
Add the ability to control the environment passed to the manifest and plugin compilation
It defaults to `ProcessEnv.vars` so that there is no change in semantics unless the client code customizes the environment.
1 parent 8d0beab commit fb32460

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

Sources/PackageLoading/ManifestLoader.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
763763
cmd += ["-o", compiledManifestFile.pathString]
764764

765765
// Compile the manifest.
766-
let compilerResult = try Process.popen(arguments: cmd)
766+
let compilerResult = try Process.popen(arguments: cmd, environment: toolchain.swiftCompilerEnvironment)
767767
let compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()
768768
result.compilerOutput = compilerOutput
769769

Sources/PackageModel/ToolchainConfiguration.swift

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public struct ToolchainConfiguration {
2121
/// Extra arguments to pass the Swift compiler (defaults to the empty string).
2222
public var swiftCompilerFlags: [String]
2323

24+
/// Environment to pass to the Swift compiler (defaults to the inherited environment).
25+
public var swiftCompilerEnvironment: [String: String]
26+
2427
/// The path of the library resources.
2528
public var libDir: AbsolutePath
2629

@@ -47,13 +50,15 @@ public struct ToolchainConfiguration {
4750
public init(
4851
swiftCompiler: AbsolutePath,
4952
swiftCompilerFlags: [String] = [],
53+
swiftCompilerEnvironment: [String: String] = ProcessEnv.vars,
5054
libDir: AbsolutePath? = nil,
5155
binDir: AbsolutePath? = nil,
5256
sdkRoot: AbsolutePath? = nil,
5357
xctestLocation: AbsolutePath? = nil
5458
) {
5559
self.swiftCompiler = swiftCompiler
5660
self.swiftCompilerFlags = swiftCompilerFlags
61+
self.swiftCompilerEnvironment = swiftCompilerEnvironment
5762
self.libDir = libDir ?? Self.libDir(forBinDir: swiftCompiler.parentDirectory)
5863
self.binDir = binDir
5964
self.sdkRoot = sdkRoot

Sources/Workspace/DefaultPluginScriptRunner.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner {
114114
let compiledExec = cacheDir.appending(component: "compiled-plugin")
115115
command += ["-o", compiledExec.pathString]
116116

117-
let result = try Process.popen(arguments: command)
117+
let result = try Process.popen(arguments: command, environment: toolchain.swiftCompilerEnvironment)
118118
let output = try (result.utf8Output() + result.utf8stderrOutput()).spm_chuzzle() ?? ""
119119
if result.exitStatus != .terminated(code: 0) {
120120
// TODO: Make this a proper error.

Tests/PackageLoadingTests/PD_5_0_LoadingTests.swift

+49
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,53 @@ class PackageDescription5_0LoadingTests: PackageDescriptionLoadingTests {
547547
XCTAssertEqual(manifest.dependencies, [])
548548
}
549549
}
550+
551+
func testManifestLoaderEnvironment() throws {
552+
try testWithTemporaryDirectory { path in
553+
let fs = localFileSystem
554+
555+
let packagePath = path.appending(component: "pkg")
556+
let manifestPath = packagePath.appending(component: "Package.swift")
557+
try fs.writeFileContents(manifestPath) { stream in
558+
stream <<< """
559+
// swift-tools-version:5
560+
import PackageDescription
561+
562+
let package = Package(
563+
name: "Trivial",
564+
targets: [
565+
.target(
566+
name: "foo",
567+
dependencies: []),
568+
]
569+
)
570+
"""
571+
}
572+
573+
let moduleTraceFilePath = path.appending(component: "swift-module-trace")
574+
var toolchain = ToolchainConfiguration.default
575+
toolchain.swiftCompilerEnvironment["SWIFT_LOADED_MODULE_TRACE_FILE"] = moduleTraceFilePath.pathString
576+
let manifestLoader = ManifestLoader(
577+
toolchain: toolchain,
578+
serializedDiagnostics: true,
579+
isManifestSandboxEnabled: false,
580+
cacheDir: nil)
581+
582+
let diagnostics = DiagnosticsEngine()
583+
let manifest = try manifestLoader.load(
584+
at: manifestPath.parentDirectory,
585+
packageKind: .local,
586+
packageLocation: manifestPath.pathString,
587+
toolsVersion: .v5,
588+
fileSystem: fs,
589+
diagnostics: diagnostics
590+
)
591+
592+
XCTAssertTrue(diagnostics.diagnostics.isEmpty)
593+
XCTAssertEqual(manifest.name, "Trivial")
594+
595+
let moduleTraceJSON = try XCTUnwrap(try localFileSystem.readFileContents(moduleTraceFilePath).validDescription)
596+
XCTAssert(moduleTraceJSON.contains("PackageDescription"))
597+
}
598+
}
550599
}

0 commit comments

Comments
 (0)