[MSBuild] Use DisableInProcNode so that NuGet dll from MSBuild are loaded in a separate process. This should hopefully avoid any future clash between our NuGet dll and MSBuild ones.

This commit is contained in:
Virgile Bello 2021-11-04 22:55:38 +09:00
Родитель 9374d4f33f
Коммит 231f558392
2 изменённых файлов: 19 добавлений и 3 удалений

Просмотреть файл

@ -69,6 +69,7 @@ namespace Stride.Core.Assets
if (MSBuildInstance == null)
throw new InvalidOperationException("Could not find a MSBuild installation (expected 16.0 or later)");
SetupMSBuildCurrentHostForOutOfProc(MSBuildInstance.MSBuildPath);
CheckMSBuildToolset();
// Reset MSBUILD_EXE_PATH once MSBuild is resolved, to not spook child process (had issues with ThisProcess(MSBuild)->CompilerApp(net472): CompilerApp couldn't load MSBuild project properly)
@ -105,6 +106,18 @@ namespace Stride.Core.Assets
return s_msBuildAssemblies.Contains(assemblyName.Name, StringComparer.OrdinalIgnoreCase);
}
private static void SetupMSBuildCurrentHostForOutOfProc(string dotNetSdkPath)
{
// Workaround for https://github.com/dotnet/msbuild/pull/7013 (dotnet.exe not properly detected by MSBuild so it fallbacks to launching our own executable instead)
var currentHostField = typeof(Microsoft.Build.Evaluation.Project).Assembly
.GetType("Microsoft.Build.BackEnd.NodeProviderOutOfProcBase")?
.GetField("CurrentHost", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
if (currentHostField != null)
{
currentHostField.SetValue(null, Path.Combine(new DirectoryInfo(dotNetSdkPath).Parent.Parent.FullName, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet"));
}
}
private static void CheckMSBuildToolset()
{
// Check that we can create a project

Просмотреть файл

@ -129,7 +129,8 @@ namespace Stride.Core.Assets
{
var parameters = new BuildParameters(pc)
{
Loggers = new[] { new LoggerRedirect(logger, true) } //Instance of ILogger instantiated earlier
Loggers = new[] { new LoggerRedirect(logger, true) }, //Instance of ILogger instantiated earlier
DisableInProcNode = true,
};
// Run a MSBuild /t:Restore <projectfile>
@ -168,7 +169,8 @@ namespace Stride.Core.Assets
{
var parameters = new BuildParameters(pc)
{
Loggers = new[] { new LoggerRedirect(logger, true) } //Instance of ILogger instantiated earlier
Loggers = new[] { new LoggerRedirect(logger, true) }, //Instance of ILogger instantiated earlier
DisableInProcNode = true,
};
// Run a MSBuild /t:Restore <projectfile>
@ -339,7 +341,8 @@ namespace Stride.Core.Assets
var buildResult = mainBuildManager.Build(
new BuildParameters(project.ProjectCollection)
{
Loggers = new[] { logger }
Loggers = new[] { logger },
DisableInProcNode = true,
},
new BuildRequestData(projectInstance, targets.Split(';'), null, flags));