Skip to content

Commit

Permalink
(chocolatey#2333) Disable loading of DLL under extensions path
Browse files Browse the repository at this point in the history
As there isn't a way to determine whether a DLL is a usable PowerShell
module without loading it into memory, we're better off not loading DLLs
directly, as it has caused issues several times in the past
(see: chocolatey#2078, chocolatey#1041).
This has previously required us to create an ever-expanding list of
exceptions to the loading behaviour.

Instead, if folks want to load DLLs as powershell modules, they can
package them alongside a PSM1 file that handles the DLL import itself.
This removes the need for Chocolatey to guess which dlls are powershell
modules and which should not be loaded.
  • Loading branch information
pauby authored and gep13 committed Sep 2, 2021
1 parent 18b253a commit 3168622
Showing 1 changed file with 0 additions and 30 deletions.
30 changes: 0 additions & 30 deletions src/chocolatey.resources/helpers/chocolateyInstaller.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,6 @@ $currentAssemblies = [System.AppDomain]::CurrentDomain.GetAssemblies()
$extensionsPath = Join-Path "$helpersPath" '..\extensions'
if (Test-Path($extensionsPath)) {
Write-Debug 'Loading community extensions'
Get-ChildItem $extensionsPath -recurse -filter "*.dll" | Select -ExpandProperty FullName | % {
$path = $_;
if ($path.Contains("extensions\chocolatey\lib-synced")) { continue }

try {
Write-Debug "Importing '$path'";
$fileNameWithoutExtension = $([System.IO.Path]::GetFileNameWithoutExtension($path))
Write-Debug "Loading '$fileNameWithoutExtension' extension.";
$loaded = $false
$currentAssemblies | % {
$name = $_.GetName().Name
if ($name -eq $fileNameWithoutExtension) {
Import-Module $_
$loaded = $true
}
}

if (!$loaded) {
if ($fileNameWithoutExtension -ne "chocolateygui.licensed") {
Import-Module $path;
}
}
} catch {
if ($env:ChocolateyPowerShellHost -eq 'true') {
Write-Warning "Import failed for '$path'. Error: '$_'"
} else {
Write-Warning "Import failed for '$path'. If it depends on a newer version of the .NET framework, please make sure you are using the built-in PowerShell Host. Error: '$_'"
}
}
}
#Resolve-Path $extensionsPath\**\*\*.psm1 | % { Write-Debug "Importing `'$_`'"; Import-Module $_.ProviderPath }
Get-ChildItem $extensionsPath -recurse -filter "*.psm1" | Select -ExpandProperty FullName | % { Write-Debug "Importing `'$_`'"; Import-Module $_; }
}
Expand Down

0 comments on commit 3168622

Please sign in to comment.