Skip to content

Commit 3ddceee

Browse files
authored
TSCBasic: alter environment handling for Windows (#317)
Rather than using the unicode environment, which should be preferred, use the ANSI environment operations. This is important to do here as the reading of the environment relies on Foundation, which in turn relies on CoreFoundation for `_CFEnviron` which returns the ANSI environment. As a result, we need to ensure that we use the ANSI environment variables through out as the C runtime will maintain the two environments in parallel and may go out of sync. This will enable additional tests in Swift Package Manager to pass on Windows, but should really be addressed by changing Foundation to use the Unicode environment variables and then using that once more here.
1 parent f6e55a7 commit 3ddceee

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

Sources/TSCBasic/ProcessEnv.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ public enum ProcessEnv {
2626
/// Set the given key and value in the process's environment.
2727
public static func setVar(_ key: String, value: String) throws {
2828
#if os(Windows)
29-
guard key.withCString(encodedAs: UTF16.self, { keyStr in
30-
value.withCString(encodedAs: UTF16.self) { valStr in
31-
SetEnvironmentVariableW(keyStr, valStr)
32-
}
33-
}) else {
29+
guard TSCLibc._putenv("\(key)=\(value)") == 0 else {
3430
throw SystemError.setenv(Int32(GetLastError()), key)
3531
}
3632
#else
@@ -44,9 +40,7 @@ public enum ProcessEnv {
4440
/// Unset the give key in the process's environment.
4541
public static func unsetVar(_ key: String) throws {
4642
#if os(Windows)
47-
guard key.withCString(encodedAs: UTF16.self, { keyStr in
48-
SetEnvironmentVariableW(keyStr, nil)
49-
}) else {
43+
guard TSCLibc._putenv("\(key)=") == 0 else {
5044
throw SystemError.unsetenv(Int32(GetLastError()), key)
5145
}
5246
#else

0 commit comments

Comments
 (0)