Skip to content

Commit 5b78c88

Browse files
committed
Package plugins don't work on Windows if the PATH environment variable isn't accessed with the right case
Builds of packages using package plugins may fail on Windows with the following error: ``` error: plugin process ended by an uncaught signal: 309 <command: C:\foo\.build\plugins\cache\myplugin.exe>, <output: > ``` 309 translates to 0x135, which maps to 0xc0000135 aka STATUS_DLL_NOT_FOUND. This is due to the following code in DefaultPluginScriptRunner.swift: ```swift #if os(Windows) let pluginLibraryPath = self.toolchain.swiftPMLibrariesLocation.pluginLibraryPath.pathString var env = ProcessInfo.processInfo.environment if let Path = env["Path"] { env["Path"] = "\(pluginLibraryPath);\(Path)" } else { env["Path"] = pluginLibraryPath } process.environment = env #endif ``` Environment variable names are case-insensitive on Windows. On a real Windows host, it tends to be spelled "Path". In a Windows Container (Docker), it tends to be spelled "PATH". The code will end up clearing out the other paths from the PATH environment variable in the else case because Path != PATH. We need to access the path here in a case-insensitive manner -- use the existing Environment abstraction for this, which handles Windows case sensitivity concerns. Also fix another occurrence of this same pattern in the manifest loader. Closes #8125
1 parent c237352 commit 5b78c88

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
10621062
var environment = Environment.current
10631063
#if os(Windows)
10641064
let windowsPathComponent = runtimePath.pathString.replacingOccurrences(of: "/", with: "\\")
1065-
environment["Path"] = "\(windowsPathComponent);\(environment["Path"] ?? "")"
1065+
environment.prependPath(key: .path, value: windowsPathComponent)
10661066
#endif
10671067

10681068
let cleanupAfterRunning = cleanupIfError.delay()

Sources/SPMBuildCore/Plugins/DefaultPluginScriptRunner.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,9 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
466466
process.environment = ProcessInfo.processInfo.environment
467467
#if os(Windows)
468468
let pluginLibraryPath = self.toolchain.swiftPMLibrariesLocation.pluginLibraryPath.pathString
469-
var env = ProcessInfo.processInfo.environment
470-
if let Path = env["Path"] {
471-
env["Path"] = "\(pluginLibraryPath);\(Path)"
472-
} else {
473-
env["Path"] = pluginLibraryPath
474-
}
475-
process.environment = env
469+
var env = Environment.current
470+
env.prependPath(key: .path, value: pluginLibraryPath)
471+
process.environment = .init(env)
476472
#endif
477473
process.currentDirectoryURL = workingDirectory.asURL
478474

0 commit comments

Comments
 (0)