Skip to content
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

[Console][FrameworkBundle] --profile option might not be set on every command #52433

Closed
keulinho opened this issue Nov 3, 2023 · 2 comments · Fixed by #52434
Closed

[Console][FrameworkBundle] --profile option might not be set on every command #52433

keulinho opened this issue Nov 3, 2023 · 2 comments · Fixed by #52434

Comments

@keulinho
Copy link
Contributor

keulinho commented Nov 3, 2023

Symfony version(s) affected

6.4

Description

With the new command profiling feature, a event listener was added that grabs the profile option from the command inputs:
https://github.com/symfony/symfony/blob/6.4/src/Symfony/Bundle/FrameworkBundle/EventListener/ConsoleProfilerListener.php#L62

Normaly that option is added to every command over the Application: https://github.com/symfony/symfony/blob/6.4/src/Symfony/Bundle/FrameworkBundle/Console/Application.php#L47

However not every command is executed over the Application class, e.g. during unit tests with the CommandTester or by just dispatching the ConsoleCommandEvent directly.

In those cases the following error is thrown:

Symfony\Component\Console\Exception\InvalidArgumentException: The "profile" option does not exist.

/.../symfony/console/Input/Input.php:127
/.../symfony/framework-bundle/EventListener/ConsoleProfilerListener.php:62
/.../symfony/event-dispatcher/Debug/WrappedListener.php:116
/.../symfony/event-dispatcher/EventDispatcher.php:206
/.../symfony/event-dispatcher/EventDispatcher.php:56

How to reproduce

Use the command tester to test a manually created command:

$command = new MyCommandToTest();
$commandTester = new CommandTester($command);

$commandTester->run($input);

Note: this differs from how the command tester is documented: https://symfony.com/doc/current/console.html#testing-commands
As there the command is fetched from the application, however, to properly unit test commands without having the whole application booted up, I think the above code is valid for testing commands and should be supported.

Possible Solution

Check if the option exists before accessing it -> PR incoming

Additional Context

No response

@GromNaN
Copy link
Member

GromNaN commented Nov 3, 2023

Hello, I tried to reproduce the error, but the Symfony\Component\Console\Tester\CommandTester class doesn't have a run method and it doesn't instantiate the ConsoleProfilerListener.
Could you provide more information on how to reproduce?

@BafS
Copy link
Contributor

BafS commented Nov 6, 2023

Same issue on 6.4 BETA 2

In Input.php line 141:

  [Symfony\Component\Console\Exception\InvalidArgumentException]
  The "profile" option does not exist.


Exception trace:

 at .../api/vendor/symfony/console/Input/Input.php:141
 Symfony\Component\Console\Input\Input->getOption() at .../api/vendor/symfony/framework-bundle/EventListener/ConsoleProfilerListener.php:62
 Symfony\Bundle\FrameworkBundle\EventListener\ConsoleProfilerListener->initialize() at .../api/vendor/symfony/event-dispatcher/Debug/WrappedListener.php:116
...

To give more details:

We are overriding Application because we don't want to have the -e shortcut (which is typically use for "email").

We have something like that:

final class Application extends SymfonyFrameworkApplication
{
    public function __construct(KernelInterface $kernel)
    {
        parent::__construct($kernel);

        $inputDefinition = $this->getDefaultInputDefinition();
        $inputDefinition->addOptions([
            new InputOption('--env', null, InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()),
            new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'),
        ]);

        $this->setDefinition($inputDefinition);
    }
}

and the InputOption was not set with "profile". Easy to fix but it's still a BC in my opinion.

@chalasr chalasr closed this as completed in 0e0848b Nov 7, 2023
chalasr added a commit that referenced this issue Nov 7, 2023
…r console commands (keulinho)

This PR was merged into the 6.4 branch.

Discussion
----------

[Console][FrameworkBundle] Fix missing `profile` option for console commands

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #52433
| License       | MIT

Commits
-------

0e0848b Fix missing `profile` option for console commands
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Nov 7, 2023
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Nov 7, 2023
…r console commands (keulinho)

This PR was merged into the 6.4 branch.

Discussion
----------

[Console][FrameworkBundle] Fix missing `profile` option for console commands

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix symfony/symfony#52433
| License       | MIT

Commits
-------

0e0848bc27 Fix missing `profile` option for console commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants