Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Lazy commands are loaded, although they were not actually calledΒ #35479

Closed
@lutzpetzoldt

Description

@lutzpetzoldt

Symfony version(s) affected: 4.3.10

Description
After updating from 4.3.9 to 4.3.10 the changes from #35094 lead to loading of lazy commands although they are not actually called. This occurs with commands where the first part of the name matches. For example if you have two lazy commands app:foo and app:foo:bar, and call bin/console app:foo, then app:foo:bar will also be instantiated. This should not happen according to the documentation (link). I think the problem is line 668 in Symfony\Component\Console\Application.php where the command always get instantiated even if it is a lazy command.

How to reproduce

  1. Create two lazy commands where the first part of the name matches, i.e. app:foo and app:foo:bar:
# App/Command/FooCommand.php
class FooCommand extends Command
{
    protected static $defaultName = 'app:foo';

    public function __construct(string $name = null)
    {
        var_dump('app:foo');
        parent::__construct($name);
    }
}

# App/Command/FooBarCommand.php
class FooBarCommand extends Command
{
    protected static $defaultName = 'app:foo:bar';

    public function __construct(string $name = null)
    {
        var_dump('app:foo:bar');
        parent::__construct($name);
    }
}
  1. Call bin/console app:foo. The console output shows that FooBarCommand get also instantiated.

Possible Solution
I think the problem is line 668 in Symfony\Component\Console\Application.php where the command always get instantiated just to get its name even if it is a lazy command.

Additional context

root@327000b4b57a:/var/www/portal# bin/console app:foo
/var/www/portal/src/Command/FooBarCommand.php:13:
string(11) "app:foo:bar"
/var/www/portal/src/Command/FooCommand.php:13:
string(7) "app:foo"
...

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