From ece27ff68798f508c755ccb84be5ae78b386ac82 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Fri, 30 Jun 2017 15:36:38 -0700 Subject: [PATCH] Fix spurious error generated when importing cmdlets from an in-memory assembly (#4117) --- .../engine/InitialSessionState.cs | 12 ++++++++---- .../engine/Modules/ModuleCmdletBase.cs | 10 +++++++++- .../Language/Parser/ParameterBinding.Tests.ps1 | 6 +++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 6ee87540b..235b1dc94 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -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) { diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 4cf424911..2a5f3cc9e 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -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; } diff --git a/test/powershell/Language/Parser/ParameterBinding.Tests.ps1 b/test/powershell/Language/Parser/ParameterBinding.Tests.ps1 index 038b0dc32..a34b92b16 100644 --- a/test/powershell/Language/Parser/ParameterBinding.Tests.ps1 +++ b/test/powershell/Language/Parser/ParameterBinding.Tests.ps1 @@ -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 }