Skip to content

[Console] Lazy commands with symfony/proxy-manager-bridge error #46350

@mrsuh

Description

@mrsuh

Symfony version(s) affected

6.0.8

Description

Hi!
I know that the correct way to make a command lazy is:

    protected static $defaultName        = 'app:test';
    protected static $defaultDescription = 'description';

but if we want to use lazy attribute with command in service.yaml

    App\Command\:
        resource: '../src/Command'
        lazy: true

there will be the an error:

php bin/console app:test
TypeError {#96
  #message: "ContainerLegSm4V\TestCommand_d286c1a::setName(): Return value must be of type ContainerLegSm4V\TestCommand_d286c1a, App\Command\TestCommand returned"
  #code: 0
  #file: "./var/cache/dev/ContainerLegSm4V/TestCommand_d286c1a.php"
  #line: 143
  trace: {
    ./var/cache/dev/ContainerLegSm4V/TestCommand_d286c1a.php:143 {
      ContainerLegSm4V\TestCommand_d286c1a->setName(string $name): static
      › 
      ›     return $this->valueHolder1624a->setName($name);
      › }
    }
    ./vendor/symfony/console/Command/LazyCommand.php:184 { …}
    ./vendor/symfony/console/Application.php:295 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:80 { …}
    ./vendor/symfony/console/Application.php:171 { …}
    ./vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54 { …}
    ./vendor/autoload_runtime.php:29 { …}
    ./bin/console:11 { …}
  }
}
2022-05-14T10:41:08+00:00 [critical] Uncaught Error: ContainerLegSm4V\TestCommand_d286c1a::setName(): Return value must be of type ContainerLegSm4V\TestCommand_d286c1a, App\Command\TestCommand returned

The error started appearing after this commit which added : static return type to setName() function in Symfony\Component\Console\Command\Command.php

public function setName(string $name): static

How to reproduce

git clone git@github.com:mrsuh/issue-symfony-console.git && cd issue-symfony-console

composer install

php bin/console app:test

Possible Solution

I think

  • it's wrong to add lazy attribute to an already lazy command, but it's worked before and we need to fix it. Maybe we can replace static return type with self?
  • we need to add a note to documentation not to use static return type in lazy services

Additional Context

No response

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