-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle], [DependencyInjection] added logging of unused tags #11744
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
Conversation
10f129b
to
e874ab1
Compare
Why isn't this BC? |
@@ -0,0 +1,48 @@ | |||
<?php | |||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; |
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.
you're missing the license file header, take a look at the other files
e874ab1
to
4c9172f
Compare
Thanks for the feedback, I changed the code according to your suggestions. |
$ignore = $container->getParameter('compiler.validate.ignore_unused_tag.ignore'); | ||
|
||
$tags = $container->findTags(); | ||
$logger = $container->get('logger'); |
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.
you should never get a service in a compiler pass. This is the process of building the container. Using the container in this step can lead to weird bugs.
If you want to log some stuff during the container creation, it should go in the DIC compiling logs
the XSd must be updated as well |
02112bb
to
5e103e2
Compare
DX is all about simplifying the life of the developer. So, I would definitely not make this "feature" configurable. To me, it does not make sense to configure the validation of tags in case people made typos. I do understand the possibility to break BC here but as we only talking about warnings in the logs, it seems ok to me. Instead, I would whitelist all built-in tags, that should cover most use cases. |
f3fd9aa
to
8800778
Compare
), '->findTaggedServiceIds() returns an array of service ids and its tag attributes'); | ||
$this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services'); | ||
} | ||
|
||
public function testfindUnusedTags() |
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.
testFindUnusedTags
@Marmelatze What's the status of this PR? Do you have time to finish it? Do you need some help? |
8800778
to
08e6214
Compare
Sorry I didn't have much time, lately. I integrated all the changes into the PR |
I obviously like this idea, but it does seem odd that we have a hard-coded list of the whitelisted tags. If another bundle/library introduced their own tags, they would get the warning about it being unused. So now we have the positive of helping people catch typos (though I don't know how many people will check the logs) with the negative of giving people a false message in some cases. |
@weaverryan if the bundle introducing the tags is registered, the tags are likely to be used. The false positive is if a bundle contains an integration layer with another bundle not registered in your project, relying on a tag for this. Regarding the case of checking the logs, maybe we should have a DataCollector looking at the container compiler logs and extracting these warnings to display them in the profiler (it would probably require adding |
@stof You're right - this PR is doing more intelligent things than I was giving it credit for. I also like your idea of raising something on the WDT if we have a log message above a certain level. The WDT is getting a bit busy, but this is exactly the type of info I'd want - it's more important at that moment than really anything else :). Btw, I actually do see people misspell tags fairly commonly - Thanks! |
The WDT panel can be added in a separate PR though |
Regarding the WDT, there is no need to add anything as the logger already warns you when you have errors in your code. If we really think the user made a typo, I would log an error, not a warning anyway. |
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Find all service tags which are defined, but not used an yield a warning log message. |
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.
typo: and yield a warning
Forget about my comment, it cannot be an error as we might have false positives here. But adding a WDT panel for that seems wrong to me. It should be part of some other existing panels. |
Besides my small comments about code CS, I'm 👍 as a first step towards the end goal. |
011b4cd
to
6f94e8e
Compare
Made the style changes, should I add the two tags (routing.expression_language_provider, security.expression_language_provider)? |
We are talking about container compiling logs here (most of them are debugging message though, and not useful for users until they want to debug why a service disappeared from their container), not about the logger service |
->addTag('kenrel.event_listener', array('bar' => 'bar')) | ||
; | ||
$builder->findTaggedServiceIds('kernel.event_listener'); | ||
$this->assertEquals($builder->findUnusedTags(), array( |
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.
$this->assertEquals()
takes expected value as 1st argument.
6f94e8e
to
e20c787
Compare
… during container compilation
e20c787
to
415f068
Compare
@Marmelatze Can you some more tests as currently, the pass is not tested at all. Can you also add the missing tags that were added since then as well? Thanks. |
ping |
Oh sorry totally forgot about this, is it still relevant? I have time to look into this in a few weeks. |
@Marmelatze I think it's still relevant |
Closing in favor of #15963 where I've resolved the conflict, added some tests, made some minor tweaks, and fixed some minor bugs |
This PR was merged into the 2.8 branch. Discussion ---------- added logging of unused tags | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11511, #11744 | License | MIT | Doc PR | - This is the same as #11744 but with some minor tweaks and some unit tests for the compiler pass. Commits ------- 95c9f50 added some tests d3271e1 missing tags in whitelist f51fe4a [FrameworkBundle] [DependencyInjection] added logging of unused tags during container compilation
I gave this a short shot and added a new CompilerPass to log all tags, which are not queried by
ContainerBuilder::findTaggedServiceIds()
.Example log:
This is maybe not the best solution, for tags defined in optional bundles. So I added a
ignore
configuration option for this case.Configuration:
A better approach could be a explicit declaration of the tags and their arguments? Maybe in the extension class of the regarding bundle? But what should happen when a 3rd party bundle doesn't add such declaration?