From 3bcf996372d4f52a651e517fbbb0cbab9e0c4a64 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Mon, 15 Jul 2024 20:06:03 +0200 Subject: [PATCH] Avoid BuildHost crash in Mono due to missing types The LoadProjectFileAsync routine calls EnsureMSBuildLoaded to make sure the Microsoft.Build.dll is accessible, but requires types from that assembly within the routine itself. This may not always work with Mono, as the JIT may look up the types during compilation. Fixed by splitting out a LoadProjectFileCoreAsync routine, similarly to what is already done for GetProjectsInSolution. Fixes https://github.com/dotnet/runtime/issues/101121. --- src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs b/src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs index 08e8008ad8046..c74d8ff18494f 100644 --- a/src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs +++ b/src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs @@ -161,9 +161,18 @@ private void EnsureMSBuildLoaded(string projectFilePath) /// /// Returns the target ID of the object created for this. /// - public async Task LoadProjectFileAsync(string projectFilePath, string languageName, CancellationToken cancellationToken) + public Task LoadProjectFileAsync(string projectFilePath, string languageName, CancellationToken cancellationToken) { EnsureMSBuildLoaded(projectFilePath); + return LoadProjectFileCoreAsync(projectFilePath, languageName, cancellationToken); + } + + // When using the Mono runtime, the MSBuild types used in this method must be available + // to the JIT during compilation of the method, so they have to be loaded by the caller; + // therefore this method must not be inlined. + [MethodImpl(MethodImplOptions.NoInlining)] + private async Task LoadProjectFileCoreAsync(string projectFilePath, string languageName, CancellationToken cancellationToken) + { CreateBuildManager(); ProjectFileLoader projectLoader = languageName switch