Skip to content

Commit

Permalink
[ashmind/SharpLab#1335] Provide additional information when failing t…
Browse files Browse the repository at this point in the history
…o load Roslyn internals
  • Loading branch information
ashmind committed Jun 5, 2024
1 parent 01c541b commit 14d8ccc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Common/Internal/Roslyn/RoslynInternals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private static Assembly LoadInternalsAssemblyWithDependenciesSlowUncached() {
?? LoadInternalsAssemblySlow(new Version(4, 2));
}

PreloadInternalAssemblyDependenciesSlow(assembly);
PreloadInternalsAssemblyDependenciesSlow(assembly);
EnsureInternalsTypesCanLoad(assembly);
return assembly;
}

Expand All @@ -76,6 +77,20 @@ private static Assembly LoadInternalsAssemblyWithDependenciesSlowUncached() {
return assembly;
}

private static void EnsureInternalsTypesCanLoad(Assembly assembly) {
try {
_ = assembly.DefinedTypes;
}
catch (ReflectionTypeLoadException ex) {
throw new Exception(
$"Failed to load MirrorSharp Roslyn internals.{Environment.NewLine}" +
$" * Microsoft.CodeAnalysis {RoslynAssemblies.MicrosoftCodeAnalysis.GetName().Version}.{Environment.NewLine}" +
$" * {assembly.GetName().Name}",
ex
);
}
}

private static Assembly LoadInternalsAssemblySlow(Version roslynVersion) {
var assemblyName = roslynVersion switch {
{ Major: > 4 } or { Major: 4, Minor: >= 11 } => "MirrorSharp.Internal.Roslyn411.dll",
Expand Down Expand Up @@ -112,7 +127,7 @@ private static Assembly LoadInternalsAssemblySlow(Version roslynVersion) {
#endif
}

private static void PreloadInternalAssemblyDependenciesSlow(Assembly assembly) {
private static void PreloadInternalsAssemblyDependenciesSlow(Assembly assembly) {
foreach (var reference in assembly.GetReferencedAssemblies()) {
Assembly.Load(reference);
}
Expand Down

0 comments on commit 14d8ccc

Please sign in to comment.