Skip to content

Cannot autowire service (...) when using #[Target] attribute in Symfony 6.3 #50541

Closed
@Kocal

Description

@Kocal

Symfony version(s) affected

6.3.0

Description

Hi,

While upgrading our application from Symfony 6.2.x to 6.3, I faced an issue about when using the #[Target] attribute in a Command's constructor.

With the following code:

#[AsCommand(
    name: 'app:website:generate:sitemap',
    description: 'Generate sitemaps for all websites.',
)]
class GenerateSitemapsCommand extends Command
{
    public function __construct(
        #[Target('console')] private LoggerInterface $logger,
        private SitemapGenerator $sitemapGenerator,
        private WebsiteRepository $websiteRepository,
        private Clock $clock,
        private WebsiteSwitcher $websiteSwitcher
    ) {
        parent::__construct();
    }

    // ... 
}

I have the following error with Symfony 6.3 (it works fine with Symdony 6.2):

symfony console c:cl -v

In DefinitionErrorExceptionPass.php line 51:

  [Symfony\Component\DependencyInjection\Exception\RuntimeException]
  Cannot autowire service "App\Command\Website\GenerateSitemapsCommand": "#[Target('console')" on argument "$logger" of method "__construct()"


Exception trace:
  at /Users/halliaume/workspace/international-website/vendor/symfony/dependency-injection/Compiler/DefinitionErrorExceptionPass.php:51
 Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass->processValue() at /Users/halliaume/workspace/international-website/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php:86
 Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue() at /Users/halliaume/workspace/international-website/vendor/symfony/dependency-injection/Compiler/DefinitionErrorExceptionPass.php:29
 Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass->processValue() at /Users/halliaume/workspace/international-website/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php:47
 Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->process() at /Users/halliaume/workspace/international-website/vendor/symfony/dependency-injection/Compiler/Compiler.php:80
 Symfony\Component\DependencyInjection\Compiler\Compiler->compile() at /Users/halliaume/workspace/international-website/vendor/symfony/dependency-injection/ContainerBuilder.php:767
 Symfony\Component\DependencyInjection\ContainerBuilder->compile() at /Users/halliaume/workspace/international-website/vendor/symfony/http-kernel/Kernel.php:506
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at /Users/halliaume/workspace/international-website/vendor/symfony/http-kernel/Kernel.php:757
 Symfony\Component\HttpKernel\Kernel->preBoot() at /Users/halliaume/workspace/international-website/vendor/symfony/http-kernel/Kernel.php:126
 Symfony\Component\HttpKernel\Kernel->boot() at /Users/halliaume/workspace/international-website/vendor/symfony/framework-bundle/Console/Application.php:154
 Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands() at /Users/halliaume/workspace/international-website/vendor/symfony/framework-bundle/Console/Application.php:72
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /Users/halliaume/workspace/international-website/vendor/symfony/console/Application.php:174
 Symfony\Component\Console\Application->run() at /Users/halliaume/workspace/international-website/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /Users/halliaume/workspace/international-website/vendor/autoload_runtime.php:29
 require_once() at /Users/halliaume/workspace/international-website/bin/console:21

Note that the Monolog handler console is properly defined.

Thanks!

How to reproduce

See https://github.com/Kocal/sf6.3-reproducer-autowiring-target-attribute for reproduction repository and steps.

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions