Skip to content

Commit

Permalink
Fix issue cannot load Microsoft.TestPlatform.CoreUtilities (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Dec 22, 2022
1 parent 9824e5e commit d21b8c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
using System.Reflection;

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;

/*
* /!\ WARNING /!\
* DO NOT USE EQTTRACE IN THIS CLASS AS IT WILL CAUSE LOAD ISSUE BECAUSE OF THE APPDOMAIN
* ASSEMBLY RESOLVER SETUP.
*/

/// <summary>
/// Utility function for Assembly related info
/// The caller is supposed to create AppDomain and create instance of given class in there.
Expand Down Expand Up @@ -49,16 +54,11 @@ public string[] GetFullPathToDependentAssemblies(string assemblyPath, out IList<
Assembly? assembly;
try
{
EqtTrace.Verbose($"AssemblyLoadWorker.GetFullPathToDependentAssemblies: Reflection loading {assemblyPath}.");

// First time we load in LoadFromContext to avoid issues.
assembly = _assemblyUtility.ReflectionOnlyLoadFrom(assemblyPath);
}
catch (Exception ex)
{
EqtTrace.Error($"AssemblyLoadWorker.GetFullPathToDependentAssemblies: Reflection loading of {assemblyPath} failed:");
EqtTrace.Error(ex);

warnings.Add(ex.Message);
return Array.Empty<string>(); // Otherwise just return no dependencies.
}
Expand Down Expand Up @@ -91,8 +91,9 @@ public string[] GetFullPathToDependentAssemblies(string assemblyPath, out IList<
/// </summary>
/// <param name="path">Path of the assembly file.</param>
/// <returns> String representation of the target dotNet framework e.g. .NETFramework,Version=v4.0. </returns>
internal string GetTargetFrameworkVersionStringFromPath(string path)
internal string GetTargetFrameworkVersionStringFromPath(string path, out string? errorMessage)
{
errorMessage = null;
if (!File.Exists(path))
{
return string.Empty;
Expand All @@ -105,17 +106,11 @@ internal string GetTargetFrameworkVersionStringFromPath(string path)
}
catch (BadImageFormatException)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("AssemblyHelper:GetTargetFrameworkVersionString() caught BadImageFormatException. Falling to native binary.");
}
errorMessage = "AssemblyHelper:GetTargetFrameworkVersionString() caught BadImageFormatException. Falling to native binary.";
}
catch (Exception ex)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("AssemblyHelper:GetTargetFrameworkVersionString() Returning default. Unhandled exception: {0}.", ex);
}
errorMessage = $"AssemblyHelper:GetTargetFrameworkVersionString() Returning default. Unhandled exception: {ex}.";
}

return string.Empty;
Expand Down Expand Up @@ -168,7 +163,6 @@ private void ProcessChildren(Assembly assembly, IList<string> result, ISet<strin
{
DebugEx.Assert(assembly != null, "assembly");

EqtTrace.Verbose($"AssemblyLoadWorker.GetFullPathToDependentAssemblies: Processing assembly {assembly.FullName}.");
foreach (AssemblyName reference in assembly.GetReferencedAssemblies())
{
GetDependentAssembliesInternal(reference.FullName, result, visitedAssemblies, warnings);
Expand All @@ -178,7 +172,6 @@ private void ProcessChildren(Assembly assembly, IList<string> result, ISet<strin
var modules = Array.Empty<Module>();
try
{
EqtTrace.Verbose($"AssemblyLoadWorker.GetFullPathToDependentAssemblies: Getting modules of {assembly.FullName}.");
modules = assembly.GetModules();
}
catch (FileNotFoundException e)
Expand Down Expand Up @@ -249,8 +242,6 @@ private void GetDependentAssembliesInternal(string assemblyString, IList<string>
Assembly? assembly;
try
{
EqtTrace.Verbose($"AssemblyLoadWorker.GetDependentAssembliesInternal: Reflection loading {assemblyString}.");

string postPolicyAssembly = AppDomain.CurrentDomain.ApplyPolicy(assemblyString);
DebugEx.Assert(!StringEx.IsNullOrEmpty(postPolicyAssembly), "postPolicyAssembly");

Expand All @@ -259,15 +250,11 @@ private void GetDependentAssembliesInternal(string assemblyString, IList<string>
}
catch (Exception ex)
{
EqtTrace.Error($"AssemblyLoadWorker.GetDependentAssembliesInternal: Reflection loading {assemblyString} failed:.");
EqtTrace.Error(ex);

string warning = string.Format(CultureInfo.CurrentCulture, Resource.MissingDeploymentDependency, assemblyString, ex.Message);
warnings.Add(warning);
return;
}

EqtTrace.Verbose($"AssemblyLoadWorker.GetDependentAssembliesInternal: Assembly {assemblyString} was added as dependency.");
result.Add(assembly.Location);

ProcessChildren(assembly, result, visitedAssemblies, warnings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ internal static string GetTargetFrameworkVersionString(string testSourcePath)
typeof(AssemblyLoadWorker),
null);

return assemblyLoadWorker.GetTargetFrameworkVersionStringFromPath(testSourcePath);
var targetFramework = assemblyLoadWorker.GetTargetFrameworkVersionStringFromPath(testSourcePath, out var errorMessage);

if (errorMessage is not null)
{
EqtTrace.Error(errorMessage);
}

return targetFramework;
}
catch (Exception exception)
{
Expand Down

0 comments on commit d21b8c2

Please sign in to comment.