Skip to content

Fix NativeAOT tool build race in NativeAot.Tests - #67320

Merged
wtgodbe merged 1 commit into
dotnet:mainfrom
wtgodbe:wtgodbe/nativeaot-tool-build-race
Jun 19, 2026
Merged

wtgodbe merged 1 commit into
dotnet:mainfrom
wtgodbe:wtgodbe/nativeaot-tool-build-race

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Jun 19, 2026

Copy link
Copy Markdown
Member

Problem

The Build Tests Helix legs intermittently fail with:

System.IO.IOException: The process cannot access the file
'...\artifacts\obj\aspnetcoretools\Release\net11.0\win-x64\native\aspnetcoretools.sourcelink'
because it is being used by another process.
  at ILCompiler.SourceLinkWriter..ctor(...)
ilc exited with code 1

aspnetcoretools (the NativeAOT aggregate tool) is built twice concurrently:

  1. By the solution traversal, with globals { ..., TargetFramework=net11.0 } (no RID).
  2. By Microsoft.AspNetCore.Tools.NativeAot.Tests.csproj's ProjectReference, which passed explicit RuntimeIdentifier/TargetRuntimeIdentifier globals (no TargetFramework).

Because MSBuild caches project results by (project, global properties), the two differing global-property sets are treated as separate builds, so two ilc processes NativeAOT-compile the project at the same time and race on the shared intermediate .sourcelink file. The failure is transient — it only fails when the two IlcCompile windows overlap.

Fix

Build the reference with the same global properties the traversal uses (TargetFramework only, no explicit RID). MSBuild then dedupes the reference onto the traversal's single build instead of spawning a second compile, eliminating the race while keeping the build-ordering dependency that ensures the tool is published before the test gathers its Helix payload.

The published RID is unchanged: it is still resolved by the standalone build (and by the test's NativeAotToolPublishRoot glob) via RuntimeIdentifier → TargetRuntimeIdentifier → NETCoreSdkRuntimeIdentifier, so both the normal repo build and the VMR (cross-RID) build resolve the same RID as before.

The test project references aspnetcoretools with Targets=Build and explicit
RuntimeIdentifier/TargetRuntimeIdentifier global properties. Those globals differ
from the global property set the solution traversal uses to build the same project
(TargetFramework only, no RID), so MSBuild treats them as two distinct builds and
NativeAOT-compiles aspnetcoretools twice concurrently. The two ilc processes race on
the shared intermediate file artifacts/obj/.../native/aspnetcoretools.sourcelink,
intermittently failing with an IOException (file in use) and breaking the Build Tests
Helix legs.

Build the reference with the same TargetFramework-only global properties as the
traversal so MSBuild dedupes onto that single build, eliminating the second compile
and the race while preserving the build-ordering dependency. The published RID is
unchanged: it is still resolved by the standalone build (and the test's
NativeAotToolPublishRoot glob) from RuntimeIdentifier/TargetRuntimeIdentifier/
NETCoreSdkRuntimeIdentifier, so repo and VMR (cross-RID) builds are unaffected.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 19, 2026 15:09

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 addresses intermittent Helix Build Tests failures in NativeAot.Tests caused by two concurrent, differently-keyed MSBuild builds of the aspnetcoretools NativeAOT aggregate tool racing on a shared intermediate .sourcelink file. It aligns the ProjectReference global properties with the solution traversal build so MSBuild can deduplicate the build and avoid running multiple simultaneous NativeAOT compiles.

Changes:

  • Updates the test project’s ProjectReference to build aspnetcoretools using TargetFramework only (removing explicit RuntimeIdentifier/TargetRuntimeIdentifier globals).
  • Expands the inline comment to document the MSBuild global-properties deduping rationale and the race being avoided.

@wtgodbe
wtgodbe requested a review from DamianEdwards June 19, 2026 15:12
@DamianEdwards

Copy link
Copy Markdown
Member

@wtgodbe is this in addition to the changes that were made in the VMR when the aggregated native shims were enabled?

@wtgodbe

wtgodbe commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

is this in addition to the changes that were made in the VMR when the aggregated native shims were enabled?

Can you point me to those changes? I believe it is, anything that was merged into the VMR before yesterday-ish should now be in aspnetcore

@DamianEdwards

Copy link
Copy Markdown
Member

Can you point me to those changes? I believe it is, anything that was merged into the VMR before yesterday-ish should now be in aspnetcore

OK, when we re-landed the aggregate executable shims last week that had a bunch of changes to try and fix these races, based on what was done in dotnet/sdk for a similar issue.

@wtgodbe

wtgodbe commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

OK, when we re-landed the aggregate executable shims last week that had a bunch of changes to try and fix these races, based on what was done in dotnet/sdk for a similar issue.

Yeah, in my experience these races are very annoying to try to pin down and fix. dotnet/msbuild#13599 is related

@wtgodbe
wtgodbe merged commit cb29c66 into dotnet:main Jun 19, 2026
27 checks passed
wtgodbe added a commit that referenced this pull request Jun 20, 2026
…67327)

PR #67320 attempted to fix the intermittent ILC sourcelink file lock
(aspnetcoretools.sourcelink 'being used by another process') by changing
the NativeAot.Tests ProjectReference to pass
Properties="TargetFramework=$(DefaultNetCoreTargetFramework)". That was
insufficient: the solution traversal builds the single-targeted
aspnetcoretools.csproj with NO TargetFramework global property, so passing
TargetFramework=net11.0 still produces a differently-keyed MSBuild project
instance. The two instances both run a NativeAOT compile concurrently and
race on the shared intermediate sourcelink file.

Drop the explicit property and UndefineProperties TargetFramework and
RuntimeIdentifier so this reference resolves to the same project instance
the traversal builds and dedupes onto it.

Co-authored-by: Copilot <[email protected]>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 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