Skip to content

Commit fc8f10e

Browse files
committed
Set the working directory of build tool plugin commands deterministically
This resolves an issue where outputs of build tool plugins could be emitted to a random output directory based on whatever the working directory of the calling process happened to be at the time the build tool plugin was executed. Closes #7930
1 parent c28a891 commit fc8f10e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Sources/PackagePlugin/Plugin.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ extension Plugin {
149149
// package is the one we'll set the context's `package` property to.
150150
let context: PluginContext
151151
let target: Target
152+
let pluginWorkDirectory: URL
152153
do {
153154
var deserializer = PluginContextDeserializer(wireInput)
154155
let package = try deserializer.package(for: rootPackageId)
155-
let pluginWorkDirectory = try deserializer.url(for: wireInput.pluginWorkDirId)
156+
pluginWorkDirectory = try deserializer.url(for: wireInput.pluginWorkDirId)
156157
let toolSearchDirectories = try wireInput.toolSearchDirIds.map {
157158
try deserializer.url(for: $0)
158159
}
@@ -208,7 +209,8 @@ extension Plugin {
208209
displayName: displayName,
209210
executable: executable,
210211
arguments: arguments,
211-
environment: environment
212+
environment: environment,
213+
workingDirectory: pluginWorkDirectory
212214
)
213215
let message = PluginToHostMessage.defineBuildCommand(
214216
configuration: command,
@@ -222,7 +224,8 @@ extension Plugin {
222224
displayName: displayName,
223225
executable: executable,
224226
arguments: arguments,
225-
environment: environment
227+
environment: environment,
228+
workingDirectory: pluginWorkDirectory
226229
)
227230
let message = PluginToHostMessage.definePrebuildCommand(
228231
configuration: command,
@@ -248,6 +251,7 @@ extension Plugin {
248251

249252
// Deserialize the context from the wire input structures, and create a record for us to pass to the XcodeProjectPlugin library.
250253
let record: XcodeProjectPluginInvocationRecord
254+
let pluginWorkDirectory: URL
251255
do {
252256
var deserializer = PluginContextDeserializer(wireInput)
253257
let xcodeProject = try deserializer.xcodeProject(for: rootProjectId)
@@ -256,7 +260,7 @@ extension Plugin {
256260
pluginGeneratedSources: try generatedSources.map { try deserializer.url(for: $0) },
257261
pluginGeneratedResources: try generatedResources.map { try deserializer.url(for: $0) }
258262
)
259-
let pluginWorkDirectory = try deserializer.url(for: wireInput.pluginWorkDirId)
263+
pluginWorkDirectory = try deserializer.url(for: wireInput.pluginWorkDirId)
260264
let toolSearchDirectories = try wireInput.toolSearchDirIds.map {
261265
try deserializer.url(for: $0)
262266
}
@@ -288,7 +292,7 @@ extension Plugin {
288292
executable: exec,
289293
arguments: args,
290294
environment: env,
291-
workingDirectory: nil)
295+
workingDirectory: pluginWorkDirectory)
292296
let message = PluginToHostMessage.defineBuildCommand(
293297
configuration: command,
294298
inputFiles: inputs,
@@ -301,7 +305,7 @@ extension Plugin {
301305
executable: exec,
302306
arguments: args,
303307
environment: env,
304-
workingDirectory: nil)
308+
workingDirectory: pluginWorkDirectory)
305309
let message = PluginToHostMessage.definePrebuildCommand(
306310
configuration: command,
307311
outputFilesDirectory: outdir)

0 commit comments

Comments
 (0)