-
Notifications
You must be signed in to change notification settings - Fork 757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing logging generator not getting removed in web projects #4287
Conversation
...etry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.targets
Outdated
Show resolved
Hide resolved
Saw a failure in CI in the Http.Telemetry tests which I'm almost sure is unrelated, @geeknoid @martintmk did we expect the following test to be flaky?
If it's just a one-off failure I guess it's fine ignoring, but if we see that again or have seen it before we should quarantine the test so we can investigate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the comment I left, LGTM!
Please re-target to release/8.0, if this is necessary for .NET 8. |
/backport to release/8.0-rc1 |
Started backporting to release/8.0-rc1: https://github.com/dotnet/extensions/actions/runs/5906347957 |
@@ -1,2 +1,16 @@ | |||
<Project> | |||
<!-- This package should replace the Microsoft.Extensions.Logging.Abstractions source generator. --> | |||
<Target Name="_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, why did we choose to reuse the target name and didn't create a new one (e.g., _Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzersForSure
)? I wonder if MSBuild handles targets with the same name correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Targets with the same name are overwritten. The last one wins.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, and IIRC in this particular case it was by design and intentional to replace the other target as we wanted that the same property would trigger this target without the need of hooking it up again.
Overwriting targets is pretty common in MSBuild, that is for instance how cross-compiling works when doing the outer-builds (By re-defining the Build, Test, Restore, etc targets for outer builds.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, what guarantees that this target is imported after the Microsoft.Extensions.Logging.Abstractions target when referencing the packages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question, I don't think that is deterministic. I did this a while back so don't recall exactly (and clearly I should've added better docs for it :P) but perhaps the issue then was that there were few cases where this target wouldn't be defined, so for those cases I'm just ensuring it's defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh ohh I think I remember now 😄. The logging generator is part of the aspnetcore targeting pack, so aspnetcore projects wouldn't need reference to the logging.abstractions package, and therefore wouldn't import the buildTransitive targets that disable the package. For these cases, I'm providing the target in our package as well, in order to make sure that the generator is also removed for aspnetcore apps.
We could have of course picked a different target name, but then we would have to add logic to detect if we are NOT an aspnetcore app and getting the reference from the package instead, so that we don't try to run the same operation twice. By just re-defining the target, we have the same mechanism for removing the generator whether your application references the logging.generator package, or the aspnetcore targeting pack, and we are guaranteed to only run once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that seems to be the reason why we picked this approach. If you read into the comments of the issue that this PR resolved, you can see how we realized about this case and picked this design to fix it: #4286 (comment)
Fixes #4286
PR #4238 was meant to automatically remove the platform's logging generator whenever the Microsoft.Extensions.Telemetry package was referenced, which worked just fine for console applications. For web projects though, the issue is that the generator can also be coming from the aspnetcore targeting pack, so we need special logic to be able to remove it from there as well. This PR should address that problem.
Microsoft Reviewers: Open in CodeFlow