Skip to content

Commit fb2a2f0

Browse files
committed
(chocolateyGH-1670) Add noshims options for install and upgrade
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.
1 parent 564a652 commit fb2a2f0

File tree

53 files changed

+1669
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1669
-32
lines changed

Scenarios.md

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Chocolatey Usage Scenarios
22

3-
### ChocolateyInstallCommand [ 35 Scenario(s), 293 Observation(s) ]
3+
### ChocolateyInstallCommand [ 42 Scenario(s), 312 Observation(s) ]
44

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

@@ -247,6 +247,20 @@
247247
* should not install a package in the lib directory
248248
* should put a package in the lib bad directory
249249

250+
#### when installing a package that tries to overwrite another shim
251+
252+
* should have a shim
253+
* should have a shim with target in other package tools folder
254+
* should have an error message
255+
* should have shim target
256+
257+
#### when installing a package that tries to overwrite its own shim
258+
259+
* should create a shim
260+
* should have a shim with target in tools folder
261+
* should have an error message
262+
* should have shim targets
263+
250264
#### when installing a package with a dependent package that also depends on a less constrained but still valid dependency of the same package
251265

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

299+
#### when installing a package with dependencies and noshims
300+
301+
* should create a shim for the dependency
302+
* should have shim targets
303+
* should not create a shim for the package
304+
305+
#### when installing a package with dependencies and noshimsglobal
306+
307+
* should have shim targets
308+
* should not create a shim for the dependency
309+
* should not create a shim for the package
310+
285311
#### when installing a package with dependencies happy
286312

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

355+
#### when installing a package with install bin file
356+
357+
* should not see the shim as an existing shim and remove it
358+
359+
#### when installing a package with install bin file and noshims
360+
361+
* should have shim target
362+
* should not create a shim
363+
329364
#### when installing a package with no sources enabled
330365

331366
* should have no sources enabled result
332367
* should not install any packages
333368

369+
#### when installing a package with noshims
370+
371+
* should have shim target
372+
* should not create a shim for the package
373+
334374
#### when installing a side by side package
335375

336376
* config should match package result name
@@ -675,7 +715,7 @@
675715

676716
* should throw an error that it is not allowed
677717

678-
### ChocolateyUpgradeCommand [ 36 Scenario(s), 295 Observation(s) ]
718+
### ChocolateyUpgradeCommand [ 37 Scenario(s), 295 Observation(s) ]
679719

680720
#### when force upgrading a package
681721

@@ -920,6 +960,9 @@
920960
* should have no sources enabled result
921961
* should not have any packages upgraded
922962

963+
#### when upgrading a package with noshims
964+
965+
923966
#### when upgrading a package with readonly files
924967

925968
* should contain a warning message that it upgraded successfully

src/chocolatey.resources/helpers/functions/Install-BinFile.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ param(
8686

8787
Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters
8888

89+
if ($env:ChocolateyNoShims) {
90+
Write-Debug "File shimming disabled for `'$($env:ChocolateyPackageName)`'."
91+
Write-Debug "Removing any existing shim for `'$name`'."
92+
Uninstall-BinFile $name $path
93+
return
94+
}
95+
8996
$nugetPath = [System.IO.Path]::GetFullPath((Join-Path "$helpersPath" '..\'))
9097
$nugetExePath = Join-Path "$nugetPath" 'bin'
9198
$packageBatchFileName = Join-Path $nugetExePath "$name.bat"

src/chocolatey.tests.integration/Scenario.cs

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ private static ChocolateyConfiguration baseline_configuration()
177177
config.PinCommand.Name = string.Empty;
178178
config.PinCommand.Command = PinCommandType.unknown;
179179
config.ListCommand.IdOnly = false;
180+
config.NoShims = false;
181+
config.NoShimsGlobal = false;
180182

181183
return config;
182184
}

src/chocolatey.tests.integration/chocolatey.tests.integration.csproj

+99
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,105 @@
346346
<None Include="context\nonterminatingerror\1.0\tools\chocolateyInstall.ps1">
347347
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
348348
</None>
349+
<None Include="context\shims\shimbasepackage\1.0.0\shimbasepackage.nuspec">
350+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
351+
</None>
352+
<None Include="context\shims\shimbasepackage\1.0.0\tools\chocolateyinstall.ps1">
353+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
354+
</None>
355+
<None Include="context\shims\shimbasepackage\1.0.0\tools\chocolateyuninstall.ps1">
356+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
357+
</None>
358+
<None Include="context\shims\shimbasepackage\1.0.0\tools\shimbasepackage1.exe.">
359+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
360+
</None>
361+
<None Include="context\shims\shimhasdependency\1.0.0\shimhasdependency.nuspec">
362+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
363+
</None>
364+
<None Include="context\shims\shimhasdependency\1.0.0\tools\chocolateyinstall.ps1">
365+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
366+
</None>
367+
<None Include="context\shims\shimhasdependency\1.0.0\tools\chocolateyuninstall.ps1">
368+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
369+
</None>
370+
<None Include="context\shims\shimhasdependency\1.0.0\tools\shimhasdependency1.exe">
371+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
372+
</None>
373+
<None Include="context\shims\shimoverwrite\1.0.0\shimoverwrite.nuspec">
374+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
375+
</None>
376+
<None Include="context\shims\shimoverwrite\1.0.0\tools\chocolateyinstall.ps1">
377+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
378+
</None>
379+
<None Include="context\shims\shimoverwrite\1.0.0\tools\chocolateyuninstall.ps1">
380+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
381+
</None>
382+
<None Include="context\shims\shimoverwrite\1.0.0\tools\shimoverwrite1.exe">
383+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
384+
</None>
385+
<None Include="context\shims\shimoverwrite\1.0.0\tools\install\shimoverwrite1.exe">
386+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
387+
</None>
388+
<None Include="context\shims\shimoverwriteother\1.0.0\shimoverwriteother.nuspec">
389+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
390+
</None>
391+
<None Include="context\shims\shimoverwriteother\1.0.0\tools\chocolateyinstall.ps1">
392+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
393+
</None>
394+
<None Include="context\shims\shimoverwriteother\1.0.0\tools\chocolateyuninstall.ps1">
395+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
396+
</None>
397+
<None Include="context\shims\shimoverwriteother\1.0.0\tools\shimbasepackage1.exe">
398+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
399+
</None>
400+
<None Include="context\shims\shimupgrade\1.0.0\shimupgrade.nuspec">
401+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
402+
</None>
403+
<None Include="context\shims\shimupgrade\1.0.0\tools\chocolateyinstall.ps1">
404+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
405+
</None>
406+
<None Include="context\shims\shimupgrade\1.0.0\tools\chocolateyuninstall.ps1">
407+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
408+
</None>
409+
<None Include="context\shims\shimupgrade\1.0.0\tools\shimupgrade1.exe">
410+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
411+
</None>
412+
<None Include="context\shims\shimupgrade\2.0.0\shimupgrade.nuspec">
413+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
414+
</None>
415+
<None Include="context\shims\shimupgrade\2.0.0\tools\chocolateyinstall.ps1">
416+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
417+
</None>
418+
<None Include="context\shims\shimupgrade\2.0.0\tools\chocolateyuninstall.ps1">
419+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
420+
</None>
421+
<None Include="context\shims\shimupgrade\2.0.0\tools\shimupgrade2.exe">
422+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
423+
</None>
424+
<None Include="context\shims\shimupgrade\3.0.0\shimupgrade.nuspec">
425+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
426+
</None>
427+
<None Include="context\shims\shimupgrade\3.0.0\tools\chocolateyinstall.ps1">
428+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
429+
</None>
430+
<None Include="context\shims\shimupgrade\3.0.0\tools\chocolateyuninstall.ps1">
431+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
432+
</None>
433+
<None Include="context\shims\shimupgrade\3.0.0\tools\shimupgrade3.exe">
434+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
435+
</None>
436+
<None Include="context\shims\shimwithbinfile\1.0.0\shimwithbinfile.nuspec">
437+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
438+
</None>
439+
<None Include="context\shims\shimwithbinfile\1.0.0\tools\chocolateyinstall.ps1">
440+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
441+
</None>
442+
<None Include="context\shims\shimwithbinfile\1.0.0\tools\chocolateyuninstall.ps1">
443+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
444+
</None>
445+
<None Include="context\shims\shimwithbinfile\1.0.0\tools\shimwithbinfile1.bat">
446+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
447+
</None>
349448
<None Include="context\testing.packages.config">
350449
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
351450
</None>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>shimbasepackage</id>
5+
<version>1.0.0</version>
6+
<title>shimbasepackage</title>
7+
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
8+
<owners>__REPLACE_YOUR_NAME__</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<description>__REPLACE__</description>
11+
<summary>__REPLACE__</summary>
12+
<tags>shimbasepackage</tags>
13+
</metadata>
14+
<files>
15+
<file src="tools\**" target="tools" />
16+
</files>
17+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>shimhasdependency</id>
5+
<version>1.0.0</version>
6+
<title>shimhasdependency</title>
7+
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
8+
<owners>__REPLACE_YOUR_NAME__</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<description>__REPLACE__</description>
11+
<summary>__REPLACE__</summary>
12+
<tags>shimhasdependency</tags>
13+
<dependencies>
14+
<dependency id="shimbasepackage" version="1.0.0" />
15+
</dependencies>
16+
</metadata>
17+
<files>
18+
<file src="tools\**" target="tools" />
19+
</files>
20+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>shimoverwrite</id>
5+
<version>1.0.0</version>
6+
<title>shimoverwrite</title>
7+
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
8+
<owners>__REPLACE_YOUR_NAME__</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<description>__REPLACE__</description>
11+
<summary>__REPLACE__</summary>
12+
<tags>overwrite-own</tags>
13+
</metadata>
14+
<files>
15+
<file src="tools\**" target="tools" />
16+
</files>
17+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>shimoverwriteother</id>
5+
<version>1.0.0</version>
6+
<title>shimoverwriteother</title>
7+
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
8+
<owners>__REPLACE_YOUR_NAME__</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<description>__REPLACE__</description>
11+
<summary>__REPLACE__</summary>
12+
<tags>shimoverwriteother</tags>
13+
<dependencies>
14+
<dependency id="shimbasepackage" version="1.0.0" />
15+
</dependencies>
16+
</metadata>
17+
<files>
18+
<file src="tools\**" target="tools" />
19+
</files>
20+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>shimupgrade</id>
5+
<version>1.0.0</version>
6+
<title>shimupgrade</title>
7+
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
8+
<owners>__REPLACE_YOUR_NAME__</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<description>__REPLACE__</description>
11+
<summary>__REPLACE__</summary>
12+
<tags>shimupgrade</tags>
13+
</metadata>
14+
<files>
15+
<file src="tools\**" target="tools" />
16+
</files>
17+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>shimupgrade</id>
5+
<version>2.0.0</version>
6+
<title>shimupgrade</title>
7+
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
8+
<owners>__REPLACE_YOUR_NAME__</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<description>This version throws an error when installing</description>
11+
<summary>__REPLACE__</summary>
12+
<tags>shimupgrade</tags>
13+
</metadata>
14+
<files>
15+
<file src="tools\**" target="tools" />
16+
</files>
17+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
try {
2+
Write-Output "This is $packageName v$packageVersion being installed to `n '$packageFolder'."
3+
Write-Error "Oh no! An error"
4+
throw "We had an error captain!"
5+
} catch {
6+
throw $_.Exception
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.0.0

0 commit comments

Comments
 (0)