chocolatey / cChoco

Community resource to manage Chocolatey
Apache License 2.0
154 stars 99 forks source link

cChocoPackageInstall can fail if run immediately after Chocolatey is installed. Invalid Path #174

Open rcarpenter79 opened 1 year ago

rcarpenter79 commented 1 year ago

Checklist

What You Are Seeing?

When I ran this resource on a server that was freshly built I was seeing errors about invalid paths.

I looked at the code and traced the problem to the functions Get-ChocoInstalledPackage and Get-ChocoVersion.

The problem was caused by the environment variable $env:ChocolateyInstall being null. I could retrieve the requried value using [environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine').

This problem was intermittent. I think it depended on the order that DSC ran the resources, as several steps caused the server I was building to reboot. I think a reboot after installing Chocolatey avoids this issue.

What is Expected?

The code should check if the environment variables haven't been initialised yet and update them if they haven't.

Adding this to the start of both functions resolves the problem.

if ([string]::IsNullOrEmpty($env:ChocolateyInstall))
{
    $env:ChocolateyInstall = [environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine')
}

How Did You Get This To Happen?

This problem happened when installing Chocolatey and then trying to install a package immediately afterwards.

System Details

Installed Packages

chocolatey 1.3.1
chocolatey-compatibility.extension 1.0.0
chocolatey-core.extension 1.4.0
notepadplusplus 8.5.2
notepadplusplus.install 8.5.2

Output Log

I don't have the error in the log file. 
I rebuilt the server using a modified local copy of the cChoco module to test my change.

Additional Context

No response