Skip to content

Commit

Permalink
🐛 Workaround .NET bug
Browse files Browse the repository at this point in the history
The [Environment]::SetEnvironmentVariable() method is completely
broken for any expandable values.
See https://github.com/dotnet/corefx/issues/36449 - the issue is still
not resolved even in current versions of .NET Core. The only safe
method to set PATH and other values requiring expandable string
typing in the registry is via the registry APIs.
  • Loading branch information
vexx32 authored Dec 19, 2019
1 parent 02ea30d commit dfea68e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Uninstallation.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ if (-not (Test-Path $env:ChocolateyInstall)) {
accidentally overwrite them with absolute path values. Where the registry allows us to see "%SystemRoot%" in a PATH
entry, PowerShell's registry provider only sees "C:\Windows", for example.
#>
$userPath = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey(
'Environment'
).GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString()
$userKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment')
$userPath = $userKey.GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString()
$machinePath = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey(
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'
).GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString()
$machineKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\ControlSet001\Control\Session Manager\Environment\')
$machinePath = $machineKey.GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString()
$backupPATHs = @(
"User PATH: $userPath"
Expand All @@ -86,7 +84,9 @@ if ($userPath -like "*$env:ChocolateyInstall*") {
Where-Object { $_ -and $_ -ne "$env:ChocolateyInstall\bin" }
) -join [System.IO.Path]::PathSeparator
[Environment]::SetEnvironmentVariable('PATH', $newUserPATH, 'User')
# NEVER use [Environment]::SetEnvironmentVariable() for PATH values; see https://github.com/dotnet/corefx/issues/36449
# This issue exists in ALL released versions of .NET and .NET Core as of 12/19/2019
$userKey.SetValue('PATH', $newUserPATH, 'ExpandString')
}
if ($machinePath -like "*$env:ChocolateyInstall*") {
Expand All @@ -98,7 +98,9 @@ if ($machinePath -like "*$env:ChocolateyInstall*") {
Where-Object { $_ -and $_ -ne "$env:ChocolateyInstall\bin" }
) -join [System.IO.Path]::PathSeparator
[Environment]::SetEnvironmentVariable('PATH', $newMachinePATH, 'Machine')
# NEVER use [Environment]::SetEnvironmentVariable() for PATH values; see https://github.com/dotnet/corefx/issues/36449
# This issue exists in ALL released versions of .NET and .NET Core as of 12/19/2019
$machineKey.SetValue('PATH', $newMachinePATH, 'ExpandString')
}
# Adapt for any services running in subfolders of ChocolateyInstall
Expand All @@ -115,6 +117,9 @@ Remove-Item -Path $env:ChocolateyInstall -Recurse -Force -WhatIf
[Environment]::SetEnvironmentVariable($_, [string]::Empty, $scope)
}
}
$machineKey.Close()
$userKey.Close()
~~~

Additionally, the below code will remove the environment variables pointing to the tools directory that was managed by Chocolatey.
Expand Down

0 comments on commit dfea68e

Please sign in to comment.