Fix error message for Add-Type
when -AssemblyName
with wildcard is not found (#7444)
This commit is contained in:
parent
097e14426f
commit
36dc96af0e
|
@ -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);
|
||||
|
|
|
@ -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: <assemblyName>" -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue