From 98217a46b958ee6eaed3c27aa295a27e0502d9fa Mon Sep 17 00:00:00 2001 From: Jan Provaznik Date: Fri, 10 Jul 2026 14:04:25 +0200 Subject: [PATCH] Enable MSBuild server by default in MSBuildForwardingApp Change DOTNET_CLI_USE_MSBUILD_SERVER to default to true so the MSBuild server is enabled when the user hasn't specified anything about it. An explicit MSBUILDUSESERVER value (or DOTNET_CLI_USE_MSBUILD_SERVER=false) is still respected as an opt-out. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 499249b7-514f-4ecf-88bc-2466455069c5 --- .../MSBuildForwardingAppWithoutLogging.cs | 15 ++++++++------- .../MSBuild/GivenMsbuildForwardingApp.cs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs index b4ee35b270cd..cc9a47b08884 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs @@ -17,12 +17,12 @@ internal sealed class MSBuildForwardingAppWithoutLogging private static readonly bool AlwaysExecuteMSBuildOutOfProc = Env.GetEnvironmentVariableAsBool("DOTNET_CLI_RUN_MSBUILD_OUTOFPROC"); /// - /// An override flag that determines whether to use the MSBuild server - a persistent central node that can serve + /// A flag that determines whether to use the MSBuild server - a persistent central node that can serve /// as a place to cache data and prevent re-doing CoreCLR startup/JITting for small builds. - /// By default, the MSBuild server is disabled due to stability/correctness concerns with some 1P tasks that keep static state around, - /// but it can be used by users that are confident they will not encounter those issues. + /// By default, the MSBuild server is enabled, but users that hit stability/correctness concerns with some + /// 1P tasks that keep static state around can opt out by setting this to false. /// - private static readonly bool UseMSBuildServer = Env.GetEnvironmentVariableAsBool("DOTNET_CLI_USE_MSBUILD_SERVER", false); + private static readonly bool UseMSBuildServer = Env.GetEnvironmentVariableAsBool("DOTNET_CLI_USE_MSBUILD_SERVER", true); /// /// What the SDK's opinion is on the default terminal logger. The SDK defaults to 'auto' which will use the terminal logger if the output is going to a terminal, otherwise it will use the console logger. @@ -98,9 +98,10 @@ public MSBuildForwardingAppWithoutLogging(MSBuildArgs msbuildArgs, string? msbui MSBuildPath = msbuildPath ?? defaultMSBuildPath; - // Only force MSBUILDUSESERVER on when DOTNET_CLI_USE_MSBUILD_SERVER opts in; otherwise leave - // any user-provided MSBUILDUSESERVER value untouched so it can toggle the server on its own. - if (UseMSBuildServer) + // The MSBuild server is enabled by default. Force MSBUILDUSESERVER on unless the user has opted out + // via DOTNET_CLI_USE_MSBUILD_SERVER, or has already set MSBUILDUSESERVER themselves - in which case we + // leave their value untouched so it can toggle the server on its own. + if (UseMSBuildServer && Env.GetEnvironmentVariable("MSBUILDUSESERVER") is null) { EnvironmentVariable("MSBUILDUSESERVER", "1"); } diff --git a/test/dotnet.Tests/CommandTests/MSBuild/GivenMsbuildForwardingApp.cs b/test/dotnet.Tests/CommandTests/MSBuild/GivenMsbuildForwardingApp.cs index e5f16fab1391..309f1e6a40a3 100644 --- a/test/dotnet.Tests/CommandTests/MSBuild/GivenMsbuildForwardingApp.cs +++ b/test/dotnet.Tests/CommandTests/MSBuild/GivenMsbuildForwardingApp.cs @@ -95,5 +95,21 @@ public void ItDoesNotSetCurrentWorkingDirectory() var startInfo = new MSBuildForwardingApp(new string[0], msbuildPath) .GetProcessStartInfo().WorkingDirectory.Should().Be(""); } + + [TestMethod] + public void ItEnablesMSBuildServerByDefault() + { + // The SDK enables the MSBuild server by default. Only assert this when the ambient environment + // hasn't already expressed an opinion via MSBUILDUSESERVER or DOTNET_CLI_USE_MSBUILD_SERVER. + if (Environment.GetEnvironmentVariable("MSBUILDUSESERVER") != null || + Environment.GetEnvironmentVariable("DOTNET_CLI_USE_MSBUILD_SERVER") != null) + { + return; + } + + var msbuildPath = ""; + var startInfo = new MSBuildForwardingApp(new string[0], msbuildPath).GetProcessStartInfo(); + startInfo.Environment["MSBUILDUSESERVER"].Should().Be("1"); + } } }