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

Skip to content

[DependencyInjection] Non-existent "inner" service when decorated and autowired #43272

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
BafS opened this issue Oct 1, 2021 · 12 comments · Fixed by #43579
Closed

[DependencyInjection] Non-existent "inner" service when decorated and autowired #43272

BafS opened this issue Oct 1, 2021 · 12 comments · Fixed by #43579

Comments

@BafS
Copy link
Contributor

BafS commented Oct 1, 2021

Symfony version(s) affected: > v5.3.4

Description

There is a breaking change since DependencyInjection was updated (patch version !). It works fine from v5.0.0 to v5.3.4 but newer versions have a breaking change with decorated services. It seems it was introduced #42815 because of the pass order.

How to reproduce

<?php
namespace Test;
require_once 'vendor/autoload.php';

$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder();

class Foo {}
class Bar extends Foo {}
class A {
    public function __construct(Foo $foo) {}
}

$containerBuilder->register('foo', Foo::class);

$containerBuilder->register('bar', Bar::class)->setDecoratedService('foo');
$containerBuilder->setAlias(Foo::class, 'bar.inner');

$containerBuilder->register(A::class)->setAutowired(true);

$containerBuilder->compile();

This code works for versions <= v5.3.4 and is breaking for versions > v5.3.4.

Error: PHP Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "bar.inner". in /.../vendor/symfony/dependency-injection/ContainerBuilder.php:979

Possible Solution

Change the pass order that was changed in #42815 (but if I follow correctly it was made to fix #42791 which was made to fix #42347 because autowire arguments were changed in #40406).

@stof
Copy link
Member

stof commented Oct 1, 2021

Can you provide the stack trace of the exception you get ?

@BafS
Copy link
Contributor Author

BafS commented Oct 1, 2021

Sure!

Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "bar.inner". in /.../vendor/symfony/dependency-injection/ContainerBuilder.php:979
Stack trace:
#0 /.../vendor/symfony/dependency-injection/ContainerBuilder.php(1011): Symfony\Component\DependencyInjection\ContainerBuilder->getDefinition('bar.inner')
#1 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(336): Symfony\Component\DependencyInjection\ContainerBuilder->findDefinition('bar.inner')
#2 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(259): Symfony\Component\DependencyInjection\Compiler\AutowirePass->getAutowiredReference(Object(Symfony\Component\DependencyInjection\TypedReference))
#3 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(290): Symfony\Component\DependencyInjection\Compiler\AutowirePass->Symfony\Component\DependencyInjection\Compiler\{closure}()
#4 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(180): Symfony\Component\DependencyInjection\Compiler\AutowirePass->autowireMethod(Object(ReflectionMethod), Array, true)
#5 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(133): Symfony\Component\DependencyInjection\Compiler\AutowirePass->autowireCalls(Object(ReflectionClass), true, true)
#6 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(78): Symfony\Component\DependencyInjection\Compiler\AutowirePass->doProcessValue(Object(Symfony\Component\DependencyInjection\Definition), true)
#7 /.../vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(81): Symfony\Component\DependencyInjection\Compiler\AutowirePass->processValue(Object(Symfony\Component\DependencyInjection\Definition), true)
#8 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(109): Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, true)
#9 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(78): Symfony\Component\DependencyInjection\Compiler\AutowirePass->doProcessValue(Array, true)
#10 /.../vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(46): Symfony\Component\DependencyInjection\Compiler\AutowirePass->processValue(Array, true)
#11 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(60): Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#12 /.../vendor/symfony/dependency-injection/Compiler/Compiler.php(91): Symfony\Component\DependencyInjection\Compiler\AutowirePass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#13 /.../vendor/symfony/dependency-injection/ContainerBuilder.php(744): Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#14 /.../bug.php(20): Symfony\Component\DependencyInjection\ContainerBuilder->compile()
#15 {main}
  thrown in /.../vendor/symfony/dependency-injection/ContainerBuilder.php on line 979

@xabbuh
Copy link
Member

xabbuh commented Oct 1, 2021

Do you still see the error with the latest patch release?

@BafS
Copy link
Contributor Author

BafS commented Oct 1, 2021

Yes with version v5.3.8 @xabbuh

@BafS
Copy link
Contributor Author

BafS commented Oct 1, 2021

Why was it closed?
If it was not clear, yes, I do have the bug with version 5.3.8 and 5.3.x-dev e39c344 too.

@nicolas-grekas
Copy link
Member

Oops sorry I read too fast as this being fixed in 5.3.8

@nicolas-grekas nicolas-grekas reopened this Oct 1, 2021
@xabbuh
Copy link
Member

xabbuh commented Oct 1, 2021

Can you create a small example application that allows to reproduce your issue?

@BafS
Copy link
Contributor Author

BafS commented Oct 1, 2021

I did a minimal snippet in the description to reproduce the issue.
You simply need to install symfony/dependency-injection to be able to reproduce.

  • composer req "symfony/dependency-injection:^5.3"
  • php bug.php

@Okhoshi
Copy link
Contributor

Okhoshi commented Oct 1, 2021

If I may, I have the impression that all these DI issues @BafS mentioned in the OP are due to the various changes in CompilerPass ordering.

If I sum up:

We end up in a cyclic dependency since #40406 because the attribute support was implemented as part of the AutowirePass (which was surely the right call at the time, but maybe it should have been released with 5.4 :D).
However, since then, #42039 added a simplified way to do autoconfiguration based on Attributes.

Could #40406 be reimplemented using the AttributeAutoconfigurationPass, so that we can restore the CompilerPass ordering that was in place before v5.3.4 ?

@BafS
Copy link
Contributor Author

BafS commented Oct 11, 2021

I agree with @Okhoshi, could we restore the compiler pass order like it was in 5.3.4?
There were multiple patches committed to fix issues related to the change in the order and there is still some issue. We are still stuck in version v5.3.4 because next versions are not compatible anymore.

@nicolas-grekas
Copy link
Member

If we restore the compiler pass pre-#42815, then we should find another fix for #42791 before. Anyone up to digging this?

@Okhoshi
Copy link
Contributor

Okhoshi commented Oct 17, 2021

I would be happy to propose a PR giving a more concrete overview of my suggestion.

Okhoshi added a commit to Okhoshi/symfony that referenced this issue Oct 20, 2021
nicolas-grekas added a commit that referenced this issue Oct 26, 2021
… attributes (Okhoshi)

This PR was merged into the 5.3 branch.

Discussion
----------

[DependencyInjection] Fix autowiring tagged arguments from attributes

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #43272
| License       | MIT
| Doc PR        | no

Reimplement #40406 with `AttributeAutoconfigurationPass` to avoid the BC following the change in `CompilerPass` ordering in `PassConfig`.

Also revert the various fix made in an attempt to recover the BC introduced by #40406.

~Note: `5.4` branch was needed because `AttributeAutoconfigurationPass` is not handling parameters in 5.3~

To-do:
- [x] Add a test to cover the breakage presented in #43272

Commits
-------

4c7566f [DependencyInjection] Fix autowiring tagged arguments from attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants