Skip to content

C#: Disable MSBuild node reuse and the MSBuild server during tracing - #23254

Open
michaelnebel wants to merge 2 commits into
github:mainfrom
michaelnebel:michaelnebel-csharp-extractor-env-vars
Open

michaelnebel wants to merge 2 commits into
github:mainfrom
michaelnebel:michaelnebel-csharp-extractor-env-vars

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

In this PR we set some environment variables that affects dotnet when building code and thus affects traced extraction. This is needed to support traced extraction of .NET 11 as MSBuild server is now enabled by default (see here).

Some context: dotnet might use existing servers or workers when building code (using MSBuild) and we need to prevent this, otherwise traced extraction might fail as the calls might not be intercepted. The different servers and workers:

  • Roslyn build server: This is disabled in tracing by injecting /p:UseSharedCompilation=false. This is the "best" solution for overriding in build commands as it also overrides existing settings.
  • MSBuild server: Can be disabled by setting the environment variable DOTNET_CLI_USE_MSBUILD_SERVER=false. Prior to .NET 11, the default was false but for .NET 11 the default is true. That is, if we want to do traced extraction for .NET 11 we need to disable the MS Build server.
  • MSBuild worker: There may exist some short-lived processes that dotnet might re-use for building code, but we haven't observed any problems with this. We can disable these by setting MSBUILDDISABLENODEREUSE=1 (this should also implicitly disable the MSBuild server).

Preferably, we would like to disable the use of MSBuild Server and MSBuild workers by injecting a flag into the build command (as this would also override build commands that could override the setting of an environment variable). The options are

  • --disable-build-servers: This should handle all cases mentioned above, but it was introduced in .NET 7 and previous versions of .NET fails with MSB1001: Unknown switch, which means that we would break builds for .NET 6 and earlier. So we can't use that unless we narrow traced support to .NET 7 or newer.
  • -nodeReuse:false: This would disable MSBuild Server and workers, but fails for .NET 2 Core and earlier. We could consider removing .NET Core 2 and earlier for the listed of supported .NET variants.

AFAIK, there we can't detect the dotnet version we are tracing, which leaves us with the only option of setting the environment variables for the process that invokes the extractor.

Testing

  • DCA looks good.
  • QA looks good, but need to confirm that the experiment "succeeded". The comparison dashboard has 4 stable conclusion progressions and 2 stable regressions. The stable regressions look like (1) Spurious build failure (2) The autobuilder "detects" different build-scripts?! This is not related to changes in this PR, but we need to investigate whether there is non-determinism in the build script detection.

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.

🟢 Approval recommended

No active review issues are reported for the MSBuild tracing reliability changes.

No open issues 🎉

What changed in this PR

This PR improves C# traced extraction reliability, including for .NET 11, by disabling MSBuild server use and node reuse through extractor environment settings.

Changes:

  • Disable MSBuild server use and node reuse without adding flags incompatible with older .NET versions.
  • Extend integration coverage across dotnet build, msbuild, publish, and pack, checking both environment settings.
  • Add a change note describing the reliability improvement.
File Description
csharp/​ql/​lib/​change-notes/​2026-11-26-msbuild-tracing.md Documents the tracing reliability fix.
csharp/​ql/​integration-tests/​all-platforms/​dotnet_build/​test.py Tests four build commands with the environment-check target.
csharp/​ql/​integration-tests/​all-platforms/​dotnet_build/​dotnet_build.csproj Adds assertions for both environment settings.
csharp/​codeql-extractor.yml Sets environment variables to disable MSBuild server use and node reuse.

Review effort: Balanced (adaptive)

@michaelnebel
michaelnebel marked this pull request as ready for review November 26, 2026 11:11
@michaelnebel
michaelnebel requested a review from a team as a code owner November 26, 2026 11:11
@michaelnebel
michaelnebel requested a review from jketema November 26, 2026 11:11
michaelnebel and others added 2 commits November 26, 2026 14:59
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@michaelnebel
michaelnebel force-pushed the michaelnebel-csharp-extractor-env-vars branch from 8e3dbef to 2b973cd Compare November 26, 2026 14:00

@jketema jketema 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.

Looks sensible.

@michaelnebel

Copy link
Copy Markdown
Contributor Author

Looks sensible.

Great. Will do some more testing before merging.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants