Skip to content

Commit

Permalink
chocolateyGH-303 Install-ChocolateyPath Expands Variables in PATH, Ov…
Browse files Browse the repository at this point in the history
…erwriting

Preexisting Variables

When adding a new element to PATH, Install-ChocolateyPath reads PATH
(wh ich contains expanded variables), appends the new element, and then
writes the value back to PATH. This results in PATH having its
variables overwritten with its values. Instead of reading $env:PATH to
find the current path, read it from the registry, which will retain the
PATH's original variables.
  • Loading branch information
mnadel committed Jun 4, 2015
1 parent 5d51a6c commit 7773cac
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# limitations under the License.

function Install-ChocolateyPath {
param(
[string] $pathToInstall,
Expand All @@ -26,7 +26,14 @@ param(
if (!$envPath.ToLower().Contains($pathToInstall.ToLower()))
{
Write-Host "PATH environment variable does not have $pathToInstall in it. Adding..."
$actualPath = Get-EnvironmentVariable -Name 'Path' -Scope $pathType

if ($pathType -eq [System.EnvironmentVariableTarget]::Machine) {
$reg = [Microsoft.Win32.Registry]::Machine.OpenSubKey("Environment", $true);
} else {
$reg = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Environment", $true);
}

$actualPath = $reg.GetValue('Path', $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)

$statementTerminator = ";"
#does the path end in ';'?
Expand All @@ -38,15 +45,18 @@ param(

if ($pathType -eq [System.EnvironmentVariableTarget]::Machine) {
if (Test-ProcessAdminRights) {
$reg.SetValue('Path', $actualPath)
Set-EnvironmentVariable -Name 'Path' -Value $actualPath -Scope $pathType
} else {
$psArgs = "Install-ChocolateyPath -pathToInstall `'$originalPathToInstall`' -pathType `'$pathType`'"
Start-ChocolateyProcessAsAdmin "$psArgs"
}
} else {
Set-EnvironmentVariable -Name 'Path' -Value $actualPath -Scope $pathType
$reg.SetValue('Path', $actualPath)
}

$reg.Close()

#add it to the local path as well so users will be off and running
$envPSPath = $env:PATH
$env:Path = $envPSPath + $statementTerminator + $pathToInstall
Expand Down

4 comments on commit 7773cac

@ferventcoder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave the actual getting of the path in Get-EnvironmentVariable, but add a switch for not expanding the variable.

@mnadel
Copy link
Owner Author

@mnadel mnadel commented on 7773cac Jun 8, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sure that makes more sense. Thanks.

@ferventcoder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this end up?

@mnadel
Copy link
Owner Author

@mnadel mnadel commented on 7773cac Jul 30, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reminder. I'll be picking this up again.

Please sign in to comment.