Fix multi node build

This commit is contained in:
Matt Parker
2025-08-02 21:39:00 +10:00
parent c5f8901ad4
commit 91ea2c77c3
2 changed files with 38 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using Ardalis.GuardClauses;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
@@ -22,8 +23,13 @@ public class BuildService
var normalOut = Console.Out;
Console.SetOut(BuildTextWriter);
var terminalLogger = InternalTerminalLoggerFactory.CreateLogger();
Console.SetOut(normalOut);
var nodesToBuildWith = GetBuildNodeCount(Environment.ProcessorCount);
var buildParameters = new BuildParameters
{
MaxNodeCount = nodesToBuildWith,
DisableInProcNode = true,
Loggers =
[
//new BinaryLogger { Parameters = "msbuild.binlog" },
@@ -32,16 +38,8 @@ public class BuildService
//new InMemoryLogger(LoggerVerbosity.Normal)
],
};
Console.SetOut(normalOut);
string[] targetsToBuild = buildType switch
{
BuildType.Build => ["Restore", "Build"],
BuildType.Rebuild => ["Restore", "Rebuild"],
BuildType.Clean => ["Clean"],
BuildType.Restore => ["Restore"],
_ => throw new ArgumentOutOfRangeException(nameof(buildType), buildType, null)
};
var targetsToBuild = TargetsToBuild(buildType);
var buildRequest = new BuildRequestData(
projectFullPath : solutionFilePath,
globalProperties: new Dictionary<string, string?>(),
@@ -68,6 +66,33 @@ public class BuildService
Console.WriteLine($"Build result: {buildResult.OverallResult} in {timer.ElapsedMilliseconds}ms");
}).ConfigureAwait(false);
}
private static string[] TargetsToBuild(BuildType buildType)
{
string[] targetsToBuild = buildType switch
{
BuildType.Build => ["Restore", "Build"],
BuildType.Rebuild => ["Restore", "Rebuild"],
BuildType.Clean => ["Clean"],
BuildType.Restore => ["Restore"],
_ => throw new ArgumentOutOfRangeException(nameof(buildType), buildType, null)
};
return targetsToBuild;
}
private static int GetBuildNodeCount(int processorCount)
{
var nodesToBuildWith = processorCount switch
{
1 or 2 => 1,
3 or 4 => 2,
>= 5 and <= 10 => processorCount - 2,
> 10 => processorCount - 4,
_ => throw new ArgumentOutOfRangeException(nameof(processorCount))
};
Guard.Against.NegativeOrZero(nodesToBuildWith, nameof(nodesToBuildWith));
return nodesToBuildWith;
}
}
// To build a single project

View File

@@ -7,13 +7,15 @@
</PropertyGroup>
<ItemGroup>
<!-- If any Microsoft.Build.dll ends up in the output, it will be prioritised for loading by MSBuild Nodes -->
<PackageReference Include="Ardalis.GuardClauses" Version="5.0.0" />
<PackageReference Include="Microsoft.Build" Version="17.14.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.14.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.9.1" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.14.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.14.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.14.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.14.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.VisualStudio.SolutionPersistence" Version="1.0.52" />
<PackageReference Include="NuGet.Protocol" Version="6.14.0" />
</ItemGroup>