-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected: Test on new 3.4.36 and 5.0.2 projects, and 4.3.9 in my own project
Description
When you have a class with an incorrect namespace, it will try to load it multiple times, probably due to the service PSR loader in the DI config. I've only tested this with commands, though it could happen with any PSR DI loaded class I think. In the reproduce example below, this diff needs to be applied to fix the error:
-namespace App;
+namespace App\Command;
That I get an error is 100% correct. Though I was expecting a different error, like file found but class not in there, or class and file do not match etc. DX wise this can be really hard to solve for new symfony developers when it took me a good 10 minutes to figure out that the namespace was incorrect.
I think I've seen this issue come by on slack every once in a while as well, though I don't remember anyone making a github issue.
How to reproduce
- Create a new Symfony project, or use an existing one where
src/Command
is configured to be loaded by the DI - Create
src/Command/TestCommand.php
- Use the following contents in
TestCommand.php
<?php
namespace App;
use Symfony\Component\Console\Command\Command;
class TestCommand extends Command
{
protected static $defaultName = 'app:test-command';
}
- run bin/console
Possible Solution
Not sure, I haven't dug into the actual stack trace and see if there's a fix due to time constraints
Additional context
Error on fresh 3.4.36
PHP Fatal error: Cannot declare class App\TestCommand, because the name is already in use in /home/lynn/projects/symfony-demos/wrong-error-34/src/Command/TestCommand.php on line 10
In TestCommand.php line 10:
Compile Error: Cannot declare class App\TestCommand, because the name is already in use
Error on fresh 5.0.2
PHP Fatal error: Cannot declare class App\TestCommand, because the name is already in use in /home/lynn/projects/symfony-demos/wrong-error/src/Command/TestCommand.php on line 10
Symfony\Component\ErrorHandler\Error\FatalError^ {#208
-error: array:4 [
"type" => 64
"message" => "Cannot declare class App\TestCommand, because the name is already in use"
"file" => "/home/lynn/projects/symfony-demos/wrong-error/src/Command/TestCommand.php"
"line" => 10
]
#message: "Compile Error: Cannot declare class App\TestCommand, because the name is already in use"
#code: 0
#file: "./src/Command/TestCommand.php"
#line: 10
}