Skip to content

Commit 272b335

Browse files
committed
Restore environment variables exposed to SwiftPM build tool invocations
rdar://145763371
1 parent 519af3b commit 272b335

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Sources/SWBTaskConstruction/TaskProducers/OtherTaskProducers/CustomTaskProducer.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ final class CustomTaskProducer: PhasedTaskProducer, TaskProducer {
2222
for customTask in context.configuredTarget?.target.customTasks ?? [] {
2323

2424
let commandLine = customTask.commandLine.map { context.settings.globalScope.evaluate($0) }
25-
let environment = EnvironmentBindings(customTask.environment.map { (context.settings.globalScope.evaluate($0.0), context.settings.globalScope.evaluate($0.1)) })
25+
var environmentAssignments = computeScriptEnvironment(.shellScriptPhase, scope: context.settings.globalScope, settings: context.settings, workspaceContext: context.workspaceContext)
26+
if context.workspaceContext.core.hostOperatingSystem != .macOS {
27+
environmentAssignments = environmentAssignments.filter { $0.key.lowercased() != "path" }
28+
}
29+
for (key, value) in customTask.environment {
30+
environmentAssignments[context.settings.globalScope.evaluate(key)] = context.settings.globalScope.evaluate(value)
31+
}
32+
let environment = EnvironmentBindings(environmentAssignments)
2633
let workingDirectory = customTask.workingDirectory.map { Path(context.settings.globalScope.evaluate($0)).normalize() } ?? context.defaultWorkingDirectory
2734
let inputPaths = customTask.inputFilePaths.map { Path(context.settings.globalScope.evaluate($0)).normalize() }
2835
let inputs = inputPaths.map { delegate.createNode($0) }

Tests/SWBTaskConstructionTests/CustomTaskConstructionTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,53 @@ fileprivate struct CustomTaskConstructionTests: CoreBasedTests {
179179
}
180180
}
181181
}
182+
183+
@Test(.requireSDKs(.host))
184+
func customTaskInjectsShellScriptEnvironment() async throws {
185+
let testProject = TestProject(
186+
"aProject",
187+
groupTree: TestGroup(
188+
"SomeFiles", path: "Sources",
189+
children: [
190+
TestFile("input.txt"),
191+
TestFile("main.c"),
192+
]),
193+
buildConfigurations: [
194+
TestBuildConfiguration(
195+
"Debug",
196+
buildSettings: [
197+
"GENERATE_INFOPLIST_FILE": "YES",
198+
"PRODUCT_NAME": "$(TARGET_NAME)",
199+
"SDKROOT": "auto",
200+
"MY_SETTING": "FOO",
201+
]),
202+
],
203+
targets: [
204+
TestStandardTarget(
205+
"CoreFoo", type: .framework,
206+
buildPhases: [
207+
TestSourcesBuildPhase(["main.c"])
208+
],
209+
customTasks: [
210+
TestCustomTask(
211+
commandLine: ["tool", "-foo", "-bar"],
212+
environment: ["ENVVAR": "VALUE"],
213+
workingDirectory: Path.root.join("working/directory").str,
214+
executionDescription: "My Custom Task",
215+
inputs: ["$(SRCROOT)/Sources/input.txt"],
216+
outputs: [Path.root.join("output").str],
217+
enableSandboxing: false,
218+
preparesForIndexing: false)
219+
]
220+
),
221+
])
222+
let tester = try await TaskConstructionTester(getCore(), testProject)
223+
await tester.checkBuild(runDestination: .host) { results in
224+
results.checkNoDiagnostics()
225+
226+
results.checkTask(.matchRule(["CustomTask", "My Custom Task"])) { task in
227+
task.checkEnvironment(["ENVVAR": "VALUE", "MY_SETTING": "FOO"])
228+
}
229+
}
230+
}
182231
}

0 commit comments

Comments
 (0)