Skip to content

[DI] falsely removes a service in some conditions #58087

Closed as duplicate of#54294
Closed as duplicate of#54294
@unnamed777

Description

@unnamed777

Symfony version(s) affected

6.4.10

Description

The problem in short: DI removes doctrine repository, which is used in non-shared services, and throws an exception "You have requested a non-existent service "App\Repository\ChannelRepository"" under certain circumstances.

I stumbled upon this issue on working project, got rid of everything unrelated and tried to minimize the code. So, I have a Console command, which uses CheckEmail service:

#[AsCommand('app:check-inbox', 'Check inbox and process emails')]
class CheckInboxCommand extends Command
{
    protected CheckEmail $action;

    public function __construct(
        CheckEmail $command
    )
    {
        parent::__construct();
    }
}

CheckEmail is a public non-shared service, which depends on two another services (processors):

<?php
namespace App\Action;

use App\Processor;

class CheckEmail
{
    public function __construct(
        \App\Processor\FirstProcessor $firstProcessor,
        \App\Processor\SecondProcessor $secondProcessor,
    ) {
    }
}

And two similar processors, which uses service ChannelRepository:

<?php
namespace App\Processor;

use App\Repository\ChannelRepository;
use Psr\Log\LoggerInterface;

class FirstProcessor
{
    public function __construct(
        LoggerInterface $logger,
        ChannelRepository $channelRepository,
    ) {
    }

}
<?php
namespace App\Processor;

use App\Repository\ChannelRepository;
use Psr\Log\LoggerInterface;

class SecondProcessor
{
    public function __construct(
        //LoggerInterface $logger,
        ChannelRepository $channelRepository,
    ) {
    }
}

If I try to build container (clear cache or open / in browser), I get error "You have requested a non-existent service "App\Repository\ChannelRepository"". But there are some changes (any of them), when everything starts working:

How to reproduce

PHP 8.1 is required

  • Clone https://github.com/unnamed777/project_issue
  • symfony composer install. You'll get the the same error, but it's ok for now.
  • symfony serve
  • Open link provided by Symfony CLI. The error is appeared.
  • Open src/Processor/FirstProcesssor.php and comment line with $logger in constructor. Refresh page, the error is gone.
  • Undo previous change. Open src/Controller/DevController.php and uncommend line 11 (CheckEmail $action). The error is gone.
  • Try other actions from description part.

Possible Solution

No response

Additional Context

Comparison of a log of Symfony\Component\DependencyInjection\Compiler shows the difference only in one line (it exists when the problem is here):

Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass: Inlined service "App\Repository\ChannelRepository" to "App\Processor\FirstProcessor".

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