symfony: Annotation @required causing issue after migrating from 6.0 to 6.1

Symfony version(s) affected

6.1.0

Description

Hello,

I just tried to migrate my Symfony 6.0 to 6.1 and since then I have the following issue:

[Semantical Error] The annotation "@Symfony\Contracts\Service\Attribute\Required" in method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer() was never imported. Did you maybe forget to add a "use" statement for this annotation?

When I remove “@required” annotation from AbstractController.php file if works properly.

Any ideas on why I have this error suddently raised?

How to reproduce

I’m not sure on how to provide a reproducer…

Possible Solution

No response

Additional Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 25 (18 by maintainers)

Commits related to this issue

Most upvoted comments

The reproducer also experiences the bug with symfony/service-contracts:^3.0, can this issue be reopened? Or is it fine to just continue with the PR only?

I have been able to create a reproducer: https://github.com/alexislefebvre/symfony_bug_app_48792

Input:

git clone git@github.com:alexislefebvre/symfony_bug_app_48792.git
cd ./symfony_bug_app_48792/
composer install

Output:

Installing dependencies from lock file (including require-dev)
[…]

Run composer recipes at any time to see the status of your Symfony recipes.

Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 1
!!  
!!   // Clearing the cache for the dev environment with debug true                  
!!  
!!  
!!  In FileLoader.php line 178:
!!                                                                                 
!!    [Semantical Error] The class "Symfony\Contracts\Service\Attribute\Required"  
!!     is not annotated with @Annotation.                                          
!!    Are you sure this class can be used as annotation?                           
!!    If so, then you need to add @Annotation to the _class_ doc comment of "Symf  
!!    ony\Contracts\Service\Attribute\Required".                                   
!!    If it is indeed no annotation, then you need to add @IgnoreAnnotation("requ  
!!    ired") to the _class_ doc comment of method Symfony\Bundle\FrameworkBundle\  
!!    Controller\AbstractController::setContainer() in {"path":"..\/src\/Controll  
!!    er\/","namespace":"App\\Controller"} (which is being imported from "/dev/sh  
!!    m/symfony_bug_app_48792/config/routes.yaml"). Make sure there is a loader s  
!!    upporting the "attribute" type.                                              
!!                                                                                 
!!  
!!  In AnnotationException.php line 36:
!!                                                                                 
!!    [Semantical Error] The class "Symfony\Contracts\Service\Attribute\Required"  
!!     is not annotated with @Annotation.                                          
!!    Are you sure this class can be used as annotation?                           
!!    If so, then you need to add @Annotation to the _class_ doc comment of "Symf  
!!    ony\Contracts\Service\Attribute\Required".                                   
!!    If it is indeed no annotation, then you need to add @IgnoreAnnotation("requ  
!!    ired") to the _class_ doc comment of method Symfony\Bundle\FrameworkBundle\  
!!    Controller\AbstractController::setContainer().                               
!!                                                                                 
!!  
!!  cache:clear [--no-warmup] [--no-optional-warmers]
!!  
!!  
Script @auto-scripts was called via post-install-cmd
Full exception trace when running <kbd>bin/console cache:warmup -vvv</kbd>
[critical] Error thrown while running command "cache:warmup -vvv". Message: "[Semantical Error] The class "Symfony\Contracts\Service\Attribute\Required" is not annotated with @Annotation.
Are you sure this class can be used as annotation?
If so, then you need to add @Annotation to the _class_ doc comment of "Symfony\Contracts\Service\Attribute\Required".
If it is indeed no annotation, then you need to add @IgnoreAnnotation("required") to the _class_ doc comment of method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer() in {"path":"..\/src\/Controller\/","namespace":"App\\Controller"} (which is being imported from "/dev/shm/symfony_bug_app_48792/config/routes.yaml"). Make sure there is a loader supporting the "attribute" type."
[debug] Command "cache:warmup -vvv" exited with code "1"

In FileLoader.php line 178:
                                                                                                                                                                                                                             
  [Symfony\Component\Config\Exception\LoaderLoadException]                                                                                                                                                                   
  [Semantical Error] The class "Symfony\Contracts\Service\Attribute\Required" is not annotated with @Annotation.                                                                                                             
  Are you sure this class can be used as annotation?                                                                                                                                                                         
  If so, then you need to add @Annotation to the _class_ doc comment of "Symfony\Contracts\Service\Attribute\Required".                                                                                                      
  If it is indeed no annotation, then you need to add @IgnoreAnnotation("required") to the _class_ doc comment of method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer() in {"path":"..\/src\/  
  Controller\/","namespace":"App\\Controller"} (which is being imported from "/dev/shm/symfony_bug_app_48792/config/routes.yaml"). Make sure there is a loader supporting the "attribute" type.                              
                                                                                                                                                                                                                             

Exception trace:
  at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:178
 Symfony\Component\Config\Loader\FileLoader->doImport() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:94
 Symfony\Component\Config\Loader\FileLoader->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/YamlFileLoader.php:208
 Symfony\Component\Routing\Loader\YamlFileLoader->parseImport() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/YamlFileLoader.php:99
 Symfony\Component\Routing\Loader\YamlFileLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:163
 Symfony\Component\Config\Loader\FileLoader->doImport() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:94
 Symfony\Component\Config\Loader\FileLoader->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/Configurator/RoutingConfigurator.php:45
 Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.php:81
 App\Kernel->configureRoutes() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.php:202
 App\Kernel->loadRoutes() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/ObjectLoader.php:55
 Symfony\Component\Routing\Loader\ObjectLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/DelegatingLoader.php:37
 Symfony\Component\Config\Loader\DelegatingLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Routing/DelegatingLoader.php:67
 Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Routing/Router.php:65
 Symfony\Bundle\FrameworkBundle\Routing\Router->getRouteCollection() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Router.php:317
 Symfony\Component\Routing\Router->getMatcherDumperInstance() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Router.php:255
 Symfony\Component\Routing\Router->Symfony\Component\Routing\{closure}() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/ResourceCheckerConfigCacheFactory.php:36
 Symfony\Component\Config\ResourceCheckerConfigCacheFactory->cache() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Router.php:263
 Symfony\Component\Routing\Router->getMatcher() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Routing/Router.php:92
 Symfony\Bundle\FrameworkBundle\Routing\Router->warmUp() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/CacheWarmer/RouterCacheWarmer.php:42
 Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer->warmUp() at /dev/shm/symfony_bug_app_48792/vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:96
 Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Command/CacheWarmupCommand.php:69
 Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand->execute() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Command/Command.php:312
 Symfony\Component\Console\Command\Command->run() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Application.php:1040
 Symfony\Component\Console\Application->doRunCommand() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Console/Application.php:88
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Application.php:314
 Symfony\Component\Console\Application->doRun() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Console/Application.php:77
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Application.php:168
 Symfony\Component\Console\Application->run() at /dev/shm/symfony_bug_app_48792/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /dev/shm/symfony_bug_app_48792/vendor/autoload_runtime.php:29
 require_once() at /dev/shm/symfony_bug_app_48792/bin/console:11

In AnnotationException.php line 36:
                                                                                                                                                                                                        
  [Doctrine\Common\Annotations\AnnotationException]                                                                                                                                                     
  [Semantical Error] The class "Symfony\Contracts\Service\Attribute\Required" is not annotated with @Annotation.                                                                                        
  Are you sure this class can be used as annotation?                                                                                                                                                    
  If so, then you need to add @Annotation to the _class_ doc comment of "Symfony\Contracts\Service\Attribute\Required".                                                                                 
  If it is indeed no annotation, then you need to add @IgnoreAnnotation("required") to the _class_ doc comment of method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer().  
                                                                                                                                                                                                        

Exception trace:
  at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:36
 Doctrine\Common\Annotations\AnnotationException::semanticalError() at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:833
 Doctrine\Common\Annotations\DocParser->Annotation() at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:708
 Doctrine\Common\Annotations\DocParser->Annotations() at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:368
 Doctrine\Common\Annotations\DocParser->parse() at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php:206
 Doctrine\Common\Annotations\AnnotationReader->getMethodAnnotations() at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/PsrCachedReader.php:155
 Doctrine\Common\Annotations\PsrCachedReader->fetchFromCache() at /dev/shm/symfony_bug_app_48792/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/PsrCachedReader.php:119
 Doctrine\Common\Annotations\PsrCachedReader->getMethodAnnotations() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/AnnotationClassLoader.php:364
 Symfony\Component\Routing\Loader\AnnotationClassLoader->getAnnotations() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/AnnotationClassLoader.php:129
 Symfony\Component\Routing\Loader\AnnotationClassLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/Loader.php:48
 Symfony\Component\Config\Loader\Loader->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/Psr4DirectoryLoader.php:90
 Symfony\Component\Routing\Loader\Psr4DirectoryLoader->loadFromDirectory() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/Psr4DirectoryLoader.php:46
 Symfony\Component\Routing\Loader\Psr4DirectoryLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:142
 Symfony\Component\Config\Loader\FileLoader->doImport() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:94
 Symfony\Component\Config\Loader\FileLoader->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/YamlFileLoader.php:208
 Symfony\Component\Routing\Loader\YamlFileLoader->parseImport() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/YamlFileLoader.php:99
 Symfony\Component\Routing\Loader\YamlFileLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:163
 Symfony\Component\Config\Loader\FileLoader->doImport() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/FileLoader.php:94
 Symfony\Component\Config\Loader\FileLoader->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/Configurator/RoutingConfigurator.php:45
 Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator->import() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.php:81
 App\Kernel->configureRoutes() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.php:202
 App\Kernel->loadRoutes() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Loader/ObjectLoader.php:55
 Symfony\Component\Routing\Loader\ObjectLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/Loader/DelegatingLoader.php:37
 Symfony\Component\Config\Loader\DelegatingLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Routing/DelegatingLoader.php:67
 Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader->load() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Routing/Router.php:65
 Symfony\Bundle\FrameworkBundle\Routing\Router->getRouteCollection() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Router.php:317
 Symfony\Component\Routing\Router->getMatcherDumperInstance() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Router.php:255
 Symfony\Component\Routing\Router->Symfony\Component\Routing\{closure}() at /dev/shm/symfony_bug_app_48792/vendor/symfony/config/ResourceCheckerConfigCacheFactory.php:36
 Symfony\Component\Config\ResourceCheckerConfigCacheFactory->cache() at /dev/shm/symfony_bug_app_48792/vendor/symfony/routing/Router.php:263
 Symfony\Component\Routing\Router->getMatcher() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Routing/Router.php:92
 Symfony\Bundle\FrameworkBundle\Routing\Router->warmUp() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/CacheWarmer/RouterCacheWarmer.php:42
 Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer->warmUp() at /dev/shm/symfony_bug_app_48792/vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:96
 Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Command/CacheWarmupCommand.php:69
 Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand->execute() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Command/Command.php:312
 Symfony\Component\Console\Command\Command->run() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Application.php:1040
 Symfony\Component\Console\Application->doRunCommand() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Console/Application.php:88
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Application.php:314
 Symfony\Component\Console\Application->doRun() at /dev/shm/symfony_bug_app_48792/vendor/symfony/framework-bundle/Console/Application.php:77
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /dev/shm/symfony_bug_app_48792/vendor/symfony/console/Application.php:168
 Symfony\Component\Console\Application->run() at /dev/shm/symfony_bug_app_48792/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /dev/shm/symfony_bug_app_48792/vendor/autoload_runtime.php:29
 require_once() at /dev/shm/symfony_bug_app_48792/bin/console:11

cache:warmup [--no-optional-warmers]

If we delete src/Controller/DefaultController.php then it works again.

@derrabus @nicolas-grekas I can confirm that updating symfony/service-contracts to 3 has fixed the issue. Thanks for you help.

I think this can be closed then.

When I run symfony console debug:container annotations.reader --show-arguments, I can’t see the addGlobalIgnoredName method call in there, yet we do configure it: https://github.com/symfony/symfony/blob/6f755295892cba0ae84872221f93b6c582a88104/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.php#L25-L29

I tried to add a test in Symfony based on the reproducer but the error never pop. It must be a very different context than from a standalone Symfony app.

I tried to detect this bug on our projects. It only appears when doctrine/annotations is called and when doctrine’s mapping is specifying the annotations as type. Did you try to add them?

    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotations # if you use annotations, the bug appears. If you attributes and (wrongly) specify annotations, the bug appears too. If I use attributes and specify attributes, bug disappears.
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

I hope it will help the error to pop, @alexislefebvre . Be careful with the cache. I manually delete the cache directly between each testsuite.

Please reopen the issue, the problem still exists with Symfony 6.2 (and maybe 6.0 and 6.1). It was closed from the merge because I referenced it but it has been merged on 6.3 only.

@alamirault we are not using directly doctrine/annotations in composer.json. In composer.lock file we have version 1.14.2. It is a dependency of doctrine/doctrine-bundle.

So, Doctrine Annotations is installed although you don’t use it at all? This is an issue that’s being worked on, see doctrine/DoctrineBundle#1598

I’ll try to create a reproducer

All right. Please get back to us as soon as you have one. It helps others to track down the issue and work on a fix.