From dba26af6b9c0b6824fdcac7c3ee9049173e27454 Mon Sep 17 00:00:00 2001 From: Charles Hu Date: Wed, 10 Jun 2026 11:21:39 -0700 Subject: [PATCH] Sort Windows environment block alphabetically Windows CreateProcessW documentation states that the environment block must be sorted because the system uses a sorted environment --- .../Subprocess/Platforms/Subprocess+Windows.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/Subprocess/Platforms/Subprocess+Windows.swift b/Sources/Subprocess/Platforms/Subprocess+Windows.swift index 9668ccaa..e81b86c7 100644 --- a/Sources/Subprocess/Platforms/Subprocess+Windows.swift +++ b/Sources/Subprocess/Platforms/Subprocess+Windows.swift @@ -988,11 +988,16 @@ extension Configuration { } // The environment string must be terminated by a double // null-terminator. Otherwise, CreateProcess will fail with - // INVALID_PARMETER. + // INVALID_PARMETER. It must also be sorted alphabetically according to + // CreateProcessW's documentation: + // > To do so, the application must explicitly create these environment variable + // > strings, sort them alphabetically (because the system uses a sorted environment), + // > and put them into the environment block. let environmentString = - env.map { - $0.key.rawValue + "=" + $0.value - }.joined(separator: "\0") + "\0\0" + env + .sorted { $0.key < $1.key } + .map { $0.key.rawValue + "=" + $0.value } + .joined(separator: "\0") + "\0\0" // Prepare arguments let (