docs icon indicating copy to clipboard operation
docs copied to clipboard

[Breaking change]: Support environment variables with empty value

Open jkotas opened this issue 1 month ago • 2 comments

Description

Add support for setting the environment variable to an empty value in Environment.SetEnvironment and ProcessInfo.Environment APIs.

Version

.NET 9 Preview 6

Previous behavior

Both Environment.SetEnvironment("TEST", string.Empty) and Environment.SetEnvironment("TEST", null) deleted the environment variable.

Both ProcessInfo.Environment["TEST"] = string.Empty and ProcessInfo.Environment["TEST"] = null set the environment variable in the child process to an empty value.

New behavior

Environment.SetEnvironment("TEST", string.Empty) sets the environment variable value to an empty value. Environment.SetEnvironment("TEST", null) behavior is unchanged, it still deletes the environment variable.

ProcessInfo.Environment["TEST"] = null deletes the environment variable. ProcessInfo.Environment["TEST"] = string.Empty behavior is unchanged, it still sets the environment variable the environment variable value to an empty value.

Type of breaking change

  • [ ] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • [ ] Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • [X] Behavioral change: Existing binaries may behave differently at run time.

Reason for change

Before this change, it was not possible to use Environment.SetEnvironment to set the environment variable to an empty value that is a valid environment variable value on all supported platforms.

Recommended action

Change code to pass null instead of string.Empty as the value argument to Environment.SetEnvironment API to delete the environment variable.

Change code to pass string.Empty instead of null as the value to ProcessStartInfo.Environment, ProcessStartInfo.EnvironmentVariables APIs to set the environment variable to an empty value.

Feature area

Core .NET libraries

Affected APIs

Environment.SetEnvironmentVariable, ProcessStartInfo.Environment, ProcessStartInfo.EnvironmentVariables

jkotas avatar Jun 18 '24 19:06 jkotas