Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Annotation @required causing issue after migrating from 6.0 to 6.1 #48792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jeanatpi opened this issue Dec 26, 2022 · 25 comments · Fixed by #48874
Closed

Annotation @required causing issue after migrating from 6.0 to 6.1 #48792

jeanatpi opened this issue Dec 26, 2022 · 25 comments · Fixed by #48874

Comments

@jeanatpi
Copy link

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

@jeanatpi
Copy link
Author

Some additional infos.

If I change AbstractController::setContainer method from:

/**
   * @required
 */
 #[Required]
    public function setContainer(ContainerInterface $container): ?ContainerInterface
    {
        $previous = $this->container;
        $this->container = $container;

        return $previous;
    }

to:

 #[Required]
    public function setContainer(ContainerInterface $container): ?ContainerInterface
    {
        $previous = $this->container;
        $this->container = $container;

        return $previous;
    }

I don't have the issue anymore.

Here is the output for services for annotations:

image

@alamirault
Copy link
Contributor

Which version of doctrine/annotations do you use ? Annotation extraction is done by this package. You can maybe upgrade it

@derrabus
Copy link
Member

Please create a reproducer.

@jeanatpi
Copy link
Author

@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.

A bit the same setup as Symfony demo app (except that in this case the bug directly appears when going from 6.0 to 6.1).

@derrabus I'll try to create a reproducer but it is not easy as I suspect we are in a similar situation than this one: #32347 (this is why I've posted my annotations.reader service).

As a workaround for now I've put AbstractController in our src/App/Controller, removed the @required annotation and inheritated our controllers to this one.

@derrabus
Copy link
Member

@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.

@jeanatpi
Copy link
Author

jeanatpi commented Dec 28, 2022

@derrabus it was finally simple to reproduce: https://github.com/jeanatpi/symfony_bug_48792

Checking out this repo and running composer install might generate the error

image

Thanks for sharing the info on doctrine annotations. What I still don't get is the difference between my setup and the symfony demo app one.

I continue investigating.

@jeanatpi
Copy link
Author

jeanatpi commented Dec 28, 2022

It looks like the issue appears when adding doctrine/doctrine-bundle.

What I don't get is why this issue is not appearing for symfony demo app. As package list is the similar: https://github.com/symfony/demo/blob/main/composer.json

@derrabus
Copy link
Member

Is there a reason why you've pinned the service contracts to v1?

https://github.com/jeanatpi/symfony_bug_48792/blob/b6cd27b624231f5d0ecb9e1d20a1b8ec02636209/composer.json#L42

Are you able to upgrade to v2 or v3? Can you confirm that the problem goes away if you do?

@nicolas-grekas
Copy link
Member

This might be a doctrine/annotations issue.
It's perfectly legit to have an attribute not be defined as a class.

@derrabus
Copy link
Member

derrabus commented Dec 28, 2022

Yes, but it has never been legit to use an undefined class in a doc block analyzed by Doctrine Annotations.

Doctrine Annotations does not care about the attribute, but it sees a @required docblock annotation. Before #45680, the annotation was ignored because it's not a FQCN and there is no use statement that imports that class, so it assumes it's some PHPDocumentor annotation.

Since #45680 this is a different story: The Required attribute is now imported via use and Doctrine Annotations believes that use statement to correspond to the @required annotation. It then looks up the class: With ServiceContracts 2.2+, it succeeds but because the class is not annotated with @Annotation, it skips it. But with a lower version of the contracts, the class is missing and we get the exception reported above.

Since the issue is solved by bumping service contracts, my suggestion would be to make FrameworkBundle (or DI) conflict with ServiceContracts < 2.2. Maybe we should even do that for all contracts packages: Contracts v1 had been released during the Symfony 4 days, Contracts v2 were shipped with Symfony 5.

@nicolas-grekas
Copy link
Member

I see thanks for the details.
After-the-fact conflicts won't prevent older releases from being installed.

Is there a reason why you've pinned the service contracts to v1?

This is the fix. Doing nothing on our side would be fine to me.

@jeanatpi
Copy link
Author

@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.

@derrabus
Copy link
Member

After-the-fact conflicts won't prevent older releases from being installed.

No, but it would give people a nudge when upgrading to the next Symfony minor. I believe, there should not be a reason to operate a Symfony 6 app with a Contracts v1 package. I'll prepare a PR for 6.3, let's discuss this topic there.

@derrabus
Copy link
Member

@nicolas-grekas #48810

chalasr added a commit that referenced this issue Dec 29, 2022
This PR was merged into the 6.3 branch.

Discussion
----------

Drop v1 contracts packages everywhere

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

Version 1 of the Symfony contracts have been released during the Symfony 4.x days, but still many Symfony 6 components declare compatibility with them:

https://github.com/symfony/symfony/blob/a8bbf632df06b26bbd8a62f22203054bbff34d32/src/Symfony/Component/DependencyInjection/composer.json#L22

This circumstance complicates our build matrix and makes issues like #48792 hard to track down.

Version 2 was released together with Symfony 5 which means that we really only need v1 contracts if we want to actively support Symfony 4 components which are EOL and already unsupported by Symfony 6. I make the bold assumption that no Symfony 6 app should be required to pin any of the contracts packages to v1 or even an outdated v2 minor release.

This is why I propose to drop support for contracts v1 everywhere it did not happen already, making `^2.5 || ^3` the loosest constraint we use for a contracts package on a Symfony 6.3 component or bundle.

Commits
-------

8671ad5 Drop v1 contracts packages everywhere
@BorisCSanto
Copy link

Hi,

I had this problem today after installation of the last version of doctrine/annotations.
To soluce this I'd tried with installation of the last version of symfony/service-contracts but it didn't worked so i have borred the @required annoation in abstractController like @jeanatpi said and it worked now.

@Matt-PMCT
Copy link

Matt-PMCT commented Jan 3, 2023

Just a not that I also am having this problem with a Symfony 5.4 project and doctrine/annotations 2.0. I was able to "fix" it by changing my doctrine/annotations line in composer.json from "^2.0" to "^1.14"

I also likely confirmed that somewhere in my project there is a configuration error, but it isn't clear where. Because I tried created a new symfony project and it doesn't have the problem, but my older project does. Still hunting for the culprit.

@alexislefebvre
Copy link
Contributor

alexislefebvre commented Jan 3, 2023

I have the same issue with Symfony 6.2, upgrading to doctrine/annotations version 2 trigger this error when running composer install. symfony/service-contracts is already on version 3.

mildy long log
Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 1
!!  
!!  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 /home/runner/work/truebadg  
!!    es/truebadges/config/../src/Controller/ (which is being imported from "/hom  
!!    e/runner/work/truebadges/truebadges/config/routes.yaml"). Make sure there i  
!!    s a loader supporting 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().                               

Here is the mentioned file:

# config/routes.yaml
controllers:
    resource: ../src/Controller/
    type: attribute

app_logout:
    path: /logout
    methods: GET

@alexislefebvre
Copy link
Contributor

alexislefebvre commented Jan 3, 2023

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

Input:

git clone [email protected]: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 bin/console cache:warmup -vvv
[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.

@Jeroeny
Copy link
Contributor

Jeroeny commented Jan 6, 2023

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?

@derrabus derrabus reopened this Jan 6, 2023
@fabpot fabpot closed this as completed in 0622750 Jan 7, 2023
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Jan 7, 2023
…ibute (alexislefebvre)

This PR was merged into the 6.3 branch.

Discussion
----------

[FrameworkBundle] remove double required annotation + attribute

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48792 (checked with [this reproducer](symfony/symfony#48792 (comment)))
| License       | MIT
| Doc PR        | no

Annotation and attribute were kept: symfony/symfony#45680 (comment)

To my understanding, it's not necessary since #48810

Alternative to #48868

Commits
-------

8e8772d2c1 remove double required annotation + attribute
@alexislefebvre
Copy link
Contributor

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.

@alexislefebvre
Copy link
Contributor

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.

@Alexandre-T
Copy link
Contributor

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.

@fabpot fabpot reopened this Jan 7, 2023
@alexislefebvre
Copy link
Contributor

alexislefebvre commented Jan 7, 2023

@Alexandre-T thanks for testing, I tried your conf in a project and it doesn't work for me.

Note that the reproducer doesn't use Doctrine ORM or annotations, it's only an empty controller. But this line is causing the error: https://github.com/alexislefebvre/symfony_bug_app_48792/blob/7b4419b0ff736d1e3d1c54e65a4b0bf7fc686c1b/config/routes.yaml#L5


Update: Symfony already use this config this fixtures, it should break with Symfony 6.2 and annotations 2:


You're right about the cache, the issue appears when running bin/console cache:warmup. It also appears when calling the controller, I added a PHPunit test to the reproducer to show this.

I did some testing and it looks like AnnotationReader::addGlobalIgnoredName('required') is not called, so @required is not ignored, and it crashes. It may come from Symfony and/or doctrine/annotations, we need someone with a better knowledge of these bindings to investigate.

@wouterj
Copy link
Member

wouterj commented Jan 7, 2023

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:

->set('annotations.reader', AnnotationReader::class)
->call('addGlobalIgnoredName', [
'required',
service('annotations.dummy_registry')->ignoreOnInvalid(), // dummy arg to register class_exists as annotation loader only when required
])

@alexislefebvre
Copy link
Contributor

alexislefebvre commented Jan 7, 2023

If we comment service('annotations.dummy_registry')->ignoreOnInvalid(), then it works. annotations.dummy_registry is removed there when using annotations v2: https://github.com/symfony/symfony/blob/v6.2.3/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L1643

Alternatively, it works too if we comment all code relevant to annotations.dummy_registry. (note that the idea of removing it was rejected for BC reasons: #48725 (comment))

This means that calling $container->removeDefinition('annotations.dummy_registry'); then service('annotations.dummy_registry') (like in the code shared by wouterj) has the unexpected effect of ignoring the call to addGlobalIgnoredName. So the issue may probably come from this declaration or the loading of the annotations.reader service.

nicolas-grekas added a commit that referenced this issue Jan 9, 2023
…exislefebvre)

This PR was merged into the 5.4 branch.

Discussion
----------

[FrameworkBundle] restore call to addGlobalIgnoredName

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48792 and replace #48912
| License       | MIT
| Doc PR        | no

The service `annotations.dummy_registry` is unregistered with `doctrine/annotations` v2.

This had the unexpected effect to break the call to `addGlobalIgnoredName` with `doctrine/annotations` v2 : #48792 (comment)

By using `nullOnInvalid` instead of `ignoreOnInvalid`, the call to `addGlobalIgnoredName` is restored.

These changes can be tested with this reproducer: https://github.com/alexislefebvre/symfony_bug_app_48792

Downgrading and upgrading `doctrine/annotations` become possible:

```bash
composer require doctrine/annotations:^1.13.0 --with-all-dependencies
composer require doctrine/annotations:^2.0 --with-all-dependencies
```

Thanks to `@derrabus`: #48912 (comment)

Commits
-------

e058874 [FrameworkBundle] restore call to addGlobalIgnoredName
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Jul 28, 2023
…ibute (alexislefebvre)

This PR was merged into the 6.3 branch.

Discussion
----------

[FrameworkBundle] remove double required annotation + attribute

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48792 (checked with [this reproducer](symfony/symfony#48792 (comment)))
| License       | MIT
| Doc PR        | no

Annotation and attribute were kept: symfony/symfony#45680 (comment)

To my understanding, it's not necessary since #48810

Alternative to #48868

Commits
-------

8e8772d2c1 remove double required annotation + attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.