Skip to content

Add -mt and -nodereuse switches to build scripts - #8151

Merged
ViktorHofer merged 12 commits into
mainfrom
msbuild-mt-switch
Aug 10, 2026
Merged

ViktorHofer merged 12 commits into
mainfrom
msbuild-mt-switch

Conversation

@ViktorHofer

@ViktorHofer ViktorHofer commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Implements dotnet/arcade#17242.

MSBuild's experimental multi-threaded mode was previously gated behind an internal MSBUILD_MT_ENABLED environment variable. This promotes it to a first-class build script switch.

The switch defaults to off for now, on CI and locally, so this is a no-op for existing builds. Enabling it by default for local builds is deliberately deferred until the mode has proven out; that will be a one-line change per tools.ps1/tools.sh.

Resolution semantics

Identical across the PowerShell and shell variants:

  1. An explicitly specified -mt / --mt value always wins, on CI and locally.
  2. Otherwise it's off.

tools.ps1 / tools.sh own the default resolution and emit -mt; the build scripts just expose the switch.

-mt takes a value, consistent with the existing -nodeReuse and -warnAsError parameters:

.\build.cmd -mt 1     # local build with multi-threading
./build.sh --mt 1     # same on Unix

Changes

Area Change
eng/common and src/arcade/eng/common tools.ps1/tools.sh resolve $msbuildMultiThreaded / msbuild_multi_threaded and emit -mt; build.ps1/build.sh expose -mt <value> (long form -msbuildMultiThreaded)
build.sh and eng/build.ps1 The VMR's own entry points don't go through eng/common/build.*, so they expose the switch directly. An explicit value is also flowed down as /p:DotNetBuildMT=<value>, so a single -mt 0 covers the orchestrator and every repo build.
fsharp, roslyn, aspnetcore, nuget-client These repos don't use eng/common/build.*, so their own build scripts gained -mt and -nodeReuse. nuget-client's eng/dotnet-build/build.sh invokes msbuild directly and therefore resolves the defaults itself.
repo-projects/Directory.Build.props Replaced the MSBUILD_MT_ENABLED=1 environment variable with the new switch. DotNetBuildMT now passes -mt <value>, so it can be used to opt out as well as in.

All other repos pick the change up automatically — the UpdateEngCommonFiles target copies the VMR's eng/common over every src/<repo>/eng/common before that repo builds.

Repos with custom build scripts

fsharp, roslyn, aspnetcore and nuget-client don't call eng/common/build.* directly, so both switches had to be added to their own scripts. fsharp and roslyn reject unknown arguments outright, and aspnetcore, fsharp and nuget-client hard-disabled node reuse, so an explicitly passed value was silently dropped. Each script now declares the parameter and only applies its own default when the caller didn't specify one.

msbuild and runtime do forward unrecognized arguments to eng/common/build.*, but only runtime did so in a way that preserves argument types. msbuild's wrapper re-invoked Arcade with pwsh -File, which passes every argument as a literal string, and a PowerShell [bool] parameter can never bind from a string:

Cannot process argument transformation on parameter 'msbuildMultiThreaded'.
Cannot convert value "System.String" to type "System.Boolean".

It now uses pwsh -Command, which re-parses the arguments as PowerShell so -mt 1 arrives as an integer — the same thing runtime's wrapper achieves with Invoke-Expression, and what msbuild's own build.cmd already does when it calls this script. The build still runs out-of-proc so that stage 2 doesn't inherit stage 1's state variables.

Two behaviours -File provided for free had to be restored: arguments that aren't simple tokens are single-quoted so they survive the re-parse (-warnnotaserror NU1901;NU1902;NU1903, paths containing spaces), while bare tokens such as 1 stay unquoted so they keep their type; and because -Command doesn't propagate a script's exit N, the generated command ends with exit $LASTEXITCODE, pre-seeded to 1 so a failure that prevents the script from running at all isn't reported as success.

Hard-disabling node reuse is worth fixing at the source, since it also defeats MSBuild Server. Filed dotnet/aspnetcore#68251 and dotnet/fsharp#20217; the changes here are a stop-gap.

-nodeReuse gets the same treatment

Also implements dotnet/arcade#17013.

Node reuse was gated the same way, behind an internal MSBUILD_NODEREUSE_ENABLED environment variable, with the added twist that tools.ps1 / tools.sh hard-failed any CI build that had node reuse enabled. -nodeReuse already existed as a parameter, it just couldn't be turned on for a CI build.

It now works the same way as -mt in that an explicitly passed value wins, on CI and locally. Unlike -mt its default is unchanged: !ci, i.e. on for local builds and off on CI. The MSBUILD_NODEREUSE_ENABLED environment variable and the CI hard error are gone, and eng/build.ps1 / build.sh flow an explicit value down as /p:DotNetBuildNodeReuse, which repo-projects/Directory.Build.props turns into -nodeReuse 1|0 for every repo build.

msbuild.ps1 / msbuild.sh gained the -mt switch as well, so the two smaller entry points behave like build.ps1 / build.sh.

Removing the CI hard error is a deliberate relaxation: node reuse stays off on CI by default, but it can now be opted into.

Drive-by: 1/0 is now accepted by the shell scripts

PowerShell's [bool] parameters only bind $true/$false/1/0 — passing the string true or false throws a ParameterBindingArgumentTransformationException. The shell scripts, on the other hand, only understood true/false. That meant a caller driving both (like repo-projects/Directory.Build.props) had to pick a different value per OS.

tools.sh now normalizes 1/0 to true/false for the boolean build arguments — -nodeReuse, -warnAsError and -msbuildMultiThreaded — so 1/0 is the one form that works everywhere. Normalization happens on the variable rather than at the comparison, so values forwarded straight to MSBuild (/nr:$node_reuse, /p:TreatWarningsAsErrors=$warn_as_error) stay valid. Empty still means "not specified".

This is why the per-OS ArcadeTrueBoolBuildArg / ArcadeFalseBoolBuildArg properties could be removed.

Note that this slightly changes existing behaviour: --nodereuse 1 previously meant off.

Validation

  • All modified .ps1 files parse cleanly; all modified .sh files pass bash -n.
  • Sandboxed harnesses with a stubbed build tool captured the real command line produced by tools.sh, eng/common/build.sh and eng/common/build.ps1. -mt is suppressed by default both locally and on CI, and emitted for -mt 1 in either case.
  • Verified the full -mt matrix against the real eng/common/build.ps1 parameter block and the real tools.ps1 / tools.sh resolution lines: unset locally, unset on CI, -mt 1, -mt 0, -ci -mt 1, -ci -mt 0 and the long-form -msbuildMultiThreaded all resolve identically on both platforms.
  • dotnet msbuild -getProperty:CommonArgs on a repo project confirms no -mt by default, -mt 1 with DotNetBuildMT=true, and -mt 0 with DotNetBuildMT=false.
  • -mt resolves to a Boolean parameter with the mt alias on all six PowerShell entry points, and --mt 0 is accepted by the roslyn/fsharp shell scripts (which reject unknown arguments).
  • Verified 1/0, true/false and the unset case all resolve identically through tools.sh, and that --mt 1 propagates as /p:DotNetBuildMT=true.
  • Verified dotnet msbuild -mt is accepted by the MSBuild version in this repo (18.9).
  • The same harnesses confirm the -nodeReuse matrix on both platforms: off by default on CI, on by default locally, -ci -nodeReuse 1 yields /nr:true, and -nodeReuse 1|0 propagates as /p:DotNetBuildNodeReuse. dotnet msbuild -getProperty:CommonArgs confirms -nodeReuse 1|0 is only emitted when DotNetBuildNodeReuse is set.
  • MSBUILD_MT_ENABLED and MSBUILD_NODEREUSE_ENABLED no longer appear anywhere in the repository.
  • msbuild's wrapper was replayed against the real Arcade parameter block with the exact command line that failed in CI: -mt 1 -nodeReuse 1 now bind as booleans, -mt 0 yields False, every /p: and /clp: argument survives (including one containing spaces) along with NU1901;NU1902;NU1903, exit codes propagate through both process hops, and a genuine parameter binding failure exits non-zero.

Note

-mt requires MSBuild 18+. Repos built with -msbuildEngine vs (for example aspnetcore on Windows) fail with MSB1001 on a VS 17.x install if -mt is passed. This isn't hit while the switch defaults to off, but it needs resolving before the default is flipped.

Note

This pull request description was generated with GitHub Copilot.

Promotes MSBuild's experimental multi-threaded mode from an env-var-only
gate (MSBUILD_MT_ENABLED) to a first-class build script switch. The mode is
enabled by default for local builds and is not run on CI unless explicitly
requested via -mt/--mt.

Implements dotnet/arcade#17242.

- eng/common and src/arcade/eng/common: tools.ps1/sh resolve the default
  (!ci) and emit -mt; build.ps1/sh expose the -mt/--mt switch.
- fsharp, roslyn, aspnetcore and nuget-client don't use eng/common/build.*,
  so their own build scripts gained the switch as well.
- repo-projects/Directory.Build.props: replaced the MSBUILD_MT_ENABLED
  environment variable with the new switch, driven by DotNetBuildMT.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
Copilot AI review requested due to automatic review settings August 5, 2026 15:27
@ViktorHofer
ViktorHofer requested review from a team as code owners August 5, 2026 15:27
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service
dotnet-policy-service Bot requested review from a team August 5, 2026 15:28
@ViktorHofer ViktorHofer changed the title Add -mt switch to build scripts Add -mt switch to build scripts and enable by default for local builds Aug 5, 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.

Pull request overview

This PR promotes MSBuild’s experimental multi-threaded mode to a first-class build-script switch (-mt/--mt), defaulting on for local builds and off on CI unless explicitly requested, and updates VMR orchestration to pass the new switch instead of relying on MSBUILD_MT_ENABLED.

Changes:

  • Add -mt/--mt <true|false> option handling to multiple repo build entry points (PowerShell + shell).
  • Update Arcade tools.ps1/tools.sh MSBuild wrappers to emit -mt based on resolved msbuildMultiThreaded/msbuild_multi_threaded instead of MSBUILD_MT_ENABLED.
  • Update repo-projects/Directory.Build.props to pass -mt <value> via common args (supporting opt-in and opt-out).
Show a summary per file
File Description
src/roslyn/eng/build.sh Adds --msbuildMultiThreaded/--mt argument parsing.
src/roslyn/eng/build.ps1 Adds -msbuildMultiThreaded/-mt parameter and CI defaulting behavior.
src/nuget-client/eng/dotnet-build/build.sh Adds --mt parsing and local/CI default resolution for direct dotnet msbuild invocation.
src/nuget-client/eng/dotnet-build/build.ps1 Adds -msbuildMultiThreaded/-mt parameter and CI defaulting behavior.
src/fsharp/eng/build.sh Adds --msbuildMultiThreaded/--mt argument parsing.
src/fsharp/eng/Build.ps1 Adds -msbuildMultiThreaded/-mt parameter and CI defaulting behavior.
src/aspnetcore/eng/build.sh Adds --msbuildMultiThreaded/--mt argument parsing.
src/aspnetcore/eng/build.ps1 Adds -msbuildMultiThreaded/-mt parameter, documentation, and CI defaulting behavior.
src/arcade/eng/common/tools.sh Resolves msbuild_multi_threaded default and emits -mt without MSBUILD_MT_ENABLED.
src/arcade/eng/common/tools.ps1 Resolves $msbuildMultiThreaded default and emits -mt without MSBUILD_MT_ENABLED.
src/arcade/eng/common/build.sh Exposes -msbuildMultiThreaded/-mt switch to drive tools.sh resolution.
src/arcade/eng/common/build.ps1 Exposes -msbuildMultiThreaded/-mt switch and CI behavior for Arcade build entry point.
repo-projects/Directory.Build.props Replaces MSBUILD_MT_ENABLED=1 with passing -mt <value> via common args.
eng/common/tools.sh Resolves msbuild_multi_threaded default and emits -mt without MSBUILD_MT_ENABLED.
eng/common/tools.ps1 Resolves $msbuildMultiThreaded default and emits -mt without MSBUILD_MT_ENABLED.
eng/common/build.sh Exposes -msbuildMultiThreaded/-mt switch to drive tools.sh resolution.
eng/common/build.ps1 Exposes -msbuildMultiThreaded/-mt switch and CI behavior for VMR build entry point.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 17/17 changed files
  • Comments generated: 6

Comment thread src/roslyn/eng/build.ps1 Resolved
Comment thread src/fsharp/eng/build.sh Resolved
Comment thread src/fsharp/eng/Build.ps1 Resolved
Comment thread src/roslyn/eng/build.sh Resolved
Comment thread src/aspnetcore/eng/build.ps1 Resolved
ViktorHofer and others added 6 commits August 6, 2026 09:59
build.sh and eng/build.ps1 are the VMR's own entry points and don't go
through eng/common/build.*, so they need the switch as well.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
PowerShell [bool] parameters reject the strings 'true'/'false' and only
accept $true/$false/1/0, so passing '-mt false' to a repo's build.ps1
failed with a parameter binding error. Use the existing Arcade bool build
arg properties, which already map to 0/1 on Windows and true/false
elsewhere, and add the missing ArcadeTrueBoolBuildArg counterpart.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
Passing -mt / --mt to the VMR root build script now also appends
/p:DotNetBuildMT=<value>, so a single switch controls both the
orchestrator build and every inner repo build.

Also correct the -msbuildMultiThreaded usage text: PowerShell [bool]
parameters reject the strings 'true'/'false', so the accepted values
are '1' and '0'.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
PowerShell's [bool] parameters only bind 1/0, never the strings
'true'/'false', while the shell scripts previously only understood
true/false. That forced every caller to pick a different value per OS.

Normalize 1/0 to true/false in tools.sh for the boolean build args
(-nodeReuse, -warnAsError, -msbuildMultiThreaded), so 1/0 is now the
one form that works everywhere. ArcadeTrueBoolBuildArg and
ArcadeFalseBoolBuildArg therefore no longer need to vary by OS.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
Now that 1/0 works on every platform the ArcadeTrueBoolBuildArg and
ArcadeFalseBoolBuildArg properties add no value, so inline them at their
only use site and remove them.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team August 6, 2026 09:44
ViktorHofer and others added 2 commits August 6, 2026 12:12
Give -nodeReuse the same treatment as -mt: an explicitly passed value now
wins over the CI default, which removes the need for the internal
MSBUILD_NODEREUSE_ENABLED environment variable (dotnet/arcade#17013).

- eng/common/{build,msbuild}.{ps1,sh}: node reuse and MSBuild's
  multi-threaded mode are only forced off on CI when the corresponding
  switch wasn't passed explicitly.
- eng/common/msbuild.{ps1,sh}: add the -mt / --mt switch, matching
  build.{ps1,sh}.
- eng/common/tools.{ps1,sh}: drop the hard error that failed CI builds
  when node reuse was enabled, so it can be opted into explicitly.
- eng/build.ps1, build.sh: accept -nodeReuse and flow it down to the repo
  builds as /p:DotNetBuildNodeReuse.
- repo-projects/Directory.Build.props: replace the MSBUILD_NODEREUSE_ENABLED
  environment variable with a -nodeReuse 1|0 argument driven by
  DotNetBuildNodeReuse, next to the existing -mt argument.

The src/arcade/eng/common copies are kept in sync.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
fsharp, roslyn, aspnetcore and nuget-client don't use eng/common/build.*,
so they need the -nodeReuse switch too now that the VMR flows it down.
fsharp and roslyn rejected it outright ("Invalid argument").

- fsharp, roslyn: add the --nodeReuse / -nodeReuse argument. roslyn keeps
  the !ci default; fsharp keeps node reuse off by default, which is what
  it forced before.
- aspnetcore: keep disabling node reuse (and MSBUILDDISABLENODEREUSE) by
  default, but let an explicitly passed value win. This also fixes the
  shell script setting the unused nodeReuse variable instead of node_reuse.
- nuget-client: an explicit --nodeReuse value now wins over the CI
  default, matching --msbuildMultiThreaded.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
…apper

src/msbuild/eng/build.ps1 re-invokes eng/common/build.ps1 out-of-proc with
`pwsh -File`, which passes every argument as a literal string. Typed Arcade
parameters such as [bool] $nodeReuse and [bool] $msbuildMultiThreaded cannot
bind to a string, so `-mt 1` failed with:

  Cannot process argument transformation on parameter 'msbuildMultiThreaded'.

Switch to `pwsh -Command`, which re-parses the arguments as PowerShell so
`-mt 1` arrives as an integer. This matches what src/runtime/eng/Build.ps1
already does via Invoke-Expression, and what src/msbuild/build.cmd already
does when it calls this script. The build still runs out-of-proc so that
stage 2 does not inherit stage 1's state variables.

Arguments that are not simple tokens are single-quoted so they survive the
re-parse (e.g. -warnnotaserror NU1901;NU1902;NU1903, and paths with spaces),
while bare tokens such as `1` stay unquoted so they keep their type.
`-Command` does not propagate a script's `exit N`, so the generated command
ends with `exit $LASTEXITCODE`, pre-seeded to 1 so that a failure which
prevents the script from running at all is not reported as success.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
@ViktorHofer

ViktorHofer commented Aug 7, 2026 •

Copy link
Copy Markdown
Member Author

The -mt switch was enabled by default for local builds and disabled on CI.
Keep the switch and all of its plumbing, but default it to off everywhere
until MSBuild's multi-threaded mode has proven out. Explicitly passing
-mt/--mt (or /p:DotNetBuildMT) still wins, on CI and locally.

Since the default is now off regardless of -ci, the blocks that forced it
off on CI are redundant and are removed. The equivalent nodeReuse handling
is untouched.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
@ViktorHofer ViktorHofer changed the title Add -mt switch to build scripts and enable by default for local builds Add -mt and -nodereuse switches to build scripts Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants