Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4505,13 +4505,15 @@ internal string FixupFileName(string moduleBase, string name, string extension)
result += extension;
}

//For dlls, we cannot get the path from the provider.
//We need to load the assembly and then get the path.
//If the module is already loaded, this is not expensive since the assembly is already loaded in the AppDomain
// For dlls, we cannot get the path from the provider.
// We need to load the assembly and then get the path.
// If the module is already loaded, this is not expensive since the assembly is already loaded in the AppDomain
// If the dll is not loaded, we load it from the resolved path.
// We attempt to load it from the resolved path before we try to look up in GAC on Windows.
if (!string.IsNullOrEmpty(ext) && ext.Equals(".dll", StringComparison.OrdinalIgnoreCase))
{
Exception ignored = null;
Assembly assembly = ExecutionContext.LoadAssembly(name, null, out ignored);
Assembly assembly = ExecutionContext.LoadAssembly(name, result, out ignored);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we already got the resolved assembly path, we can skip the loading, right? The loading is solely for getting the path of the assembly. When the target assembly is in GAC, we have to load it in order to get the path, but not if we already resolved the assembly file path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we actually want to load from module base first. In the Resolve method, we do not have access to module base so we need to do this here. If there are alternatives, please suggest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment to the code so it is clear to other developers that we really want to load the assembly here, not just return the pathname. Thanks!

if (assembly != null)
{
result = assembly.Location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ namespace ModuleCmdlets
}

}

It 'Should load from ModuleBase path before looking up in GAC' -Skip:(-not $IsWindows) {
$module = Get-Module PSScheduledJob -ListAvailable -SkipEditionCheck
$moduleBasePath = Split-Path $module.Path
$destPath = New-Item -ItemType Directory -Path "$TestDrive/PSScheduledJob"
$gacAssemblyPath = (Get-ChildItem "${env:WinDir}\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.ScheduledJob" -Recurse -Filter 'Microsoft.PowerShell.ScheduledJob.dll').FullName

# Copy format, type and psd1
Copy-Item "$moduleBasePath/PSScheduledJob*.*" -Destination $destPath -Force

# Copy assembly to temp module folder"
Copy-Item $gacAssemblyPath -Destination $destPath -Force

# Use a different pwsh so that we do not have the PSScheduledJob module already loaded.
$loadedAssemblyLocation = pwsh -noprofile -c "Import-Module $destPath -Force; [Microsoft.PowerShell.ScheduledJob.AddJobTriggerCommand].Assembly.Location"
$loadedAssemblyLocation | Should -BeLike "$TestDrive*\Microsoft.PowerShell.ScheduledJob.dll"
}
}

Describe "Import-Module should be case insensitive" -Tags 'CI' {
Expand Down