msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

TargetBuiltReason should have a field for InitialTargets

Open KirillOsenkov opened this issue 2 years ago • 1 comments

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: image

Need to be careful about this check though: https://github.com/dotnet/msbuild/blob/f7f0b1924904ec7f2792c0a7805a4d0e91ac8955/src/Build/BackEnd/Components/RequestBuilder/TargetBuilder.cs#L710

KirillOsenkov avatar Nov 29 '23 02:11 KirillOsenkov

I do notice that the message for the TargetStartedEventArgs includes the (entry point) string

image

So there's some indication already.

KirillOsenkov avatar Nov 29 '23 02:11 KirillOsenkov

@maridematte pls check if this is caused by same problem or unrelated: https://github.com/KirillOsenkov/MSBuildStructuredLog/issues/762

JanKrivanek avatar Mar 06 '24 12:03 JanKrivanek

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.

maridematte avatar Mar 19 '24 14:03 maridematte

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.

KirillOsenkov avatar Mar 19 '24 15:03 KirillOsenkov

Or could we reconstruct the initial targets from the BuildRequestEntry passed to that method? (entry.RequestConfiguration.ProjectInitialTargets)

That should not require any contract change.

JanKrivanek avatar Mar 19 '24 15:03 JanKrivanek

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.

JanKrivanek avatar Mar 19 '24 15:03 JanKrivanek

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.

rainersigwald avatar Mar 19 '24 15:03 rainersigwald

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

KirillOsenkov avatar Mar 19 '24 16:03 KirillOsenkov

Would be interesting to get to the bottom of this. We can use this bug or file a new one.

KirillOsenkov avatar Mar 19 '24 16:03 KirillOsenkov