Skip to content

[ci] Enable MSBuild multithreaded mode with MSBUILDENABLEMULTITHREADED - #13753

Open
jonathanpeppers wants to merge 4 commits into
mainfrom
jonathanpeppers-ci-msbuild-multithreaded
Open

jonathanpeppers wants to merge 4 commits into
mainfrom
jonathanpeppers-ci-msbuild-multithreaded

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 9390e4a4af49abb251175b5c87e85fda71dfc2c1

Dimension Assessment Rationale
Change confidence 7/10 ↑ On Windows, Prepare.proj run with the local 12.0 alpha SDK under -mt succeeded end to end; binlog shows strict multithreaded mode and the 12.0 alpha MSBuildSDKsPath.
↓ The Makefile change itself was not run (no macOS/Linux machine); CI has not run on this revision.
↓ Later build steps (make jenkins, tests) under -mt are not verified.
Diagnosis confidence 9/10 ↑ Local repro gave the same MSB4216; only an inherited MSBuildSDKsPath causes it (DOTNET_HOST_PATH and MSBuildExtensionsPath alone pass).
Evidence fit 9/10 ↑ The CI error names the bootstrap SDK path dotnet-bootstrap/sdk/11.0.100-rc.3.26522.148/MSBuild from the Prepare.proj <Exec/>, which matches the repro.

Context: https://github.com/dotnet/msbuild/blob/main/documentation/wiki/MSBuild-Environment-Variables.md
Context: #12061

MSBuild reads MSBUILDENABLEMULTITHREADED=1 and then runs as if -mt was passed. An explicit -mt:false still turns it off. Set it in variables.yaml so every CI build, including the sample projects that tests build, runs in multithreaded mode.

The signing stages (dotnet_prepare_release in the official and internal pipelines) and the macOS entitlements step set it to 0. The internal job builds workload MSIs with VS MSBuild.exe, which Arcade keeps out of -mt (dotnet/dotnet#9989). The opt-out applies on all branches, so main signs the same way as release/*. The internal signing steps still get -mt from Arcade's build.ps1 for the outer dotnet build, as they do today.

MSBUILDFORCEMULTITHREADED=1 also exists, but it ignores -mt:false, so it is not used.

make prepare uses the local SDK

With -mt, macOS make prepare failed:

Microsoft.Common.CurrentVersion.targets(3473,5): error MSB4216: Could not run the "GenerateResource" task because MSBuild could not create or connect to a task host with runtime "NET" and architecture "arm64". ... the required executable "/Users/cloudtest/vss/_work/_temp/dotnet-bootstrap/sdk/11.0.100-rc.3.26522.148/MSBuild" exists and can be run. [src/Microsoft.Android.Build.BaseTasks/Microsoft.Android.Build.BaseTasks.csproj]

Since #12061, make prepare runs Prepare.proj with the system dotnet, and Prepare.proj runs <Exec/> on the local SDK ($(DotNetPreviewTool)). The dotnet CLI sets MSBuildSDKsPath to its own Sdks folder before it runs MSBuild, and a child dotnet keeps an inherited value. So the local MSBuild imported the system SDK's Microsoft.NET.Sdk. That mix worked until now. With -mt, GenerateResource runs in a .NET task host that starts from that SDK's $(NetCoreSdkRoot), and its handshake with the newer MSBuild fails.

make prepare already runs install-dotnet first, so Prepare.proj now runs with the local SDK, and the outer and inner builds use the same SDK. The SYSTEM_DOTNET_BINLOG helper has no other callers and is removed. On Windows CI, Prepare runs on a clean agent before the local SDK exists, so $(DotNetPreviewTool) is the system dotnet and there is no mix.

Validation

  • A small project built with SDK 11.0.100 and -v:diag logged Multi-threaded strict mode is enabled with the variable set, and did not with -mt:false.
  • The pinned SDK (VMR 6e6f978f) and release/11.0.1xx both read the variable in Traits.cs.
  • Repro: SDK 11.0.100 <Exec/> of the 12.0.100-alpha.1.26556.115 dotnet build on a project with a .resx gave the same MSB4216. Setting each inherited variable by itself showed that only MSBuildSDKsPath fails (303 s timeout); DOTNET_HOST_PATH and MSBuildExtensionsPath alone pass.
  • MSBUILDENABLEMULTITHREADED=1 bin/Debug/dotnet/dotnet build build-tools/scripts/Prepare.proj -t:Prepare on Windows (local 12.0 alpha SDK as the outer build, BaseTasks obj deleted first): succeeded. The BootstrapTasks binlog shows strict multithreaded mode and the 12.0 alpha MSBuildSDKsPath.
  • The Makefile change was not run locally (no macOS/Linux machine). The rest of the SDK build and tests were not run locally under -mt.

jonathanpeppers and others added 4 commits November 24, 2026 10:23
Set MSBUILDENABLEMULTITHREADED=1 in the shared CI variables so MSBuild
runs with -mt by default. Set it to 0 for the signing stages and the
macOS entitlements step, because they run VS MSBuild.exe and signing
tooling that has not been validated in multithreaded mode.

Co-authored-by: Copilot App <[email protected]>
Prepare.proj runs on the bootstrap SDK and <Exec/>s the local SDK.
The outer dotnet sets MSBuildSDKsPath to its own Sdks folder, and the
inner dotnet keeps an inherited value, so the inner build imported
the bootstrap SDK's Microsoft.NET.Sdk. With -mt, GenerateResource runs
in a .NET task host started from $(NetCoreSdkRoot) of that SDK, and
the handshake with the newer MSBuild fails with MSB4216.

Co-authored-by: Copilot App <[email protected]>
`make prepare` ran Prepare.proj with the system `dotnet`, and
Prepare.proj then runs the local SDK with <Exec/>. The child inherited
the outer SDK's MSBuildSDKsPath, so the local MSBuild imported the
system SDK's Microsoft.NET.Sdk. With -mt this fails with MSB4216.

`install-dotnet` runs first, so run Prepare.proj with the local SDK
and drop the MSBuildSDKsPath workaround and the unused
SYSTEM_DOTNET_BINLOG helper.

Co-authored-by: Copilot App <[email protected]>
@jonathanpeppers
jonathanpeppers marked this pull request as ready for review November 25, 2026 15:29
Copilot AI balanced review requested due to automatic review settings November 25, 2026 15:29
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new default makes non-multithreaded test cases run multithreaded too; both false branches need explicit -mt:false to preserve ordinary MSBuild coverage.

1 open issue
What changed in this PR

Enables MSBuild multithreaded mode by default in CI, including builds launched by tests, while adding targeted signing opt-outs. Updates Unix preparation to use the local SDK and avoid mixing SDK versions in nested builds.

Changes:

  • Sets MSBUILDENABLEMULTITHREADED=1 globally in CI, preserving explicit -mt:false overrides.
  • Disables the environment-variable default for release signing and macOS entitlements steps.
  • Switches make prepare to the local SDK, removes the unused system-dotnet helper, and documents CI behavior.
File Description
Makefile Runs preparation with the local SDK.
Documentation/​guides/​MSBuildBestPractices.md Documents multithreaded CI defaults and opt-outs.
build-tools/​scripts/​msbuild.mk Removes the unused system-dotnet helper.
build-tools/​automation/​yaml-templates/​variables.yaml Enables multithreaded mode by default.
build-tools/​automation/​yaml-templates/​commercial-build.yaml Adds an opt-out for macOS entitlements.
build-tools/​automation/​yaml-templates/​build-macos.yaml Adds an opt-out for macOS entitlements.
build-tools/​automation/​azure-pipelines.yaml Adds a release-signing stage opt-out.
build-tools/​automation/​azure-pipelines-internal.yaml Adds a signing and MSI creation stage opt-out.

Review effort: Balanced

Comment on lines +96 to +97
- name: MSBUILDENABLEMULTITHREADED
value: '1'
@jonathanpeppers

Copy link
Copy Markdown
Member Author

🤖 Evidence that CI ran MSBuild in -mt mode

I replayed the android-tools-logs-windows/build-windows.binlog artifact from this PR's build (1697985) and from a build without this change (1699110, #13777):

This PR Without this change
-mt on the command line no no
Multi-threaded strict mode is enabled message, process directory moved to MT-sentinel-CWD yes no
Tasks started in an external .NET task host 15 0
Nodes used 2 1

Both builds ran the same command line (MSBuild.exe -maxcpucount --verbosity:m ...) on SDK 11.0.100-rc.3.26522.148, so MSBUILDENABLEMULTITHREADED=1 is the only cause of the difference.

The task host launches are tasks that are not marked as multithreaded-safe, which MSBuild moves out of process only in -mt mode. For example:

Launching task "GenerateResource" from assembly "Microsoft.Build.Tasks.Core, ..." in an external task host with a runtime of "NET" and a process architecture of "x64".

The other 14 are SourceLink tasks (LocateRepository, GetSourceLinkUrl, GenerateSourceLinkFile, ...). The macOS binlog (android-tools-logs-mac/build-mac.binlog) also has the Multi-threaded strict mode is enabled message.

Limit: these binlogs cover only the Android tools test build. A successful run does not upload binlogs for the SDK build (make jenkins) or the MSBuild tests unless XA.PublishAllLogs=true.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants