8000 Fix error in windows provider when the environment as an existing set… · PowerShell/PowerShell@41b3161 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41b3161

Browse files
authored
Fix error in windows provider when the environment as an existing set of variables name that only differs by case (#6320)
- make the provider storage for the environment on windows ignore duplicates - add tests to verify existing environment get-item behavior
1 parent 1730d8c commit 41b3161

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/System.Management.Automation/namespaces/EnvironmentProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ internal override IDictionary GetSessionStateTable()
198198
IDictionary environmentTable = Environment.GetEnvironmentVariables();
199199
foreach (DictionaryEntry entry in environmentTable)
200200
{
201-
providerTable.Add((string)entry.Key, entry);
201+
// Windows only: duplicate key (variable name that differs only in case)
202+
// NOTE: Even though this shouldn't happen, it can, e.g. when npm
203+
// creates duplicate environment variables that differ only in case -
204+
// see https://github.com/PowerShell/PowerShell/issues/6305.
205+
// However, because retrieval *by name* later is invariably
206+
// case-Insensitive, in effect only a *single* variable exists.
207+
// We simply ask Environment.GetEnvironmentVariable() which value is
208+
// the effective one, and use that.
209+
providerTable.TryAdd((string)entry.Key, entry);
202210
}
203211

204212
return providerTable;

test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,30 @@ Describe "Get-Item" -Tags "CI" {
116116
${result} | Should -BeOfType "Microsoft.Win32.RegistryKey"
117117
}
118118
}
119+
120+
Context "Environment provider" -tag "CI" {
121+
BeforeAll {
122+
$env:testvar="b"
123+
$env:testVar="a"
124+
}
125+
126+
AfterAll {
127+
Clear-Item -Path env:testvar -ErrorAction SilentlyContinue
128+
Clear-Item -Path env:testVar -ErrorAction SilentlyContinue
129+
}
130+
131+
It "get-item testVar" {
132+
(get-item env:\testVar).Value | Should -BeExactly "a"
133+
}
134+
135+
It "get-item is case-sensitive/insensitive as appropriate" {
136+
$expectedValue = "b"
137+
if($IsWindows)
138+
{
139+
$expectedValue = "a"
140+
}
141+
142+
(get-item env:\testvar).Value | Should -BeExactly $expectedValue
143+
}
144+
}
119145
}

0 commit comments

Comments
 (0)
0