Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -757,15 +774,20 @@ 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 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}