Skip to content

Commit

Permalink
(chocolatey#310) Fix issue with environment handling
Browse files Browse the repository at this point in the history
- Only return environment variable names that have an associated value
from Get-EnvironmentVariableNames
- When calling Update-SessionEnvironment, don't call SetVariable if the
value at the queried scope is null.
  • Loading branch information
vexx32 committed May 24, 2024
1 parent 7d47e4b commit 87f3d38
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public static string[] GetVariableNames(EnvironmentVariableTarget scope)
{
using (var registryKey = GetEnvironmentKey(scope))
{
return registryKey.GetValueNames();
// .NET seems to cache the value names even if they're set to null (removed) at some point
// so we need to only return the names of things that have non-null values.
var names = registryKey.GetValueNames();
return names.Where(n => !string.IsNullOrEmpty((string)registryKey.GetValue(n))).ToArray();
}
}
catch
Expand Down Expand Up @@ -219,7 +222,10 @@ public static void UpdateSession(PSCmdlet cmdlet)
foreach (var name in GetVariableNames(scope))
{
var value = GetVariable(cmdlet, name, scope);
SetVariable(cmdlet, name, EnvironmentVariableTarget.Process, value);
if (!string.IsNullOrEmpty(value))
{
SetVariable(cmdlet, name, EnvironmentVariableTarget.Process, value);
}
}
}

Expand Down

0 comments on commit 87f3d38

Please sign in to comment.