Skip to content

Commit

Permalink
(chocolateyGH-1670) Add noshims options for install and upgrade
Browse files Browse the repository at this point in the history
The --noshims and --noshimsglobal options allow the user to stop shims
being created for either the package, or the package and its
dependencies.

In addition to this new behaviour, the implementation handles the
creation and removal of shims more robustly, so that shims from other
packages (or the same package) cannot be unintentionally overwritten or
removed.

The new ShimRegistry class keeps track of the currently installed shims
and their corresponding packages. It does this by examining all the exe
files in the shim directory (note that old-style .bat and unixy shims
are not supported) and extracting the target file from the binary. If
this target path contains the lib folder, then the package name can be
obtained.

ShimRegistry is updated for each package, just before the Powershell
scripts are run, using `ShimGenerationService::take_snapshot`. When
ShimGenerationService installs the package, ShimRegistry provides its
existing shim files (ie. those that were not modified or removed since
the last update) so that they can be deleted prior to installing any
new ones.

To stop shims being added from Install-BinFile, a new environment
variable `chocolateyNoShims` is used when running the Powershell
scripts. If a package sets a shim this way, but then forgets to call
Uninstall-BinFile when uninstalling, the shim will be removed anyway if
its target is in the package folder.
  • Loading branch information
johnstevenson committed Feb 17, 2020
1 parent 564a652 commit fb2a2f0
Show file tree
Hide file tree
Showing 53 changed files with 1,669 additions and 32 deletions.
47 changes: 45 additions & 2 deletions Scenarios.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Chocolatey Usage Scenarios

### ChocolateyInstallCommand [ 35 Scenario(s), 293 Observation(s) ]
### ChocolateyInstallCommand [ 42 Scenario(s), 312 Observation(s) ]

#### when force installing a package that depends on an unavailable newer version of an installed dependency forcing dependencies

Expand Down Expand Up @@ -247,6 +247,20 @@
* should not install a package in the lib directory
* should put a package in the lib bad directory

#### when installing a package that tries to overwrite another shim

* should have a shim
* should have a shim with target in other package tools folder
* should have an error message
* should have shim target

#### when installing a package that tries to overwrite its own shim

* should create a shim
* should have a shim with target in tools folder
* should have an error message
* should have shim targets

#### when installing a package with a dependent package that also depends on a less constrained but still valid dependency of the same package

* [PENDING] should contain a message that everything installed successfully
Expand Down Expand Up @@ -282,6 +296,18 @@
* should not install a package in the lib directory
* should not install the dependency in the lib directory

#### when installing a package with dependencies and noshims

* should create a shim for the dependency
* should have shim targets
* should not create a shim for the package

#### when installing a package with dependencies and noshimsglobal

* should have shim targets
* should not create a shim for the dependency
* should not create a shim for the package

#### when installing a package with dependencies happy

* should contain a message that everything installed successfully
Expand Down Expand Up @@ -326,11 +352,25 @@
* [PENDING] should not install the conflicting package in the lib directory
* [PENDING] should not upgrade the exact version dependency

#### when installing a package with install bin file

* should not see the shim as an existing shim and remove it

#### when installing a package with install bin file and noshims

* should have shim target
* should not create a shim

#### when installing a package with no sources enabled

* should have no sources enabled result
* should not install any packages

#### when installing a package with noshims

* should have shim target
* should not create a shim for the package

#### when installing a side by side package

* config should match package result name
Expand Down Expand Up @@ -675,7 +715,7 @@

* should throw an error that it is not allowed

### ChocolateyUpgradeCommand [ 36 Scenario(s), 295 Observation(s) ]
### ChocolateyUpgradeCommand [ 37 Scenario(s), 295 Observation(s) ]

#### when force upgrading a package

Expand Down Expand Up @@ -920,6 +960,9 @@
* should have no sources enabled result
* should not have any packages upgraded

#### when upgrading a package with noshims


#### when upgrading a package with readonly files

* should contain a warning message that it upgraded successfully
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ param(

Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters

if ($env:ChocolateyNoShims) {
Write-Debug "File shimming disabled for `'$($env:ChocolateyPackageName)`'."
Write-Debug "Removing any existing shim for `'$name`'."
Uninstall-BinFile $name $path
return
}

$nugetPath = [System.IO.Path]::GetFullPath((Join-Path "$helpersPath" '..\'))
$nugetExePath = Join-Path "$nugetPath" 'bin'
$packageBatchFileName = Join-Path $nugetExePath "$name.bat"
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ private static ChocolateyConfiguration baseline_configuration()
config.PinCommand.Name = string.Empty;
config.PinCommand.Command = PinCommandType.unknown;
config.ListCommand.IdOnly = false;
config.NoShims = false;
config.NoShimsGlobal = false;

return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,105 @@
<None Include="context\nonterminatingerror\1.0\tools\chocolateyInstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimbasepackage\1.0.0\shimbasepackage.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimbasepackage\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimbasepackage\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimbasepackage\1.0.0\tools\shimbasepackage1.exe.">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimhasdependency\1.0.0\shimhasdependency.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimhasdependency\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimhasdependency\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimhasdependency\1.0.0\tools\shimhasdependency1.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwrite\1.0.0\shimoverwrite.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwrite\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwrite\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwrite\1.0.0\tools\shimoverwrite1.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwrite\1.0.0\tools\install\shimoverwrite1.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwriteother\1.0.0\shimoverwriteother.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwriteother\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwriteother\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimoverwriteother\1.0.0\tools\shimbasepackage1.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\1.0.0\shimupgrade.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\1.0.0\tools\shimupgrade1.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\2.0.0\shimupgrade.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\2.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\2.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\2.0.0\tools\shimupgrade2.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\3.0.0\shimupgrade.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\3.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\3.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimupgrade\3.0.0\tools\shimupgrade3.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimwithbinfile\1.0.0\shimwithbinfile.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimwithbinfile\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimwithbinfile\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\shims\shimwithbinfile\1.0.0\tools\shimwithbinfile1.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\testing.packages.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>shimbasepackage</id>
<version>1.0.0</version>
<title>shimbasepackage</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<tags>shimbasepackage</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>shimhasdependency</id>
<version>1.0.0</version>
<title>shimhasdependency</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<tags>shimhasdependency</tags>
<dependencies>
<dependency id="shimbasepackage" version="1.0.0" />
</dependencies>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>shimoverwrite</id>
<version>1.0.0</version>
<title>shimoverwrite</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<tags>overwrite-own</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>shimoverwriteother</id>
<version>1.0.0</version>
<title>shimoverwriteother</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<tags>shimoverwriteother</tags>
<dependencies>
<dependency id="shimbasepackage" version="1.0.0" />
</dependencies>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>shimupgrade</id>
<version>1.0.0</version>
<title>shimupgrade</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<tags>shimupgrade</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>shimupgrade</id>
<version>2.0.0</version>
<title>shimupgrade</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This version throws an error when installing</description>
<summary>__REPLACE__</summary>
<tags>shimupgrade</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
try {
Write-Output "This is $packageName v$packageVersion being installed to `n '$packageFolder'."
Write-Error "Oh no! An error"
throw "We had an error captain!"
} catch {
throw $_.Exception
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.0.0

0 comments on commit fb2a2f0

Please sign in to comment.