Description
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:
- If
App\Action\CheckEmail
is made shared (remove "shared: false" in services.yaml), the error dissappears. - If I comment
$logger
inFirstProcessor
constructor https://github.com/unnamed777/project_issue/blob/master/src/Processor/FirstProcessor.php#L10, the error disappears. - If I use
CheckEmail
in constructor of another service (and keep it in constructor ofCheckInboxCommand
), the error disappears. For instance https://github.com/unnamed777/project_issue/blob/master/src/Controller/DevController.php#L11, uncommenting$action
inDevController
resolves problem. - If I remove
CheckEmail
fromCheckInboxCommand
constructor and add it inDevController
constructor, the error disappears.
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".