From d335eaf155107a6fb3fda0643a42d82751f83795 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 Aug 2018 10:59:17 -0700 Subject: [PATCH 1/3] fix error message if assemblyname with wildcard is not found --- .../commands/utility/AddType.cs | 11 ++++++++--- .../Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 7d9ee05cac0..5bd4a42c767 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -757,10 +757,15 @@ private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) } // Look up the assembly in the current folder - string currentFolderPath = SessionState.Path.GetResolvedPSPathFromPSPath(refAssemblyDll)[0].Path; - if (File.Exists(currentFolderPath)) + var psPaths = SessionState.Path.GetResolvedPSPathFromPSPath(refAssemblyDll); + + if (psPaths.Count > 0) { - return currentFolderPath; + string currentFolderPath = psPaths[0].Path; + if (File.Exists(currentFolderPath)) + { + return currentFolderPath; + } } ErrorRecord errorRecord = new ErrorRecord( diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 index 5257228493b..9b268b6da48 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 @@ -226,4 +226,12 @@ public class AttributeTest$guid : PSCmdlet New-Item -Path $VBFile -ItemType File -Force > $null { Add-Type -Path $VBFile } | Should -Throw -ErrorId "EXTENSION_NOT_SUPPORTED,Microsoft.PowerShell.Commands.AddTypeCommand" } + + It "Throw terminating error when specified assembly is not found: " -TestCases @( + @{ assemblyName = "does_not_exist_with_wildcard_*"; errorid = "ErrorLoadingAssembly,Microsoft.PowerShell.Commands.AddTypeCommand"}, + @{ assemblyName = "does_not_exist"; errorid = "PathNotFound,Microsoft.PowerShell.Commands.AddTypeCommand"} + ) { + param ($assemblyName, $errorid) + { Add-Type -AssemblyName $assemblyName } | Should -Throw -ErrorId $errorid + } } From fdbf50baded7b06078200be1d046e3044bb24739 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 Aug 2018 14:34:18 -0700 Subject: [PATCH 2/3] fix case where assembly name is relative path --- .../commands/utility/AddType.cs | 27 +++++++++++++++---- .../Add-Type.Tests.ps1 | 2 ++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 5bd4a42c767..b96bf027d09 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -701,6 +701,8 @@ private static string GetReferenceAssemblyPathBasedOnType(Type type) private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) { + ErrorRecord errorRecord; + // if it's a path, resolve it if (assembly.Contains(PathType.DirectorySeparatorChar) || assembly.Contains(PathType.AltDirectorySeparatorChar)) { @@ -711,7 +713,22 @@ private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) else { var paths = SessionState.Path.GetResolvedPSPathFromPSPath(assembly); - return paths[0].Path; + if (paths.Count > 0) + { + return paths[0].Path; + } + else + { + errorRecord = new ErrorRecord( + new Exception( + String.Format(ParserStrings.ErrorLoadingAssembly, assembly)), + "ErrorLoadingAssembly", + ErrorCategory.InvalidOperation, + assembly); + + ThrowTerminatingError(errorRecord); + return null; + } } } @@ -757,18 +774,18 @@ private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) } // Look up the assembly in the current folder - var psPaths = SessionState.Path.GetResolvedPSPathFromPSPath(refAssemblyDll); + var resolvedPaths = SessionState.Path.GetResolvedPSPathFromPSPath(refAssemblyDll); - if (psPaths.Count > 0) + if (resolvedPaths.Count > 0) { - string currentFolderPath = psPaths[0].Path; + string currentFolderPath = resolvedPaths[0].Path; if (File.Exists(currentFolderPath)) { return currentFolderPath; } } - ErrorRecord errorRecord = new ErrorRecord( + errorRecord = new ErrorRecord( new Exception( String.Format(ParserStrings.ErrorLoadingAssembly, assembly)), "ErrorLoadingAssembly", diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 index 9b268b6da48..b1a431b9e57 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 @@ -229,6 +229,8 @@ public class AttributeTest$guid : PSCmdlet It "Throw terminating error when specified assembly is not found: " -TestCases @( @{ assemblyName = "does_not_exist_with_wildcard_*"; errorid = "ErrorLoadingAssembly,Microsoft.PowerShell.Commands.AddTypeCommand"}, + @{ assemblyName = "../does_not_exist_with_wildcard_*"; errorid = "ErrorLoadingAssembly,Microsoft.PowerShell.Commands.AddTypeCommand"}, + @{ assemblyName = "${PSHOME}/does_not_exist"; errorid = "System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand"}, @{ assemblyName = "does_not_exist"; errorid = "PathNotFound,Microsoft.PowerShell.Commands.AddTypeCommand"} ) { param ($assemblyName, $errorid) From b18cf93680a622dc2d7d2e4e35f6ef9b7fe200f1 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 3 Aug 2018 14:51:03 -0700 Subject: [PATCH 3/3] address codefactor issue --- .../commands/utility/AddType.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index b96bf027d09..a9b6280b8bb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -721,7 +721,7 @@ private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) { errorRecord = new ErrorRecord( new Exception( - String.Format(ParserStrings.ErrorLoadingAssembly, assembly)), + string.Format(ParserStrings.ErrorLoadingAssembly, assembly)), "ErrorLoadingAssembly", ErrorCategory.InvalidOperation, assembly); @@ -787,7 +787,7 @@ private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) errorRecord = new ErrorRecord( new Exception( - String.Format(ParserStrings.ErrorLoadingAssembly, assembly)), + string.Format(ParserStrings.ErrorLoadingAssembly, assembly)), "ErrorLoadingAssembly", ErrorCategory.InvalidOperation, assembly);