Skip to content

Fix intermittent circular dependency in sharedfx.targets (MSB4006) - #6697

Merged
wtgodbe merged 3 commits into
dotnet:mainfrom
wtgodbe:wtgodbe/fix-sharedfx-circular-dep
May 19, 2026
Merged

wtgodbe merged 3 commits into
dotnet:mainfrom
wtgodbe:wtgodbe/fix-sharedfx-circular-dep

Conversation

@wtgodbe

@wtgodbe wtgodbe commented May 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes an intermittent MSB4006: There is a circular dependency in the target dependency graph build failure in shared framework projects, and fixes a latent platform manifest bug exposed by the fix.

Problem 1: Circular dependency (MSB4006)

sharedfx.targets contains self-referencing <MSBuild Projects="$(MSBuildProjectFullPath)"> calls in _AddFilesToNuGetPackage and GetFilesToPublish. When these nested calls produce identical global properties to the parent build, the MSBuild scheduler treats them as the same build and detects a false circular dependency.

_AddFilesToNuGetPackage (line 540) uses RemoveProperties="NoBuild", but when NoBuild isn't set in the outer build, removing it is a no-op — the nested call has identical global properties to the parent.

Fix

Add differentiating dummy properties (_GettingFilesForPackage=true, _GettingFilesForPublish=true) to the nested MSBuild calls so they always have distinct global property sets from the parent build.

This is the same pattern used in the workaround at dotnet/aspnetcore#65667 (which added _PublishingToLayout=true to the caller side for GetFilesToPublish). This PR fixes both targets at the source in arcade and removes the aspnetcore workaround.

Evidence

Problem 2: Missing platform manifest entry (exposed by fix)

With the circular dependency fix, the nested MSBuild call in _AddFilesToNuGetPackage now correctly runs as a distinct build instead of reusing cached results from the parent. This causes _ValidateAllFilesInTemplatedPlatformManifest to evaluate freshly and catch a pre-existing bug: Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib (the composite R2R Mach-O image produced by Microsoft.NETCore.App.Runtime.CoreCLR.sfxproj on Apple mobile platforms) was missing from the PlatformManifestFileEntry list.

The manifest already had Microsoft.NETCore.App.r2r.dylib (from the Mono sfxproj), but not the CoreCLR variant. This bug was latent — previously hidden because MSBuild reused the parent build's already-evaluated targets, skipping fresh validation.

Fix

Add Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib to the PlatformManifestFileEntry list in src/runtime/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props.

Changes

Commit Component Change
1 src/arcade Add _GettingFilesForPackage and _GettingFilesForPublish differentiating properties to nested MSBuild calls in sharedfx.targets
2 src/aspnetcore Remove _PublishingToLayout workaround from sfxproj (no longer needed)
3 src/runtime Add missing Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib platform manifest entry

Add differentiating global properties to self-referencing MSBuild calls
in _AddFilesToNuGetPackage and GetFilesToPublish targets. Without these,
when the MSBuild scheduler builds the same sfxproj concurrently with
identical global properties, it detects a false circular dependency
involving _AddFilesToNuGetPackage or GetFilesToPublish.

The _AddFilesToNuGetPackage target was observed causing MSB4006 in
dotnet/aspnetcore CI (build 1426800). A previous workaround in
aspnetcore (PR dotnet/aspnetcore#65667) addressed GetFilesToPublish
from the caller side; this fix addresses both targets at the source.

See dotnet/msbuild#13599

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings May 19, 2026 17:39
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team May 19, 2026 17:39
The _PublishingToLayout workaround (dotnet/aspnetcore#65667) is no longer
needed now that sharedfx.targets adds differentiating properties to its
self-referencing MSBuild calls directly.

Co-authored-by: Copilot <[email protected]>

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

Fixes an intermittent MSBuild scheduler false-positive circular dependency (MSB4006) triggered by self-referencing MSBuild Projects="$(MSBuildProjectFullPath)" calls in the SharedFramework SDK’s targets, and removes an aspnetcore caller-side workaround now made unnecessary by the SDK-side fix.

Changes:

  • Add dummy global properties to the nested self-builds in sharedfx.targets so they always differ from the parent build configuration.
  • Remove _PublishingToLayout=true from the aspnetcore sharedfx publish-to-layout nested call (the prior workaround for GetFilesToPublish).

Reviewed changes

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

File Description
src/aspnetcore/src/Framework/App.Runtime/src/Microsoft.AspNetCore.App.Runtime.sfxproj Removes the _PublishingToLayout workaround property from a nested publish call now that the SDK target itself differentiates configurations.
src/arcade/src/Microsoft.DotNet.SharedFramework.Sdk/targets/sharedfx.targets Adds _GettingFilesForPackage / _GettingFilesForPublish properties to nested self-MSBuild calls to prevent intermittent MSB4006 circular dependency detection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wtgodbe
wtgodbe enabled auto-merge (squash) May 19, 2026 18:19
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team May 19, 2026 18:19
@ViktorHofer

Copy link
Copy Markdown
Member

cc @jkoritzinsky

@wtgodbe

wtgodbe commented May 19, 2026

Copy link
Copy Markdown
Member Author

Needs a fix:

Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib. Add these file names with extensions to the 'PlatformManifestFileEntry' item group for the runtime pack and corresponding ref pack to include them in the platform manifest.
/Users/runner/work/1/s/artifacts/source-built-sdks/Microsoft.DotNet.SharedFramework.Sdk/targets/sharedfx.targets(303,5): error : The following files are missing entries in the templated manifest: [/Users/runner/work/1/s/src/runtime/src/installer/pkg/sfx/Microsoft.NETCore.App/Microsoft.NETCore.App.Runtime.CoreCLR.sfxproj]
/Users/runner/work/1/s/artifacts/source-built-sdks/Microsoft.DotNet.SharedFramework.Sdk/targets/sharedfx.targets(303,5): error : Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib. Add these file names with extensions to the 'PlatformManifestFileEntry' item group for the runtime pack and corresponding ref pack to include them in the platform manifest. [/Users/runner/work/1/s/src/runtime/src/installer/pkg/sfx/Microsoft.NETCore.App/Microsoft.NETCore.App.Runtime.CoreCLR.sfxproj]

Looking

The Microsoft.NETCore.App.Runtime.CoreCLR.sfxproj produces a composite
R2R Mach-O image named Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib
on Apple mobile platforms, but the platform manifest only listed
Microsoft.NETCore.App.r2r.dylib (from Microsoft.NETCore.App.Mono.sfxproj).

This was a latent bug previously hidden because _AddFilesToNuGetPackage's
nested MSBuild call had identical global properties to the parent build,
causing MSBuild to reuse cached results and skip fresh validation. The
sharedfx.targets fix in this PR makes the nested call use distinct global
properties, which correctly triggers _ValidateAllFilesInTemplatedPlatformManifest
and surfaces this missing manifest entry.

Co-authored-by: Copilot <[email protected]>
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team May 19, 2026 21:11
@wtgodbe

wtgodbe commented May 19, 2026

Copy link
Copy Markdown
Member Author

Pushed 7080fe5 - CC @radekdoulik, @hoyosjs - see the "Problem 2" section in the PR description

@wtgodbe
wtgodbe merged commit 4683e8c into dotnet:main May 19, 2026
11 checks passed
@omajid

omajid commented May 21, 2026

Copy link
Copy Markdown
Member

I think this PR has introduced a new problem.

When building the VMR, I get:


  dotnet/artifacts/source-built-sdks/Microsoft.DotNet.SharedFramework.Sdk/targets/sharedfx.targets(198,5): error : The process cannot access the file 'dotnet/src/runtime/artifacts/obj/coreclr-pack/Release/net11.0/fedora.43-x64/Microsoft.NETCore.App.deps.json' because it is being used by another process. [dotnet/src/runtime/src/installer/pkg/sfx/Microsoft.NETCore.App/Microsoft.NETCore.App.Runtime.CoreCLR.sfxproj]
  dotnet/src/runtime/src/installer/pkg/sfx/bundle/Microsoft.NETCore.App.Bundle.bundleproj(44,5): error MSB4181: The "MSBuild" task returned false but did not log an error.

Claude says:


Before the PR

Both nested calls had identical global properties to their parent. MSBuild's scheduler sees two build requests for the same project with the same global properties and treats them as the same build instance — it deduplicates them, serializes them, or serves a cached result. Only one instance of _GenerateSharedFrameworkDepsFile ever runs. No file contention.

The downside: the deduplication/caching interaction occasionally triggered MSB4006 (false-positive "circular dependency" from MSBuild's scheduler when two nodes raced to claim the same project). This was the bug the PR was fixing. It was intermittent.

After the PR

The two nested calls now have different global properties. MSBuild's scheduler treats them as entirely separate project instances — they get separate cache entries, and can be scheduled on separate worker nodes concurrently. Both execute _GenerateSharedFrameworkDepsFile → File.Create(depsFilePath) with FileShare.None on the same path → "file in use by another process".

The MSB4006 is gone. In its place is a file-lock crash. This failure is more deterministic — it happens whenever the scheduler places the two instances on concurrent nodes, which at -maxcpucount (the default) on a multi-core machine happens reliably.

pull Bot pushed a commit to TheDegenerateDev5150/dotnet-master that referenced this pull request May 22, 2026
Reverts the _GettingFilesForPackage/_GettingFilesForPublish property
additions in sharedfx.targets and restores _PublishingToLayout in
aspnetcore sfxproj. These changes caused MSBuild to schedule the nested
calls as separate project instances, leading to concurrent writes to
the same deps.json file — producing deterministic file-lock failures
on all main rolling builds (6/6 failed in 24h).

Keeps the PlatformManifestFileEntry addition for
Microsoft.NETCore.App.Runtime.CoreCLR.r2r.dylib (real bug fix).

The original MSB4006 intermittent circular dependency is tracked at
dotnet/msbuild#13599 and needs a fix that
doesn't create concurrent project instances.

Partial revert of dotnet#6697.

Co-authored-by: Copilot <[email protected]>
lewing added a commit that referenced this pull request May 22, 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.

6 participants