Skip to content

Rebuild the solution graph when MSBuild initializes the plugin from a build request - #188

Open
djack42 wants to merge 1 commit into
microsoft:mainfrom
djack42:upstream-pr/graph-init-race
Open

djack42 wants to merge 1 commit into
microsoft:mainfrom
djack42:upstream-pr/graph-init-race

Conversation

@djack42

@djack42 djack42 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

In a graph build, ProjectCacheService initializes project cache plugins with the graph in the background (InitializePluginsForGraph is not awaited), while the scheduler already submits the first nodes. When a node's cache query gets there first, MSBuild initializes the plugin from that build request instead. BeginBuildAsync then gets no Graph, one entry point per project of the solution, and that node's targets as RequestedTargets.

The plugin builds its graph from those entry points, which has two consequences:

  • the graph lacks the solution's ProjectDependencies edges;
  • every project gets the requesting node's targets instead of the solution build's.

Weak fingerprints differ from the ones the same build would compute with the real graph, so lookups miss. Outer builds of multi-targeting projects are never added either, since MSBuild reports fewer targets than the plugin expects. We saw it with MSBuild 18.8 on a loaded CI runner, as hit ratios of 16–19 % on builds that otherwise get 100 %.

The fix belongs in MSBuild: dotnet/msbuild#15514 registers the plugins before a graph build submits its nodes. Until builds run on an MSBuild with that fix, this change works around it in the plugin:

  • Detection: no Graph, and entry points that carry IsGraphBuild=true (the global property MSBuild sets on graph build requests).
  • Recovery: rebuild the graph from the SolutionPath global property, with the solution configuration that maps the projects the way the build does, and use the solution build's Build targets.
  • Safety check: keep the rebuilt graph only if one of its nodes gets exactly the requesting node's targets. Otherwise keep the entry point graph, as today.
  • Logging: both outcomes are logged at high importance, so the race shows in the build output.

The workaround can be removed once the supported MSBuild versions include the fix.

Tests: GraphBuildRequestInitializationTests covers the detection, the solution entry point (path and solution configuration), and the target list check, for a graph and a non-graph initialization.

🤖 Generated with Claude Code

… build request

In a graph build, MSBuild's ProjectCacheService initializes the plugin with the graph in the
background (InitializePluginsForGraph is fire-and-forget), while the scheduler already submits
the first nodes. When a node's cache query gets there first, the plugin gets no graph, one entry
point per project of the solution, and that node's targets as the build's targets. Its graph then
lacks the solution's ProjectDependencies edges and puts these targets on every project: weak
fingerprints differ, lookups miss, and outer builds are never added because MSBuild reports fewer
targets than expected. Seen with MSBuild 18.8 on a loaded CI runner, as 16-19 % hit ratios.

Detect it (no graph, entry points carrying IsGraphBuild=true), rebuild the graph from the
SolutionPath global property with the solution configuration that maps the projects as the build
does, and use the "Build" targets. Keep the rebuilt graph only if one of its nodes gets exactly
the requesting node's targets; otherwise keep the entry point graph. Both outcomes are logged at
high importance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant