-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a few tests for Env Vars edge cases #46039
Conversation
…itive This reverts commit cd1a8da.
…e same Example: "NPM_CONFIG_CACHE=^" "npm_config_cache=^" fixes dotnet#42029
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsThis is a proposal of a fix for #42029 Windows treats env-vars in a case insensitive way. Kernel32.SetEnvironmentVariable("A", "b");
Console.WriteLine(GetEnvironmentVariableCore("A")); // prints "b"
Kernel32.SetEnvironmentVariable("a", "c");
Console.WriteLine(GetEnvironmentVariableCore("A")); // prints "c" internal static class Kernel32
{
[DllImport("kernel32.dll", EntryPoint = "SetEnvironmentVariableW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
internal static extern bool SetEnvironmentVariable(string lpName, string? lpValue);
[DllImport("kernel32.dll", EntryPoint = "GetEnvironmentVariableW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern unsafe uint GetEnvironmentVariable(string lpName, ref char lpBuffer, uint nSize);
}
private static void SetEnvironmentVariableCore(string variable, string? value)
{
if (!Kernel32.SetEnvironmentVariable(variable, value))
{
int errorCode = Marshal.GetLastWin32Error();
throw new Exception($"Error code: {errorCode}");
}
}
private unsafe static string? GetEnvironmentVariableCore(string variable)
{
char[] buffer = new char[1000];
fixed (char* pinned = buffer)
{
uint size = Kernel32.GetEnvironmentVariable(variable, ref buffer[0], (uint)buffer.Length);
return new string(pinned, 0, (int)size);
}
} The problem is, that by using For such apps (example #42029),
We can't change the runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessStartInfo.cs Line 87 in 995bbae
Because this would introduce a breaking change in scenarios, where users were so far overwriting the env var: var startInfo1 = new ProcessStartInfo();
startInfo1.Environment["A"] = "b";
startInfo1.Environment["a"] = "c"; // was always resulting in the env var set to "c"
var startInfo2 = new ProcessStartInfo();
startInfo2.Environment["a"] = "c";
startInfo2.Environment["A"] = "b"; // was always resulting in the env var set to "b"
// with `StringComparison` changed it would be dependent on the order of items in the dictionary I think that the best we can do is to not throw for cases like #42029 where there are simply two env var keys with the same value: "NPM_CONFIG_CACHE=^"
"npm_config_cache=^" And ignore such "duplicates" but throw (as we used to do so far) where the values are different.
|
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessStartInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessStartInfo.Windows.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Edit: #42029 turned out to be not reproducible using the supported versions of .NET Core (2.1, 3.1 and 5.0) Please see #42029 (comment) for full details.