symfony: Cannot declare class Symfony\Component\Messenger\Transport\Doctrine\DoctrineTransportFactory, because the name is already in use

Symfony version(s) affected: 5.2.1 PHP version: 7.4

Description
I use Symfony/messenger component with DoctrineTransport. It works great in the dev environment, but I’m getting this error in the prod environment when I want to dispatch a message.

Warning: Cannot declare class Symfony\Component\Messenger\Transport\Doctrine\DoctrineTransportFactory, because the name is already in use

I tried to trace it, and it seems it’s because of preload functionality in php7.4, and somehow after cache generation, the DoctrineTransportFactory class was included twice.

Additional context
Sentry log: https://sentry.io/share/issue/63ed6759822349c4b991b9ea8fc4ab14/

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 17 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@fabpot I don’t use the symfony/symfony package but still get the error. Could you reopen the issue please? 🙏 I am also happy to provide more information if someone can point me in the right direction. As stated above it seems to be somehow related to preloading. Can’t reproduce it without preloading enabled.

I’ve got a similar error Warning: Cannot declare class Symfony\Component\Security\Core\User\UserChecker, because the name is already in use.

The common thing with this issue is:

  • both classes are aliases.
  • preloading enabled

Minimal reproducer:

<?php
// App_KernelProdContainer.preload.php

use Symfony\Component\DependencyInjection\Dumper\Preloader;

require dirname(__DIR__, 3).'/vendor/autoload.php';

$classes = [];
$classes[] = 'Symfony\Component\Security\Core\User\InMemoryUserChecker';

Preloader::preload($classes);

Maybe it’s a bug in PHP when preloading aliases… Fix that works for me

 // InMemoryUserChecker.php

-class_alias(InMemoryUserChecker::class, UserChecker::class);
+if (!class_exists(UserChecker::class, false)) {
+    class_alias(InMemoryUserChecker::class, UserChecker::class);
+}

You should really move away from using the symfony/symfony package. Even if you are not using Flex, it allows to control which components you require and avoids downloading a lot of code you probably don’t need. The symfony/doctrine-messenger package issue is not the only issue you can have while using symfony/symfony directly.

Is there any other information we could provide to help solve this issue?

Just got the same error.

  • Symfony: v5.2.3
  • PHP: 8.0.2
  • Preloading is active and integrated the same way as mentioned above.

EDIT: I only have the App_KernelProdContainer.preload.php file present. No App_KernelProdDebugContainer.preload.php.

@nicolas-grekas Yes, here is my preload.php file:

if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
    require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
}

@deguif You may need to require the package symfony/doctrine-messenger explicitly if you are using symfony/symfony