Skip to content

Razor EA: load from ServiceHubCore #77720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 31, 2025
Merged
Changes from 3 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
12 changes: 12 additions & 0 deletions src/Tools/ExternalAccess/Razor/RazorAnalyzerAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal sealed class RazorAnalyzerAssemblyResolver() : IAnalyzerAssemblyResolve
public const string RazorUtilsAssemblyName = "Microsoft.AspNetCore.Razor.Utilities.Shared";
public const string ObjectPoolAssemblyName = "Microsoft.Extensions.ObjectPool";

private const string ServiceHubCoreFolderName = "ServiceHubCore";

internal static readonly ImmutableArray<string> RazorAssemblyNames = [RazorCompilerAssemblyName, RazorUtilsAssemblyName, ObjectPoolAssemblyName];

public Assembly? Resolve(AnalyzerAssemblyLoader loader, AssemblyName assemblyName, AssemblyLoadContext directoryContext, string directory) =>
Expand Down Expand Up @@ -54,6 +56,16 @@ internal sealed class RazorAnalyzerAssemblyResolver() : IAnalyzerAssemblyResolve
}

var assemblyFileName = $"{assemblyName.Name}.dll";

// Depending on who wins the race to load these assemblies, the base directory will either be the tooling root (if Roslyn wins)
// or the ServiceHubCore subfolder (razor). In the root directory these are netstandard2.0 targeted, in ServiceHubCore they are
// .net targeted. We need to always pick the same set of assemblies regardless of who causes us to load. Because this code only
// runs in a .net based host, it's safe to always choose the .net targeted ServiceHubCore versions.
if (!directory.TrimEnd('/', '\\').EndsWith(ServiceHubCoreFolderName))
{
directory = Path.Combine(directory, ServiceHubCoreFolderName);
}

var assemblyPath = Path.Combine(directory, assemblyFileName);
if (File.Exists(assemblyPath))
{
Expand Down
Loading