Skip to content

Add MSBUILDFORCEMULTITHREADED env var to force multi-threaded mode#13662

Merged
JanProvaznik merged 3 commits into
mainfrom
copilot/create-msbuildforceenv-var
May 6, 2026
Merged

Add MSBUILDFORCEMULTITHREADED env var to force multi-threaded mode#13662
JanProvaznik merged 3 commits into
mainfrom
copilot/create-msbuildforceenv-var

Conversation

Copilot AI commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #13606

Context

There was no environment-variable equivalent of the -multiThreaded / -mt switch, making it awkward to opt in to multi-threaded mode in scenarios where the command line cannot be modified (CI, IDE, wrapper scripts). Other switches (e.g. MSBUILDDISABLENODEREUSE for -nodeReuse) already follow this pattern.

Changes Made

  • src/Framework/Traits.cs — added Traits.ForceMultiThreaded, reading MSBUILDFORCEMULTITHREADED == "1", alongside the existing ForceAllTasksOutOfProcToTaskHost.
  • src/MSBuild/XMake.csIsMultiThreadedEnabled short-circuits to true when Traits.Instance.ForceMultiThreaded is set; otherwise falls back to the existing parameterized switch. The method was promoted from private to internal for direct unit-test coverage (matching the pattern of neighboring ProcessMaxCPUCountSwitch).
  • documentation/wiki/MSBuild-Environment-Variables.md — documented the new variable next to the other MSBUILDFORCE* entries.
  • src/MSBuild.UnitTests/XMake_Tests.cs — added three unit tests for the parsing/trait-resolution logic:
    • env var = "1" → multi-threaded enabled even without -mt.
    • env var unset → not enabled.
    • env var = non-"1" value (e.g. "true") → not enabled (only literal "1" is honored, matching neighboring MSBUILDFORCE* flags).
  • src/MSBuild.UnitTests/MSBuildMultithreaded_Tests.cs — added an end-to-end integration test (MSBuildForceMultiThreadedEnvironmentVariablePropagatesToBuildParameters) that launches a real MSBuild process with MSBUILDFORCEMULTITHREADED=1 and no /mt switch, then runs the existing EnvironmentIsolationTestTask with IsMultithreadedMode='true'. The task observes whether TaskEnvironment is isolated from the global environment, which is only true when BuildParameters.MultiThreaded == true. This proves the env var flows end-to-end: EnvironmentTraits.ForceMultiThreadedIsMultiThreadedEnabled → local multiThreadedparameters.MultiThreaded.
internal static bool IsMultiThreadedEnabled(CommandLineSwitches commandLineSwitches)
{
    if (Traits.Instance.ForceMultiThreaded)
    {
        return true;
    }

    return commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.MultiThreaded);
}

Testing

  • dotnet build src/MSBuild/MSBuild.csproj and src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj — 0 warnings, 0 errors.
  • All 4 new tests pass when run via the Microsoft.Build.CommandLine.UnitTests runner with --filter-method "*MSBuildForceMultiThreaded*" (4 succeeded, 0 failed).
  • The new integration test's build log explicitly shows MultiThreaded Mode - TaskEnvironment is isolated from global environment (PASS), confirming the env var propagates to BuildParameters.MultiThreaded.

Notes

  • Env var is opt-in and only honored when set to exactly "1", matching the convention of neighboring MSBUILDFORCE* flags.
  • Command-line switch behavior is unchanged when the env var is unset.
  • The longer name MSBUILDFORCEMULTITHREADED was chosen over the initial MSBUILDFORCEMT for clarity, per reviewer feedback.

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

Adds an environment-variable escape hatch to force MSBuild’s multi-threaded (in-proc node) mode, mirroring existing patterns where env vars can drive CLI behavior when command lines can’t be modified.

Changes:

  • Introduces Traits.ForceMultiThreaded backed by MSBUILDFORCEMT=1.
  • Updates XMake.IsMultiThreadedEnabled to short-circuit to multi-threaded mode when the trait is set.
  • Documents the new environment variable in the environment-variable reference.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/MSBuild/XMake.cs Honors Traits.Instance.ForceMultiThreaded to force-enable multi-threaded mode.
src/Framework/Traits.cs Adds a new trait that reads MSBUILDFORCEMT from the environment.
documentation/wiki/MSBuild-Environment-Variables.md Documents MSBUILDFORCEMT=1 alongside other MSBUILDFORCE* variables.

Comment thread documentation/wiki/MSBuild-Environment-Variables.md
Comment thread src/MSBuild/XMake.cs

@AR-May AR-May left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider adding some tests for the parameters parsing logic.

Comment thread src/Framework/Traits.cs Outdated
Agent-Logs-Url: https://github.com/dotnet/msbuild/sessions/f193f24e-7408-43e0-85cd-d540744399e1

Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>

Copilot AI commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

Consider adding some tests for the parameters parsing logic.

Added three unit tests for the parsing/trait-resolution logic in IsMultiThreadedEnabled covering env var = "1", unset, and non-"1" values. (a92ec5c)

Copilot AI changed the title Add MSBUILDFORCEMT env var to force multi-threaded mode Add MSBUILDFORCEMULTITHREADED env var to force multi-threaded mode May 4, 2026
Copilot AI requested a review from JanProvaznik May 4, 2026 17:16
…o BuildParameters

Agent-Logs-Url: https://github.com/dotnet/msbuild/sessions/d2a55112-0f8e-493e-bfc4-8499bb384f7a

Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
@JanProvaznik JanProvaznik requested a review from AR-May May 4, 2026 17:28
@JanProvaznik JanProvaznik merged commit 0411cab into main May 6, 2026
10 checks passed
@JanProvaznik JanProvaznik deleted the copilot/create-msbuildforceenv-var branch May 6, 2026 08:38
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.

Allow enabling /mt mode via environment variable

4 participants