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
19 changes: 12 additions & 7 deletions src/System.Management.Automation/engine/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,21 +1196,19 @@ internal void RemoveAssembly(string name)
}
}


[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadWithPartialName")]
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]
internal static Assembly LoadAssembly(string name, string filename, out Exception error)
{
// First we try to load the assembly based on the filename

Assembly loadedAssembly = null;
error = null;

if (!String.IsNullOrEmpty(filename))
{
try
{
loadedAssembly = Assembly.LoadFrom(filename);
return loadedAssembly;
}
catch (FileNotFoundException fileNotFound)
{
Expand All @@ -1219,17 +1217,22 @@ internal static Assembly LoadAssembly(string name, string filename, out Exceptio
catch (FileLoadException fileLoadException)
{
error = fileLoadException;
return null;
}
catch (BadImageFormatException badImage)
{
error = badImage;
return null;
}
catch (SecurityException securityException)
{
error = securityException;
return null;
}
}
else if (!String.IsNullOrEmpty(name))

// Then we try to load the assembly based on the given name
if (!String.IsNullOrEmpty(name))
{
string fixedName = null;
// Remove the '.dll' if it's there...
Expand All @@ -1252,8 +1255,6 @@ internal static Assembly LoadAssembly(string name, string filename, out Exceptio
catch (FileLoadException fileLoadException)
{
error = fileLoadException;
// this is a legitimate error on CoreCLR for a newly emited with Add-Type assemblies
// they cannot be loaded by name, but we are only interested in importing them by path
}
catch (BadImageFormatException badImage)
Copy link
Member

Choose a reason for hiding this comment

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

The comments in the catch (FileLoadException fileLoadException) block right above this line seems not needed anymore, since we now try loading from path first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved

{
Expand All @@ -1265,7 +1266,11 @@ internal static Assembly LoadAssembly(string name, string filename, out Exceptio
}
}

// We either return the loaded Assembly, or return null.
// If the assembly is loaded, we ignore error as it may come from the filepath loading.
if (loadedAssembly != null)
{
error = null;
}
return loadedAssembly;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,24 @@ namespace ModuleCmdlets
$results[0] | Should Be $path
$results[1] | Should BeExactly "BinaryModuleCmdlet1 exported by the ModuleCmdlets module."
}

It "PS should try to load the assembly from assembly name if file path doesn't exist" {

$psdFile = Join-Path $TESTDRIVE test.psd1
$nestedModule = Join-Path NOExistedPath Microsoft.PowerShell.Commands.Utility.dll
New-ModuleManifest -Path $psdFile -NestedModules $nestedModule
try
{
$module = Import-Module $psdFile -PassThru
$module.NestedModules | Should Not BeNullOrEmpty
$assemblyLocation = [Microsoft.PowerShell.Commands.AddTypeCommand].Assembly.Location
$module.NestedModules.ImplementingAssembly.Location | Should Be $assemblyLocation
}
finally
{
Remove-Module $module -ErrorAction SilentlyContinue
}

}
}