Fix spurious error generated when importing cmdlets from an in-memory assembly (#4117)

This commit is contained in:
Bruce Payette 2017-06-30 15:36:38 -07:00 committed by Dongbo Wang
parent 3e35fb9918
commit ece27ff687
3 changed files with 22 additions and 6 deletions

View file

@ -4494,10 +4494,14 @@ namespace System.Management.Automation.Runspaces
string throwAwayHelpFile = null;
PSSnapInHelpers.AnalyzePSSnapInAssembly(assembly, assemblyPath, null, module, true, out cmdlets, out aliases, out providers, out throwAwayHelpFile);
SessionStateAssemblyEntry assemblyEntry =
new SessionStateAssemblyEntry(assembly.FullName, assemblyPath);
this.Assemblies.Add(assemblyEntry);
// If this is an in-memory assembly, don't added it to the list of AssemblyEntries
// since it can't be loaded by path or name
if (! string.IsNullOrEmpty(assembly.Location))
{
SessionStateAssemblyEntry assemblyEntry =
new SessionStateAssemblyEntry(assembly.FullName, assemblyPath);
this.Assemblies.Add(assemblyEntry);
}
if (cmdlets != null)
{

View file

@ -6843,6 +6843,15 @@ namespace Microsoft.PowerShell.Commands
assemblyVersion = GetAssemblyVersionNumber(assemblyToLoad);
assembly = assemblyToLoad;
// If this is an in-memory only assembly, add it directly to the assembly cache if
// it isn't already there.
if (string.IsNullOrEmpty(assembly.Location))
{
if (! Context.AssemblyCache.ContainsKey(assembly.FullName))
{
Context.AssemblyCache.Add(assembly.FullName, assembly);
}
}
}
else
{
@ -7196,7 +7205,6 @@ namespace Microsoft.PowerShell.Commands
{
AddModuleToModuleTables(this.Context, this.TargetSessionState.Internal, module);
}
return module;
}

View file

@ -47,7 +47,7 @@ Describe 'Argument transformation attribute on optional argument with explicit $
'@
$mod = Add-Type -PassThru -TypeDefinition $tdefinition
Import-Module $mod[0].Assembly
Import-Module $mod[0].Assembly -ErrorVariable ErrorImportingModule
function Invoke-ScriptFunctionTakesObject
{
@ -68,6 +68,10 @@ Describe 'Argument transformation attribute on optional argument with explicit $
}
It "There was no error importing the in-memory module" {
$ErrorImportingModule | Should Be $null
}
It "Script function takes object" {
Invoke-ScriptFunctionTakesObject | Should Be 42
}