Skip to content

Add MSBuild multithreaded build support to the CsWinRT tasks - #2671

Merged
Sergio Pedri (Sergio0694) merged 7 commits into
staging/3.0from
user/sergiopedri/msbuild-multithreaded-tasks
Nov 26, 2026
Merged

Sergio Pedri (Sergio0694) merged 7 commits into
staging/3.0from
user/sergiopedri/msbuild-multithreaded-tasks

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Summary

Add support for MSBuild multithreaded builds (-mt) to all CsWinRT 3.0 build tasks, by multi-targeting WinRT.Generator.Tasks as net472 and net10.0 and using the real MSBuild multithreading APIs.

Motivation

MSBuild 18.x can run tasks in-process in parallel (-mt), but only tasks marked with [MSBuildMultiThreadableTask] opt in (the others run in a slower sidecar process). In this mode the process current directory and environment are no longer project-scoped, so tasks must not rely on them. The Microsoft.Build.* 18.x packages that define these APIs only ship net472 and net10.0 assemblies, so the tasks can no longer target netstandard2.0. net472 is still needed because Visual Studio and MSBuild.exe run tasks on .NET Framework, while the dotnet CLI runs them on .NET.

Changes

  • src/WinRT.Generator.Tasks/: all six tasks are marked [MSBuildMultiThreadableTask]. The five ToolTask-based tasks inherit IMultiThreadableTask from ToolTask, while GenerateCsWinRTStubExes implements it directly and starts cl.exe from TaskEnvironment.GetProcessStartInfo(). Every path a task touches itself is resolved with TaskEnvironment.GetAbsolutePath (via a new Extensions/TaskEnvironmentExtensions.cs helper).
  • src/WinRT.Generator.Tasks/WinRT.Generator.Tasks.csproj: now targets net472;net10.0 and references Microsoft.Build.Utilities.Core 18.7.1 (the first version where ToolTask supports multithreaded builds) and PolySharp (new central package version in src/Directory.Packages.props). Also sets PlatformTarget=AnyCPU and emits SupportedOSPlatform on net10.0.
  • nuget/Microsoft.Windows.CsWinRT.targets: new _CsWinRTValidateMSBuildVersion target that fails early with a clear message when MSBuild is older than 18.7.
  • nuget/Microsoft.Windows.CsWinRT.CsWinRTGen.targets, src/Directory.Build.props: CsWinRTGenTasksAssembly now selects the net10.0 or net472 task assembly based on $(MSBuildRuntimeType).
  • nuget/Microsoft.Windows.CsWinRT.nuspec, src/build.cmd, build/AzurePipelineTemplates/: ship, stage and sign both task assemblies (tools\net10.0\ and tools\net472\).
  • src/Tests/InteropGeneratorTest/: point at the net10.0 task assembly and add Test_MultiThreadableTasks, which checks that every task in both assemblies has the attribute and IMultiThreadableTask.
  • .github/: update the generator tasks section of the Copilot instructions and the interop generator skill.

Notes

  • This raises the minimum supported MSBuild version to 18.7. The CI agents need Visual Studio with MSBuild 18.7 or later.
  • Verified locally by building both targets in Debug and Release, running Test_ServerGC and Test_MultiThreadableTasks, and running a task with a relative path from a different current directory under dotnet msbuild -mt (net10.0) and the Visual Studio MSBuild.exe (net472). The pipelines and NuGet packing were not run.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Multi-target WinRT.Generator.Tasks as net472 and net10.0 and use the MSBuild multithreaded build APIs (MSBuildMultiThreadableTask, IMultiThreadableTask, TaskEnvironment) in all tasks. Require MSBuild 18.7 or later, the first version where ToolTask supports them. Ship both task assemblies in the NuGet package and select one by MSBuildRuntimeType.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A project reference to a multi-targeted project only builds the target framework compatible with the referencing project, so the net472 tasks were not built. Desktop MSBuild consumers load the net472 assembly, and Test_MultiThreadableTasks opens both, so explicitly reference each target framework.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A bare 'cl' was looked up by CreateProcess using the PATH of the MSBuild process rather than the environment table supplied by TaskEnvironment, so it failed when the compiler was only on the task's PATH under -mt. Add ProcessStartInfo.ResolveFileName, which searches the PATH of the start info for bare names, and use it before launching cl.exe.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ectory

ToolTask passes the tool path to Process as is, so a relative ToolPath was resolved against the current directory of the shared MSBuild process, and failed under -mt. Override GetProcessStartInfo in all tool tasks to resolve the file name, enforce it with Test_MultiThreadableTasks, and document it.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Sergio0694
Sergio Pedri (Sergio0694) force-pushed the user/sergiopedri/msbuild-multithreaded-tasks branch from 74b4edd to b303234 Compare November 26, 2026 00:21
TaskEnvironment.GetAbsolutePath leaves root-relative (\foo) and drive-relative (C:foo) paths unchanged, so Windows resolved them against the drive and directory of the MSBuild process rather than the project, which failed under -mt when the host was on another drive. Qualify these against the project drive or directory in TryGetAbsolutePath, and add unit tests for the path qualification.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Sergio0694
Sergio Pedri (Sergio0694) force-pushed the user/sergiopedri/msbuild-multithreaded-tasks branch from 9c27760 to 0a2f0b7 Compare November 26, 2026 01:51
@Sergio0694
Sergio Pedri (Sergio0694) merged commit 8cc1b89 into staging/3.0 Nov 26, 2026
20 checks passed
@Sergio0694
Sergio Pedri (Sergio0694) deleted the user/sergiopedri/msbuild-multithreaded-tasks branch November 26, 2026 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants