TargetBuiltReason should have a field for InitialTargets
Currently we liberally use TargetBuiltReason.None
https://source.dot.net/#Microsoft.Build.Framework/TargetBuiltReason.cs,e71ba02076cc1555,references
Specifically we use None for InitialTargets, so it's hard to tell why a target was built:
Need to be careful about this check though: https://github.com/dotnet/msbuild/blob/f7f0b1924904ec7f2792c0a7805a4d0e91ac8955/src/Build/BackEnd/Components/RequestBuilder/TargetBuilder.cs#L710
I do notice that the message for the TargetStartedEventArgs includes the (entry point) string
So there's some indication already.
@maridematte pls check if this is caused by same problem or unrelated: https://github.com/KirillOsenkov/MSBuildStructuredLog/issues/762
Investigation results:
We currently assign the TargetBuiltReason when pushing targets to the stack https://github.com/dotnet/msbuild/blob/cd64b7b4a690d809cf14fe2af807a328cce04e54/src/Build/BackEnd/Components/RequestBuilder/TargetBuilder.cs#L162
During this process we define if they're being pushed because of another target and assign either BeforeTargets, DependsOn or AfterTargets. However at that step we only have access to a list of targets to be built, but not which ones are the initial targets or the entry points for the build.
The list of targets to build passed is a mix of initial targets, and the rest of the targets all joined together https://github.com/dotnet/msbuild/blob/cd64b7b4a690d809cf14fe2af807a328cce04e54/src/Build/BackEnd/Shared/BuildRequestConfiguration.cs#L758
To make the change requested here we would have to partially rewrite how targets are ordered to build, or do some pretty hacky stuff on the code and change all the tests to match, and we do not have the capacity for such a big change at the moment. Moving this to backlog for now.
yes this is definitely very low priority. I understand why it would be hard to implement given the current implementation.
Perhaps instead we should just log which targets are initial at the beginning.
Or could we reconstruct the initial targets from the BuildRequestEntry passed to that method? (entry.RequestConfiguration.ProjectInitialTargets)
That should not require any contract change.
Btw. is the AfterTargets BuildReason populated as well?
I'm wondering whether the https://github.com/KirillOsenkov/MSBuildStructuredLog/issues/762 is an MSBuild issue or Viewer issue.
It looks like we populate it https://github.com/dotnet/msbuild/blob/cd64b7b4a690d809cf14fe2af807a328cce04e54/src/Build/BackEnd/Components/RequestBuilder/TargetBuilder.cs#L432
but I wouldn't guarantee it is correct on the MSBuild side.
I took a look and MSBuild is not setting the ParentTarget for AfterTargets for that particular case: https://github.com/KirillOsenkov/MSBuildStructuredLog/issues/762#issuecomment-2007592218
Would be interesting to get to the bottom of this. We can use this bug or file a new one.