chocolatey / cChoco

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

$env:ChocolateyInstall is empty during package install #130

Closed cribeiro84 closed 4 years ago

cribeiro84 commented 5 years ago

There is an unexpected behaviour that's getting the $env:ChocolateyInstall as null value.

cribeiro84 commented 5 years ago

PR raised: #129

pauby commented 5 years ago

@cribeiro84 Is this an issue for Chocolatey that should be raised? I would have though $env:ChocolateyInstall not being populated is a bug?

Does it happen when you install Chocolatey outside of cChoco or only when cChoco installs Chocolatey?

cribeiro84 commented 5 years ago

@pauby this issue happens during the Get-ChocoInstalledPackage call. When it tries to get the cache location, for some reaosn, the $env:ChocolateyInstall is null/empty but if I open a new PowerShell window and try to get the $env:ChocolateyInstall outside of the cChoco context, it works.

pauby commented 5 years ago

@cribeiro84 Thanks for confirming that. I think we need to look at what is causing the issue (sounds like scope) rather than working around it.

Can you give me steps you go through so I can replicate it?

cribeiro84 commented 5 years ago

I'm really sorry for the delay in replying back to you @pauby. What I did was just to install the cChoco module on my server and I ran the PowerShell as Administrator directly on the server. My DSC script I added the following block:

foreach ($chocoPackage in $node.ChocoPackages) { cChocoPackageInstaller $("ChocoPackage$chocoPackage") { Ensure = 'Present' Name = $chocoPackage } }

pauby commented 4 years ago

Using your code I cannot reproduce this error. I do get a different error but if use:

        cChocoInstaller InstallChoco
        {
        InstallDir = "$env:ALLUSERSPROFILE\Chocolatey"
        }

    foreach ($chocoPackage in $node.ChocoPackages) { 
        cChocoPackageInstaller $("ChocoPackage$chocoPackage") { 
            Ensure = 'Present'
            Name = $chocoPackage 
            DependsOn = '[cChocoInstaller]installChoco'
        } 
        }

It works. The part added is the DependsOn = '[cChocoInstaller]installChoco' to each package declaration and the:

        cChocoInstaller InstallChoco
        {
        InstallDir = "$env:ALLUSERSPROFILE\Chocolatey"
        }
cribeiro84 commented 4 years ago

Sorry for the delay. I was not able to reproduce the issue anymore, closing this issue.