Skip to content

test(msbuild): serialize process-wide env mutation across MsBuild fixtures - #5062

Merged
arturcic merged 1 commit into
GitTools:feature/managed-gitfrom
arturcic:feature/managed-git-msbuild-env-race
Jul 14, 2026
Merged

arturcic merged 1 commit into
GitTools:feature/managed-gitfrom
arturcic:feature/managed-git-msbuild-env-race

Conversation

@arturcic

Copy link
Copy Markdown
Member

Description

Fixes an intermittent WriteVersionInfoTask test failure that showed up far more often under the managed Git backend. Investigation (dual-backend) traced it to a pre-existing process-global environment-variable race in the MsBuild test fixtures, not the git backend.

Part of #5031.

Root cause

MsBuildTaskFixture mutates the real process environment (set/reset TF_BUILD etc.) so the task can detect the build agent, and MsBuildExeFixture launches a child process that inherits it. Under [assembly: Parallelizable(ParallelScope.Fixtures)] these race:

  • an Azure task sees TF_BUILD cleared by a concurrent fixture → falls back to LocalBuild, emits no ##vso output (empty log); or
  • a non-CI MSBuild child inherits TF_BUILD=true left set by a concurrent Azure fixture → wrongly emits ##vso.

The git backend computes the correct version in every case — the managed backend only widened the race window (~14×) because its CLI-driven normalization makes each task execution much slower (~2.8s vs libgit2 ~0.2s). The race is latent on libgit2 too.

Fix (test-infra only; product code untouched)

New MsBuildProcessEnvironment helper with a shared lock + Reset()/Apply(). Both fixtures hold the lock across the whole reset→set→execute→reset window (and the exe fixture resets before launching the child), making the environment-sensitive sections mutually exclusive without disabling fixture parallelism.

Verification

  • Before: full managed-mode suite failed within 2–6 parallel runs.
  • After: 14/14 (agent) + 5/5 (independent) clean full-suite managed-mode runs.
  • GitVersion.Git.Managed.Tests 141/141; src + new-cli build 0/0; dotnet format --verify-no-changes clean. No product or libgit2 code changed.

🤖 Generated with Claude Code

@arturcic arturcic added this to the 7.x milestone Jul 14, 2026
@arturcic
arturcic force-pushed the feature/managed-git-msbuild-env-race branch from 2fc613b to 537fe76 Compare July 14, 2026 09:12
…tures

The MsBuild task fixtures manipulate real process-wide environment
variables so the task can detect the current build agent, and the
MsBuild.exe fixture launches a child process that inherits that
environment. Under [assembly: Parallelizable(ParallelScope.Fixtures)]
these fixtures run concurrently, so one fixture setting/resetting a CI
variable such as TF_BUILD races with another's task execution or child
process launch:

- an Azure task could see TF_BUILD cleared by a concurrent fixture and
  fall back to LocalBuild, emitting no ##vso output (empty log); and
- a non-CI MSBuild child could inherit TF_BUILD=true left set by a
  concurrent Azure task and wrongly emit ##vso output.

This surfaced as intermittent WriteVersionInfoTest failures that were
far more frequent under the managed git backend, whose CLI-driven
normalization makes each task execution much slower and therefore widens
the racing window; the git backend itself computes the correct version
in every case.

Introduce a shared MsBuildProcessEnvironment lock/reset helper and hold
it around the whole set-env/execute/reset window in both fixtures,
launching the MSBuild child from a clean, reset environment. This makes
the environment-sensitive sections mutually exclusive without disabling
fixture parallelism.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@arturcic
arturcic force-pushed the feature/managed-git-msbuild-env-race branch from 537fe76 to 20ed7ea Compare July 14, 2026 09:23
@sonarqubecloud

Copy link
Copy Markdown

@arturcic
arturcic merged commit 670b636 into GitTools:feature/managed-git Jul 14, 2026
126 checks passed
@arturcic
arturcic deleted the feature/managed-git-msbuild-env-race branch July 14, 2026 09:52
@mergify

mergify Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Thank you @arturcic for your contribution!

arturcic added a commit that referenced this pull request Jul 16, 2026
…v-race

test(msbuild): serialize process-wide env mutation across MsBuild fixtures
arturcic added a commit that referenced this pull request Jul 16, 2026
…v-race

test(msbuild): serialize process-wide env mutation across MsBuild fixtures
@arturcic arturcic modified the milestones: 7.0.x, 7.0.0 Nov 15, 2026
@arturcic arturcic added the build label Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants