8000 [DI] Confusing error message when an argument can't be autowired · Issue #22848 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Confusing error message when an argument can't be autowired #22848

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
javiereguiluz opened this issue May 22, 2017 · 3 comments
Closed

[DI] Confusing error message when an argument can't be autowired #22848

javiereguiluz opened this issue May 22, 2017 · 3 comments

Comments

@javiereguiluz
Copy link
Member
Q A
Bug report? yes?
Feature request? no
BC Break report? no
Symfony version 3.3

In symfony/demo#562 we're updating the Symfony Demo app to 3.3.0 RC1 and all the new DI features. During the upgrade I suffered lots of errors ... but the error messages were perfect. Examples:

AutowiringFailedException in AutowirePass.php line 271:
Cannot autowire service "AppBundle\EventListener\CommentNotificationSubscriber":
argument "$sender" of method "__construct()" must have a type-hint or be given a
value explicitly.

AutowiringFailedException in AutowirePass.php line 271: Cannot autowire service
"AppBundle\EventListener\RedirectToPreferredLocaleSubscriber": argument
"$locales" of method "__construct()" must have a type-hint or be given a value
explicitly.

However, when I removed this original service config for our Twig extension:

app.twig.app_extension:
    public:    false
    class:     AppBundle\Twig\AppExtension
    arguments: ['@markdown', '%app_locales%']
    tags:
        - { name: twig.extension }

I saw this confusing error:

FatalThrowableError in AppExtension.php line 42:
Type error: Too few arguments to function
AppBundle\Twig\AppExtension::__construct(), 0 passed in /Users/javier/sf
/symfony-demo/var/cache/dev/appDevDebugProjectContainer.php on line 3565 and
exactly 2 expected

This was fixed when I added this:

AppBundle\Twig\AppExtension:
    arguments:
        $locales: '%app_locales%'

Why did I see a different error message in this case? Thanks!

@xabbuh xabbuh added this to the 3.3 milestone May 22, 2017
@weaverryan
Copy link
Member

I've also gotten this exact bad error. I'll check into it!

@javiereguiluz
Copy link
Member Author

Another example of this confusing error message. I'm refactoring the console commands to use DI instead of extending from ContainerAware. I added this:

    public function __construct(EntityManagerInterface $em)
    {
        parent::__construct();

        $this->entityManager = $em;
    }

But somehow PHPStorm didn't import the EntityManagerInterface class automatically, and I saw this error:

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError:
Type error: Too few arguments to function
AppBundle\Command\DeleteUserCommand::__construct(), 0 passed in symfony-
demo/var/cache/dev/appDevDebugProjectContainer.php on line 685 and exactly 1
expected in symfony-demo/src/AppBundle/Command/DeleteUserCommand.php:44

Stack trace:
#0 symfony-demo/var/cache/dev/appDevDebugProjectContainer.php(685): AppBundle\Command\DeleteUserCommand->__construct()
#1 symfony-demo/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php(341): appDevDebugProjectContainer->getConsole_Command_AppbundleCommandDeleteusercommandService()
#2 symfony-demo/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(141): Symfony\Component\DependencyInjection\Container->get('console.command...')
#3 symfony-demo/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Applicatio in symfony-demo/src/AppBundle/Command/DeleteUserCommand.php on line 44

@weaverryan
Copy link
Member

Fix in #22857. This affects any service that's inlined - i.e. any service that is only be referenced by only one other service. For example, the Command class is only referenced by the Command Application, and so autowiring errors for the Command class are swallowed (and you get this bad error instead).

nicolas-grekas added a commit that referenced this issue May 23, 2017
This PR was squashed before being merged into the 3.3 branch (closes #22857).

Discussion
----------

[DI] Fix autowire error for inlined services

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22848
| License       | MIT
| Doc PR        | n/a

The `AutowirePass` defers autowiring exceptions until later so that we don't throw autowiring exceptions for services that are ultimately removed. But, if a service is *inlined*, then it appears to be removed, and so we don't throw the exception. This fixes that.

It's an easy fix - but it's a bit ugly. We're adding a bit more "state" to the passes... simply because there is some information that needs to be shared through the compiler process. There might be a better way of doing this in the future (e.g. storing some metadata on the `Compiler`), but this *does* work well.

Commits
-------

4bcef3d [DI] Fix autowire error for inlined services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0