Skip to content

[FrameworkBundle] Fail properly on unregistrable command #48547

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
use function max;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand Down Expand Up @@ -68,13 +69,16 @@ public function doRun(InputInterface $input, OutputInterface $output): int
{
$this->registerCommands();

$statusCode = Command::SUCCESS;
if ($this->registrationErrors) {
$statusCode = Command::FAILURE;

$this->renderRegistrationErrors($input, $output);
}

$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));

return parent::doRun($input, $output);
return max($statusCode, parent::doRun($input, $output));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not change the status code of all existing commands.
Instead, I propose that we change the status code of the "list" command only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, but why? Is there any reason any command should succeed when there are registration errors?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question the other way around, I'm against breaking the whole console when only one command is down.

}

protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int
Expand All @@ -83,7 +87,9 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
$renderRegistrationErrors = true;

if (!$command instanceof ListCommand) {
$statusCode = Command::SUCCESS;
if ($this->registrationErrors) {
$statusCode = Command::FAILURE;
$this->renderRegistrationErrors($input, $output);
$this->registrationErrors = [];
$renderRegistrationErrors = false;
Expand Down Expand Up @@ -120,7 +126,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
}

try {
$returnCode = parent::doRunCommand($command, $input, $output);
$returnCode = max($statusCode ?? 0, parent::doRunCommand($command, $input, $output));
} finally {
$requestStack?->pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName(
$this->assertSame($newCommand, $application->get('example'));
}

public function testRunOnlyWarnsOnUnregistrableCommand()
public function testUnregistrableCommandsAreConsideredFailure()
{
$container = new ContainerBuilder();
$container->register('event_dispatcher', EventDispatcher::class);
Expand All @@ -148,7 +148,7 @@ public function testRunOnlyWarnsOnUnregistrableCommand()
$tester->run(['command' => 'fine']);
$output = $tester->getDisplay();

$tester->assertCommandIsSuccessful();
$this->assertSame(1, $tester->getStatusCode());
$this->assertStringContainsString('Some commands could not be registered:', $output);
$this->assertStringContainsString('throwing', $output);
$this->assertStringContainsString('fine', $output);
Expand Down Expand Up @@ -181,7 +181,7 @@ public function testRegistrationErrorsAreDisplayedOnCommandNotFound()
$this->assertStringContainsString('Command "fine" is not defined.', $output);
}

public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd()
public function testRunFailsOnUnregistrableCommandAtTheEnd()
{
$container = new ContainerBuilder();
$container->register('event_dispatcher', EventDispatcher::class);
Expand All @@ -205,7 +205,7 @@ public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd()
$tester = new ApplicationTester($application);
$tester->run(['command' => 'list']);

$tester->assertCommandIsSuccessful();
$this->assertSame(1, $tester->getStatusCode());
$display = explode('List commands', $tester->getDisplay());

$this->assertStringContainsString(trim('[WARNING] Some commands could not be registered:'), trim($display[1]));
Expand Down
Loading