From 36dc96af0e45a625b8eba6d6b66154bde6884a61 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 6 Aug 2018 11:12:39 -0700 Subject: [PATCH] Fix error message for `Add-Type` when `-AssemblyName` with wildcard is not found (#7444) --- .../commands/utility/AddType.cs | 34 +++++++++++++++---- .../Add-Type.Tests.ps1 | 10 ++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 7d9ee05ca..a9b6280b8 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 @@ namespace Microsoft.PowerShell.Commands 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 @@ namespace Microsoft.PowerShell.Commands 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,15 +774,20 @@ namespace Microsoft.PowerShell.Commands } // Look up the assembly in the current folder - string currentFolderPath = SessionState.Path.GetResolvedPSPathFromPSPath(refAssemblyDll)[0].Path; - if (File.Exists(currentFolderPath)) + var resolvedPaths = SessionState.Path.GetResolvedPSPathFromPSPath(refAssemblyDll); + + if (resolvedPaths.Count > 0) { - return currentFolderPath; + 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)), + string.Format(ParserStrings.ErrorLoadingAssembly, assembly)), "ErrorLoadingAssembly", ErrorCategory.InvalidOperation, assembly); 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 525722849..b1a431b9e 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,14 @@ 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_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) + { Add-Type -AssemblyName $assemblyName } | Should -Throw -ErrorId $errorid + } }