Skip to content

[DI][DX] Cannot declare class with invalid namespace #35256

@linaori

Description

@linaori

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

  1. Create a new Symfony project, or use an existing one where src/Command is configured to be loaded by the DI
  2. Create src/Command/TestCommand.php
  3. 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';
}
  1. 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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DXDX = Developer eXperience (anything that improves the experience of using Symfony)DependencyInjection

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions