Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent the $env:temp variable from being changed on every run of Install-BoxstarterPackage #442

Closed
me-kell opened this issue Oct 5, 2020 · 20 comments · Fixed by #479
Closed
Assignees
Labels
5 - Released The issue has been resolved, and released to the public for consumption Bug Issues where something has happened which was not expected or intended
Milestone

Comments

@me-kell
Copy link

me-kell commented Oct 5, 2020

What You Are Seeing?

$env:temp is changed on every run of Install-BoxstarterPackage

How Did You Get This To Happen? (Steps to Reproduce)

I am running Boxstarter v2.12.0 and Chocolatey v0.10.15 under Windows 10

Here is my script to reproduce it. The same behaviour can be observed in Boxstarter Shell or Powershell

# uncomment following line if running in Powershell
# Import-Module Boxstarter.Chocolatey
$filename = "$env:userprofile\test_script.ps1"
Set-Content -Path $filename -Value 'Write-Host "env:temp=$env:temp"'
Install-BoxstarterPackage -PackageName "$filename"

every time you call Install-BoxstarterPackage -PackageName "$filename" additional subdirectories chocolatey\chocolatey are appended to $env:temp

env:temp=C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey
env:temp=C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey
env:temp=C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey

Related Issues?

Maybe following issues are related to this one:

@me-kell me-kell added 0 - _Triaging Issue is accepted, but a milestone has yet to be added for the issue Bug Issues where something has happened which was not expected or intended labels Oct 5, 2020
@me-kell me-kell changed the title $env:temp is changed on every run of Install-BoxstarterPackage $env:temp is changed on every run of Install-BoxstarterPackage Oct 5, 2020
@me-kell
Copy link
Author

me-kell commented Oct 5, 2020

The following script shows a similar behaviour. But this time the $env:temp seems to be changed by the call of cinst:

PS> $filename = "$env:userprofile\test_script.ps1"
PS> Set-Content -Path $filename -Value '
>> choco install chocolatey
>> Write-Host "env:temp=$env:temp"
>> choco install chocolatey
>> Write-Host "env:temp=$env:temp"
>> choco install chocolatey
>> Write-Host "env:temp=$env:temp"
>> '
PS> Install-BoxstarterPackage -PackageName "$filename"```

Outputs

PS> Install-BoxstarterPackage -PackageName "$filename"
env:temp=C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey
...
PS> Install-BoxstarterPackage -PackageName "$filename"
env:temp=C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey
...
PS> Install-BoxstarterPackage -PackageName "$filename"
env:temp=C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey

This seems be related to the call to choco install and not to choco install itself since the following lines called directly (i.e. not via script) do not change the $env:temp:

PS> choco install chocolatey
PS> Write-Host "env:temp=$env:temp"
env:temp=C:\Users\map\AppData\Local\Temp
PS> choco install chocolatey
PS> Write-Host "env:temp=$env:temp"
env:temp=C:\Users\map\AppData\Local\Temp
PS> choco install chocolatey
PS> Write-Host "env:temp=$env:temp"
env:temp=C:\Users\map\AppData\Local\Temp

@me-kell
Copy link
Author

me-kell commented Oct 13, 2020

It is the dll chocolatey.dll packaged with Boxstarter that changes the variable $env:temp.
Replacing the packaged chocolatey.dll with a new version from chocolatey.lib.0.10.15\lib\chocolatey.dll returns the $env:temp unchanged.

Nonetheless the dll chocolatey.dll is always called three times via ChocolateWrapper.Run. See Line #267 in file Boxstarter.Chocolatey\invoke-chocolatey.ps1. These three calls result in a $env:temp with three chocolatey folders appended when using the packaged chocolatey.dll.

When using the chocolatey.lib.0.10.15\lib\chocolatey.dll the ChocolateWrapper.Run is also called thre times but chocolatey.lib.0.10.15\lib\chocolatey.dll appends chocolatey to the $env:temp in the first call and restores it to its value in the second and third call returning the initial and correct value of $env:temp.

IMHO this is both a Boxstarter and a Chocolatey issue.

Where can I see which version of chocolatey.dll is packages with Boxstarter?

@pauby
Copy link
Member

pauby commented Oct 13, 2020

@me-kell Thanks for troubleshooting this!

We can't replace the DLL with the new one at this stage. This was tried several versions ago and broke a lot of Boxstarter functionality. Updating Boxstarter to use the newer version of Chocolatey is something we are very much aware of though and raised issue #394 to look at it.

A solution may be to capture what $env:TEMP before the try{} and restore it in the finally{} block and include enough comments around it to indicate this is a workaround for the issue in Chocolatey 0.10.5. Thoughts?

@pauby pauby added 0 - Backlog Issue is accepted, but is not ready to be worked on or not in current sprint Up For Grabs and removed 0 - _Triaging Issue is accepted, but a milestone has yet to be added for the issue labels Oct 13, 2020
@me-kell
Copy link
Author

me-kell commented Oct 13, 2020

I'm currently using this (and similar) workarounds but I wonder why ChocolateWrapper.Run is called three (apparently embedded) times.

In the following example I capture $env:temp and a timestamp $tstamp to help differenciate the different calls of $choco.Run()

    Enter-BoxstarterLogable {
        Write-BoxstarterMessage "calling choco now with $chocoArgs" -Verbose
        $cd = [System.IO.Directory]::GetCurrentDirectory()
->      $oldEnvTemp = $env:temp
->      $tstamp = Get-Date -Format HH:mm:ss.fff
        try {
            # Chocolatey.dll uses GetCurrentDirectory which is not quite right
            # when calling via PowerShell. so we set it here
            Write-BoxstarterMessage "setting current directory location to $((Get-Location).Path)" -Verbose
            [System.IO.Directory]::SetCurrentDirectory("$(Convert-Path (Get-Location).Path)")
->          Write-Host "##### before $tstamp $env:temp #####"
            $choco.Run($chocoArgs)
->          Write-Host "#####    mid $tstamp $env:temp #####"
        }
        finally {
            Write-BoxstarterMessage "restoring current directory location to $cd" -Verbose
            [System.IO.Directory]::SetCurrentDirectory($cd)
->          $env:temp = $oldEnvTemp
->          Write-Host "#####  after $tstamp $env:temp #####"
        }
    }

The result is as follows. Please note my added bracketing to visualize the different calls. These are:

  1. a first call to create a temp package for the script
  2. a second call to install the created package
  3. a third call with double output for the choco install line in the script
Boxstarter: Installing package C:\Users\map\test_script.ps1
Boxstarter Version 2.12.0
(c) 2018 Chocolatey Software, Inc, 2012 - 2018 Matt Wrock. https://boxstarter.org

Boxstarter: Disabling Automatic Updates from Windows Update

+- ##### before 11:37:13.707 C:\Users\map\AppData\Local\Temp #####
|      Attempting to build package from 'tmpD60D.tmp.nuspec'.
|      Successfully created package 'C:\ProgramData\Boxstarter\BuildPackages\tmpD60D.tmp.1.0.0.nupkg'
|  #####    mid 11:37:13.707 C:\Users\map\AppData\Local\Temp\chocolatey #####
+- #####  after 11:37:13.707 C:\Users\map\AppData\Local\Temp #####

Boxstarter: Created a temporary package tmpD60D.tmp from C:\Users\map\test_script.ps1 in C:\ProgramData\Boxstarter\BuildPackages
+ Boxstarter starting Calling Chocolatey to install tmpD60D.tmp. This may take several minutes to complete...

+- ##### before 11:37:14.676 C:\Users\map\AppData\Local\Temp #####
|      Installing the following packages:
|      tmpD60D.tmp
|      By installing you accept licenses for the packages.
|      [NuGet] Installing 'tmpD60D.tmp 1.0.0'.
|      [NuGet] Successfully installed 'tmpD60D.tmp 1.0.0'.
|
|      tmpD60D.tmp v1.0.0 (forced)
|      tmpd60d.tmp package files install completed. Performing other installation steps.
|      + Boxstarter starting Calling Chocolatey to install chocolatey. This may take several minutes to complete...
|      + Boxstarter starting Calling Chocolatey to install chocolatey. This may take several minutes to complete...
|  +-  ##### before 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
|  |   ##### before 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
|  |       Installing the following packages:
|  |       chocolatey
|  |       By installing you accept licenses for the packages.
|  |       chocolatey v0.10.15 already installed.
|  |       Use --force to reinstall, specify a version to install, or try upgrade.
|  |
|  |       Chocolatey installed 0/1 packages. 0 packages failed.
|  |       See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
|  |
|  |       Warnings:
|  |       - chocolatey - chocolatey v0.10.15 already installed.
|  |       Use --force to reinstall, specify a version to install, or try upgrade.
|  |   #####    mid 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey #####
|  |   #####    mid 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey #####
|  |   #####  after 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
|  +-  #####  after 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
|      + Boxstarter finished Calling Chocolatey to install chocolatey. This may take several minutes to complete... 00:00:00.4347155
|      + Boxstarter finished Calling Chocolatey to install chocolatey. This may take several minutes to complete... 00:00:00.4347155
|       The install of tmpd60d.tmp was successful.
|        Software install location not explicitly set, could be in package or
|        default install location if installer.
|
|      Chocolatey installed 1/1 packages. 0 packages failed.
|       See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
|  #####    mid 11:37:14.676 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
+- #####  after 11:37:14.676 C:\Users\map\AppData\Local\Temp #####

+ Boxstarter finished Calling Chocolatey to install tmpD60D.tmp. This may take several minutes to complete... 00:00:03.9730649
True
Boxstarter: Restore Automatic Updates from Windows Update

@pauby
Copy link
Member

pauby commented Oct 13, 2020

The first two make sense:

  • creating the package
  • the outer one that runs the package starting at ##### before 11:37:14.676 C:\Users\map\AppData\Local\Temp ##### and ending at ##### after 11:37:14.676 C:\Users\map\AppData\Local\Temp #####.

The inner one starting at ##### before 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey ##### and ending at ##### after 11:37:18.160 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey ##### doesn't make as much sense, and we'd need to investigate that more.

@me-kell
Copy link
Author

me-kell commented Oct 13, 2020

This workaroung won't help since the $env:temp seems only to be resetted for the outer call.

In subsequent calls even though they start with the initial value of $env:temp apparently chocolatey.dll gets the previous value of it ignoring the value of the Boxstarter-process.

The question is: where does chocolatey.dll store and get the $env:temp?

Here the example with simplified output to visualize the values of $env:temp:

PS C:\> $filename = "$env:userprofile\test_script.ps1"
PS C:\> Set-Content -Path $filename -Value 'choco install chocolatey'
PS C:\> Install-BoxstarterPackage -PackageName "$filename"

+-##### before 12:18:14.256 C:\Users\map\AppData\Local\Temp #####
| #####    mid 12:18:14.256 C:\Users\map\AppData\Local\Temp\chocolatey #####
+-#####  after 12:18:14.256 C:\Users\map\AppData\Local\Temp #####
+-##### before 12:18:15.162 C:\Users\map\AppData\Local\Temp #####
| +-##### before 12:18:19.021 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
| | ##### before 12:18:19.021 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
| | #####    mid 12:18:19.021 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey #####
| | #####    mid 12:18:19.021 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey #####
| | #####  after 12:18:19.021 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
| +-#####  after 12:18:19.021 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
| #####    mid 12:18:15.162 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey #####
+-#####  after 12:18:15.162 C:\Users\map\AppData\Local\Temp #####

PS C:\> Install-BoxstarterPackage -PackageName "$filename"

+-##### before 12:18:22.740 C:\Users\map\AppData\Local\Temp #####
| #####    mid 12:18:22.740 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey #####
+-#####  after 12:18:22.740 C:\Users\map\AppData\Local\Temp #####
+-##### before 12:18:23.131 C:\Users\map\AppData\Local\Temp #####
| +-##### before 12:18:24.490 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
| | ##### before 12:18:24.490 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
| | #####    mid 12:18:24.490 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
| | #####    mid 12:18:24.490 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
| | #####  after 12:18:24.490 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
| +-#####  after 12:18:24.490 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
| #####    mid 12:18:23.131 C:\Users\map\AppData\Local\Temp\chocolatey\chocolatey\chocolatey\chocolatey\chocolatey #####
+-#####  after 12:18:23.131 C:\Users\map\AppData\Local\Temp #####

@me-kell
Copy link
Author

me-kell commented Oct 13, 2020

Is it possible to see the source code for chocolatey.dll? Is it available somewhere? which version does Boxstarter use?

@pauby
Copy link
Member

pauby commented Oct 13, 2020

The question is: where does chocolatey.dll store and get the $env:temp?

That's a Chocolatey question. And as it was fixed in previous releases it's something we are going to need to look at working around or at the very least document.

@pauby
Copy link
Member

pauby commented Oct 13, 2020

Is it possible to see the source code for chocolatey.dll? Is it available somewhere? which version does Boxstarter use?

Have a look at the chocolatey/choco repo. Boxstarter uses 0.10.5 of chocolatey.dll.

@me-kell
Copy link
Author

me-kell commented Oct 13, 2020

After looking at the code I realized that:

  1. running a script with one or more lines with choco install xyz via Install-BoxstarterPackage -PackageName "$filename" Boxstarter makes a temp package which is internally installed via invoke-Boxstarter (outer call to choco install temppackage). The temppackage itself installs xyz also via invoke-Boxstarter (inner call to choco install xyz)
  2. Both Boxstarter and Chocolatey juggle a lot with $env:temp
  3. The nested calls on (1.) and the juggling with $env:temp result in concatenation of chocolatey subfolders under $env:temp. At some time the length of the path produces an error.

Because chocolatey.dll cannot be updated to a newer version (currently 0.10.5) a workaround must at least include the following three steps:

  1. In Boxstarter.Chocolatey\invoke-chocolatey.ps1 capture $env:temp before the try-block and restore the captured value in the finally-block (lines 262-272) (as suggested by @pauby above):
    Enter-BoxstarterLogable {
        Write-BoxstarterMessage "calling choco now with $chocoArgs" -Verbose
        $cd = [System.IO.Directory]::GetCurrentDirectory()
->      $oldEnvTemp = $env:temp
        try {
            # Chocolatey.dll uses GetCurrentDirectory which is not quite right
            # when calling via PowerShell. so we set it here
            Write-BoxstarterMessage "setting current directory location to $((Get-Location).Path)" -Verbose
            [System.IO.Directory]::SetCurrentDirectory("$(Convert-Path (Get-Location).Path)")
            $choco.Run($chocoArgs)
        }
        finally {
            Write-BoxstarterMessage "restoring current directory location to $cd" -Verbose
            [System.IO.Directory]::SetCurrentDirectory($cd)
->          $env:temp = $oldEnvTemp
        }
    }
  1. In Boxstarter.Chocolatey\Invoce-ChocolateyBoxstarter add -cacheLocation $env:temp to the call to Chocolatey install (line 199):
    Chocolatey install $bootstrapPackage `
                -source $source `
                -force:$force `
                -execution-timeout 86400 `
->              -cacheLocation $env:temp
  1. In your script add -cacheLocation $env:temp to the call to choco install xyz:
choco install xyz -cacheLocation $env:temp

I came to this idea from the Chocolatey documentation on Config-Settings at cacheLocation:

It is highly recommended this be set to make Chocolatey more deterministic in cleanup.

UPDATE: See a less invasive workaround below

@pauby
Copy link
Member

pauby commented Oct 14, 2020

@me-kell Thanks for your work on this. If you have it working are you in a position to submit a Pull Request?

@me-kell
Copy link
Author

me-kell commented Oct 14, 2020

@pauby I'm testing another workaround and evaluating the possible consequences of both workarounds. As far as I am sure of the consequences I'll submit a Pull Request.

@me-kell
Copy link
Author

me-kell commented Oct 14, 2020

Another (less invasive than above) workaround should include following steps:

  1. before you run a script with one or more lines having choco install xyz via Install-BoxstarterPackage -PackageName "$filename" enter the following in Boxstarter promt:
choco config set --name="'cacheLocation'" --value="'$env:temp'"
  1. In Boxstarter.Chocolatey\Invoce-ChocolateyBoxstarter add -cacheLocation $env:temp to the call to Chocolatey install (line 199):
    Chocolatey install $bootstrapPackage `
                -source $source `
                -force:$force `
                -execution-timeout 86400 `
->              -cacheLocation $env:temp

This workaround is less invasive since it only needs to change one line of code in Boxstarter.Chocolatey\Invoce-ChocolateyBoxstarter as shown in (2.)

On the other side it needs to run the choco config set --name="'cacheLocation'" --value="'$env:temp'" manually.

@pauby Would it make sense to set this at the start of Boxstarter? Maybe in BoxstarterShell.ps1 after the if(!(Test-Admin)) {...} block?

$_currentChocoCacheLocation = choco config get --name="'cacheLocation'" -r
if ( ($_currentChocoCacheLocation -eq '') -or !(Test-Path $_currentChocoCacheLocation) ) {
    choco config set --name="'cacheLocation'" --value="'$env:temp'" -r
}
Remove-Variable -Name _currentChocoCacheLocation 

@pauby
Copy link
Member

pauby commented Oct 14, 2020

@pauby Would it make sense to set this at the start of Boxstarter? Maybe in BoxstarterShell.ps1 after the if(!(Test-Admin)) {...} block?

I am going to defer than one to @mwallner

@mwallner
Copy link
Member

hey @me-kell - that looks promising, thank you for digging this up!

Considering what you've brought up so far, I'd always vote for the following:
in the Boxstarter Chocolatey wrapper funciton, if cacheLocation is not specified by the user, automagically add it and let it point to $env:temp (should be set to $env:temp/Boxstarter somewhere before imo) - this way, all consecutive chocolatey commands that done via boxstarter should point to the same cacheLocation.

As I've recently made a couple of changes in the package parameter handling, I've got a pretty good idea of where I'd implement that. If you're willing to do a PR that'd be awesome, but I can also offer you to take this over from here if you like.

@mwallner
Copy link
Member

Hey @me-kell , @pauby - I've had a minute to take a spin on it - what do you think about mwallner@8397369 ?
(I've co-authored you as you've basically done all the reasearch @me-kell )

mwallner added a commit to mwallner/boxstarter that referenced this issue Oct 15, 2020
@MisinformedDNA
Copy link

@mwallner Can you create a PR with your code so the team can keep track of it?

@mwallner
Copy link
Member

hey @MisinformedDNA , I totally lost track of this, thanks for bringing it up again!
hopefully I'll have some time this weekend 😃 (unfortunately a day only has 24 hours 😄 )

@mwallner mwallner self-assigned this Jan 29, 2021
@mwallner mwallner removed the 0 - Backlog Issue is accepted, but is not ready to be worked on or not in current sprint label Jan 29, 2021
mwallner added a commit to mwallner/boxstarter that referenced this issue Jan 31, 2021
mwallner added a commit to mwallner/boxstarter that referenced this issue Jan 31, 2021
@pauby pauby added this to the 3.0.0 milestone Oct 25, 2021
@gep13
Copy link
Member

gep13 commented Apr 8, 2022

Due to the fact that the new version of Boxstarter will include the new version of Chocolatey, this issue will no longer happen, and additional is no longer required.

@gep13 gep13 added the 3 - Review Code has been added, and is available for review as a pull request label Apr 25, 2022
@gep13 gep13 added 4 - Done Code has been added to the repository, and has been reviewed by a team member and removed 3 - Review Code has been added, and is available for review as a pull request labels Apr 27, 2022
@gep13 gep13 changed the title $env:temp is changed on every run of Install-BoxstarterPackage Prevent the $env:temp variable from being changed on every run of Install-BoxstarterPackage Apr 27, 2022
@gep13 gep13 modified the milestones: 3.0.0-beta-20220426-14, 3.0.0 Jul 14, 2022
@gep13 gep13 added 5 - Released The issue has been resolved, and released to the public for consumption and removed 4 - Done Code has been added to the repository, and has been reviewed by a team member labels Jul 14, 2022
@choco-bot
Copy link

🎉 This issue has been resolved in version 3.0.0 🎉

The release is available on:

Your GitReleaseManager bot 📦🚀

tveyben added a commit to tveyben/boxstarter that referenced this issue Jan 18, 2023
as the need for this workaround apparently is fixed by this:
chocolatey/boxstarter#442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 - Released The issue has been resolved, and released to the public for consumption Bug Issues where something has happened which was not expected or intended
Projects
None yet
6 participants