Skip to content

[DI] Warn when a definition relies on a deprecated class in ContainerBuilder::createService() #16001

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

Merged
merged 1 commit into from
Oct 6, 2015

Conversation

nicolas-grekas
Copy link
Member

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

The new feature is in the DI component and it enlighten a deprecation from Doctrine that we ignored in FrameworkBundle, that is also fixed in this PR.

See https://github.com/symfony/symfony/pull/16001/files?w=1

<service id="annotations.file_cache_reader" class="%annotations.file_cache_reader.class%" public="false">
<deprecated>The "%service_id% service is deprecated since 2.8 and will be removed in 3.0."</deprecated>
Copy link
Member Author

Choose a reason for hiding this comment

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

or should I remove the service right now since it's private?

Copy link
Member

Choose a reason for hiding this comment

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

a private service can still be used by any other service. It is not about restricting access to it, but about giving a hint to the optimizer that this service is private to the container (i.e. the container can give it to other services, but not to the outside through get, and so it can inline it)

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, but this does not answer my question :) I bet that for bc, we should keep the definition even if it's unlikely that anyone reuses this service?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that "even if it is unlikely" is still likely but meh.

My comment was mainly for the fact that you misplaced the closing ". :}

@stof
Copy link
Member

stof commented Sep 29, 2015

this should also be triggered when dumping the container rather than instantiating things from the ContainerBuilder directly.

@nicolas-grekas
Copy link
Member Author

If we trigger these notices when dumping the container, we will autoload all service classes (since we need to load the class definition to get the annotation). Is this an issue at all or not?

@Taluu
Copy link
Contributor

Taluu commented Sep 29, 2015

I'm not quite sure we should trigger these warning if there is a @deprecated tag on a class (specially in the Container). I think when a class is deprecated, it should trigger this warning by itself like it is done currently, so that when the class is loaded, the warning is triggered, rather than forcing the hand of the container builder

@nicolas-grekas
Copy link
Member Author

@Taluu your argument could be used against you own PR (#15491) but in fact knowing which service definition triggered some class deprecation notice is really valuable and is a time saver for devs.
I see the same benefit here: more contextual info lead to faster resolution, contributing to higher Sf 3.0 adoption rate.

@stof
Copy link
Member

stof commented Sep 29, 2015

@nicolas-grekas they should not be triggered at dump-time. They should be triggered at runtime. So IMO, the right solution would be to trigger the deprecation in doctrine itself, as we do in Symfony.
In a fullstack project, you will never get a service from the ContainerBuilder directly (if you do it, it means you are getting it before the container is fully built, which is an unsupported use case). Your code would allow to have the feature only for people using the ContainerBuilder directly at runtime (Behat for instance). I think you catched it in FrameworkBundle only because the FrameworkBundle testsuite also contains such usage of the container (it gets services from it without dumping the container and using the dumped one when testing the DI extension)

#15491 is there to support the case where we want to deprecate using a class as service instead of deprecating the class itself (perfect example: the request service).

@nicolas-grekas
Copy link
Member Author

I spent a bit more time on this: I think that doing the check at runtime is not possible because of the overhead it would add. We could also do the check in setDefinition in the container builder but this would trigger autoloading for all service classes at build time, which would be also a big overhead.

Which means that I don't have any better idea than this PR. Even if it won't catch much cases since as stof said nobody (but tests) uses the builder directly to get services, it still helped find the issue with this deprecated doctrine class. For this reason, I'd propose to merge this PR asis.

@stof btw, the #14307 issue targetted by #15491 is not only about "the case where we want to deprecate using a class as service instead of deprecating the class itself". If you read the issue description, it's also clearly about triggering more useful messages, and I fully support this use case!

@nicolas-grekas nicolas-grekas changed the title [DI] Trigger notice when a definition relies on a deprecated class [DI] Warn when a definition relies on a deprecated class in ContainerBuilder::createService() Sep 30, 2015
@fabpot
Copy link
Member

fabpot commented Oct 6, 2015

Thank you @nicolas-grekas.

@fabpot fabpot merged commit ca69fa3 into symfony:2.8 Oct 6, 2015
fabpot added a commit that referenced this pull request Oct 6, 2015
…ss in ContainerBuilder::createService() (nicolas-grekas)

This PR was merged into the 2.8 branch.

Discussion
----------

[DI] Warn when a definition relies on a deprecated class in ContainerBuilder::createService()

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

The new feature is in the DI component and it enlighten a deprecation from Doctrine that we ignored in FrameworkBundle, that is also fixed in this PR.

See https://github.com/symfony/symfony/pull/16001/files?w=1

Commits
-------

ca69fa3 [DI] Warn when a definition relies on a deprecated class in ContainerBuilder::createService()
@nicolas-grekas nicolas-grekas deleted the di-auto-deprec branch October 7, 2015 06:26
@peshi
Copy link

peshi commented Oct 7, 2015

It seems that this PR is causing trouble.

Issue: #16167

@fabpot fabpot mentioned this pull request Nov 16, 2015
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