Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

Marmelatze
Copy link

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #11511
License MIT
Doc PR -

I gave this a short shot and added a new CompilerPass to log all tags, which are not queried by ContainerBuilder::findTaggedServiceIds().
Example log:

[2014-08-23 15:45:32] app.WARNING: Tag "kenrel.event_listener" was defined on the service(s) foo.bar_service, but was never used. Did you mean "kernel.event_listener"

This is maybe not the best solution, for tags defined in optional bundles. So I added a ignore configuration option for this case.

Configuration:

framework:
    compiler:
        validate:
            tags:
                enabled: true
                ignore:
                    - foo.bar

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?

@wouterj
Copy link
Member

wouterj commented Aug 23, 2014

Why isn't this BC?

@@ -0,0 +1,48 @@
<?php
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
Copy link
Member

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

@Marmelatze
Copy link
Author

Thanks for the feedback, I changed the code according to your suggestions.
This should be BC, as the configuration is optional, isn't it?

$ignore = $container->getParameter('compiler.validate.ignore_unused_tag.ignore');

$tags = $container->findTags();
$logger = $container->get('logger');
Copy link
Member

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

@stof
Copy link
Member

stof commented Aug 25, 2014

the XSd must be updated as well

@Marmelatze Marmelatze force-pushed the unused_tags branch 2 times, most recently from 02112bb to 5e103e2 Compare August 26, 2014 07:32
@fabpot
Copy link
Member

fabpot commented Aug 28, 2014

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.

@Marmelatze Marmelatze force-pushed the unused_tags branch 3 times, most recently from f3fd9aa to 8800778 Compare September 5, 2014 08:12
), '->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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testFindUnusedTags

@fabpot
Copy link
Member

fabpot commented Sep 15, 2014

@Marmelatze What's the status of this PR? Do you have time to finish it? Do you need some help?

@Marmelatze
Copy link
Author

Sorry I didn't have much time, lately. I integrated all the changes into the PR

@weaverryan
Copy link
Member

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.

@stof
Copy link
Member

stof commented Sep 25, 2014

@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 WARNING in the message to allow identifying warning messages easily without limiting them to this particular compiler pass)

@weaverryan
Copy link
Member

@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 - kernal.event_listener :). But I'm not sure this will help them unless we try to get the message in front of their face.

Thanks!

@stof
Copy link
Member

stof commented Sep 25, 2014

The WDT panel can be added in a separate PR though

@fabpot
Copy link
Member

fabpot commented Sep 26, 2014

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.
Copy link
Member

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

@fabpot
Copy link
Member

fabpot commented Sep 26, 2014

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.

@fabpot
Copy link
Member

fabpot commented Sep 26, 2014

Besides my small comments about code CS, I'm 👍 as a first step towards the end goal.

@Marmelatze Marmelatze force-pushed the unused_tags branch 2 times, most recently from 011b4cd to 6f94e8e Compare September 26, 2014 08:09
@Marmelatze
Copy link
Author

Made the style changes, should I add the two tags (routing.expression_language_provider, security.expression_language_provider)?
The fabbot and travis failures seem to be unrelated?

@stof
Copy link
Member

stof commented Sep 26, 2014

there is no need to add anything as the logger already warns you when you have errors in your code

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(
Copy link
Contributor

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.

@fabpot
Copy link
Member

fabpot commented Feb 5, 2015

@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.

@stof
Copy link
Member

stof commented Aug 1, 2015

ping

@Marmelatze
Copy link
Author

Oh sorry totally forgot about this, is it still relevant? I have time to look into this in a few weeks.

@fabpot
Copy link
Member

fabpot commented Aug 3, 2015

@Marmelatze I think it's still relevant

@fabpot
Copy link
Member

fabpot commented Sep 28, 2015

Closing in favor of #15963 where I've resolved the conflict, added some tests, made some minor tweaks, and fixed some minor bugs

@fabpot fabpot closed this Sep 28, 2015
fabpot added a commit that referenced this pull request Sep 28, 2015
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants