Confusing and inconsistent behavior around `AfterTargets="Build"` in referenced projects
- Dominant language
- C#
- Stars
- 5.5k
- Forks
- 1.5k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 141
Description
When building multiple projects in parallel that reference the same dependency, AfterTargets targets in the dependency project may not be complete when `ResolveProjectReferences` in depending projects returns, unlike the single-proc behavior. This occurs when the dependency project is built on the scheduler node, but not when it is built on another node.
This is the root cause of https://github.com/dotnet/sdk/issues/48785.
## Expected Behavior
The MSBuild task should wait for all AfterTargets targets to complete execution before returning, in ALL callers, not just the first one, and independent of what nodes the various projects are scheduled on. Projects that depend on `Dependency.proj` should not continue until all AfterTargets targets have fully completed.
## Actual Behavior
MSBuild can return from building a reference as soon as the main target (implicitly `Build`) completes, without waiting for `AfterTargets="Build"` targets to finish. This means that two projects ProjectA and ProjectB that reference the same project may continue executing their build at different times--one after only the `Build` target completes, and the other after the `AfterTargets` targets complete.
## Repro
With these projects (click to expand)
`Entry.proj`:
```xml
```
`initial.proj`:
```xml
```
`ProjectA.proj`:
```xml
```
`ProjectB.proj`:
```xml
```
`Dependency.proj`:
```xml
```
[AfterTargets_Build_Return_Timing.zip](https://github.com/user-attachments/files/20016292/AfterTargets_Build_Return_Timing.zip)
You can see the behavior changes in different conditions:
```sh-session
❯ dotnet msbuild .\Entry.proj -tl:off -p:AfterTargetsProjectIsOnNode1=false
MSBuild version 17.14.0-preview-25175-08+5880e1c75 for .NET
Dependency Build
Dependency AfterTargets start
Dependency AfterTargets completed
ProjectA Build
ProjectB Build
❯ dotnet msbuild .\Entry.proj -tl:off -p:AfterTargetsProjectIsOnNode1=true
MSBuild version 17.14.0-preview-25175-08+5880e1c75 for .NET
Dependency Build
Dependency AfterTargets start
S:\work\AfterTargets_Build_Return_Timing\ProjectB.proj(11,5): warning : Dependency AfterTarget INCOMPLETE
ProjectB Build
Dependency AfterTargets completed
ProjectA Build
❯ dotnet msbuild .\Entry.proj -tl:off -p:AfterTargetsProjectIsOnNode1=true -m:1
MSBuild version 17.14.0-preview-25175-08+5880e1c75 for .NET
Dependency Build
Dependency AfterTargets start
Dependency AfterTargets completed
ProjectA Build
ProjectB Build
❯ $env:MSBUILDNOINPROCNODE=1
❯ dotnet msbuild .\Entry.proj -tl:off -p:AfterTargetsProjectIsOnNode1=true
MSBuild version 17.14.0-preview-25175-08+5880e1c75 for .NET
Dependency Build
Dependency AfterTargets start
Dependency AfterTargets completed
ProjectA Build
ProjectB Build
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.