Skip to content

Commit

Permalink
Fix exception case in compiler resolver (#75000)
Browse files Browse the repository at this point in the history
* Fix exception case in compiler resolver

This fixes how we deal with exceptions in the compiler assembly resolver.

* better exception semantics
  • Loading branch information
jaredpar authored Sep 5, 2024
1 parent 8ce8103 commit 1ac3ea0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NET
using System.Runtime.Loader;
using System.Reflection;
using Xunit;

namespace Microsoft.CodeAnalysis.UnitTests;

public sealed class CompilerAnalyzerAssemblyResolverTests
{
[Fact]
public void ExceptionReturnsNull()
{
var context = new AssemblyLoadContext(nameof(ExceptionReturnsNull), isCollectible: true);
var resolver = new AnalyzerAssemblyLoader.CompilerAnalyzerAssemblyResolver(context);
var name = new AssemblyName("NotARealAssembly");
Assert.Null(resolver.ResolveAssembly(name));
context.Unload();
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,19 @@ internal sealed class CompilerAnalyzerAssemblyResolver(AssemblyLoadContext compi
{
private readonly AssemblyLoadContext _compilerAlc = compilerContext;

public Assembly? ResolveAssembly(AssemblyName assemblyName) => _compilerAlc.LoadFromAssemblyName(assemblyName);
public Assembly? ResolveAssembly(AssemblyName assemblyName)
{
try
{
return _compilerAlc.LoadFromAssemblyName(assemblyName);
}
catch
{
// The LoadFromAssemblyName method will throw if the assembly cannot be found. Need
// to catch this exception and return null to satisfy the interface contract.
return null;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis.ErrorReporting;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
Expand Down Expand Up @@ -415,7 +416,7 @@ internal string GetRealAnalyzerLoadPath(string originalFullPath)
return resolvedAssembly;
}
}
catch
catch (Exception ex) when (FatalError.ReportAndCatch(ex, ErrorSeverity.Diagnostic))
{
// Ignore if the external resolver throws
}
Expand Down

0 comments on commit 1ac3ea0

Please sign in to comment.