Skip to content

Commit

Permalink
Merge pull request #19328 from unoplatform/mergify/bp/release/stable/…
Browse files Browse the repository at this point in the history
…5.6/pr-19326

fix: Ensure the assembly loader is registered early (backport #19326)
  • Loading branch information
jeromelaban authored Jan 25, 2025
2 parents 9773d00 + ce4bfc0 commit 1b3e705
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static string BuildMSBuildPath(string? workDir)
throw new InvalidOperationException("Unable to find dotnet SDK base path");
}

private static void RegisterAssemblyLoader()
public static void RegisterAssemblyLoader()
{
// Force assembly loader to consider siblings, when running in a separate appdomain / ALC.
Assembly? Load(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -42,7 +43,10 @@ private bool InitializeMetadataUpdater(ConfigureServer configureServer)

if (_useRoslynHotReload)
{
CompilationWorkspaceProvider.InitializeRoslyn(Path.GetDirectoryName(configureServer.ProjectPath));
// Assembly registrations must be done before the workspace is initialized
// Not doing so will cause the roslyn msbuild workspace to fail to load because
// of a missing path on assemblies loaded from a memory stream.
CompilationWorkspaceProvider.RegisterAssemblyLoader();

InitializeInner(configureServer);

Expand All @@ -54,7 +58,22 @@ private bool InitializeMetadataUpdater(ConfigureServer configureServer)
}
}

private void InitializeInner(ConfigureServer configureServer) => _initializeTask = Task.Run(
private void InitializeInner(ConfigureServer configureServer)
{
if (Assembly.Load("Microsoft.CodeAnalysis.Workspaces") is { } wsAsm)
{
// If this assembly was loaded from a stream, it will not have a location.
// This will indicate that the assembly loader from CompilationWorkspaceProvider
// has been registered too late.
if (string.IsNullOrEmpty(wsAsm.Location))
{
throw new InvalidOperationException("Microsoft.CodeAnalysis.Workspaces was loaded from a stream and must loaded from a known path");
}
}

CompilationWorkspaceProvider.InitializeRoslyn(Path.GetDirectoryName(configureServer.ProjectPath));

_initializeTask = Task.Run(
async () =>
{
try
Expand Down Expand Up @@ -103,6 +122,7 @@ private void InitializeInner(ConfigureServer configureServer) => _initializeTask
}
},
CancellationToken.None);
}

private void ObserveSolutionPaths(Solution solution)
{
Expand Down

0 comments on commit 1b3e705

Please sign in to comment.