Skip to content

[FrameworkBundle] Ignore AnnotationException exceptions in the AnnotationsCacheWarmer #20959

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
wants to merge 3 commits into from

Conversation

fancyweb
Copy link
Contributor

@fancyweb fancyweb commented Dec 16, 2016

Q A
Branch? 3.2
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

I ran into a case that led to an AnnotationException in the AnnotationsCacheWarmer. I deduced that these exceptions should be ignored to not break the cache warming process.

The case :

  • You have a controller in your AppBundle\Controller\ directory.
  • You have an annotation in this controller that will never be found (because you made a mistake in the class / annotation name) or not every time : for example your controller is only used in dev env and it uses an annotation that is defined in a vendor bundle that is only under the require-dev key in your composer.json because you don't need it in your production deployment. So in production, your controller exists but will never be used. However the vendor bundle does not exist.
  • You dump your autoload files with composer using the --optimize option.
  • You end up with an AnnotationException.

Why :

  • In the FrameworkExtension, the **Bundle\Controller\ pattern is added as an "annotated class to compile".
  • In the AddClassesToCachePass, all the classes defined in the autoload_classmap.php file are matched against the previous pattern.
  • Your controller will match. So even if you don't use it at all in your application, the annotation will still be read and the exception will be thrown.

@fancyweb
Copy link
Contributor Author

Coding standard issues are unrelated to my change, should I still apply them ?

@@ -75,6 +76,8 @@ public function warmUp($cacheDir)
$this->readAllComponents($reader, $class);
} catch (\ReflectionException $e) {
// ignore failing reflection
} catch (AnnotationException $e) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a comment explaining why

@fancyweb
Copy link
Contributor Author

@fabpot I added a short explanation and a link that reference this PR.

Also I will ask again, should I apply the suggested coding standard changes even if they are not related to my code ?

@xabbuh
Copy link
Member

xabbuh commented Dec 21, 2016

@fancyweb No, please ignore them.

@nicolas-grekas nicolas-grekas added this to the 3.2 milestone Dec 26, 2016
* environment but is always added to the annotations.map file by some Symfony default behaviors,
* and you always end up with a not found Annotation.
*
* cf https://github.com/symfony/symfony/pull/20959 for a longer explanation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be removed, we don't add links to github: the git history (git blame) should have everything instead (which it has)

@xabbuh
Copy link
Member

xabbuh commented Dec 27, 2016

Isn't this a bug fix?

@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

nicolas-grekas added a commit that referenced this pull request Dec 27, 2016
… the AnnotationsCacheWarmer (fancyweb)

This PR was squashed before being merged into the 3.2 branch (closes #20959).

Discussion
----------

[FrameworkBundle] Ignore AnnotationException exceptions in the AnnotationsCacheWarmer

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

I ran into a case that led to an `AnnotationException` in the `AnnotationsCacheWarmer`. I deduced that these exceptions should be ignored to not break the cache warming process.

The case :
* You have a controller in your `AppBundle\Controller\` directory.
* You have an annotation in this controller that will never be found (because you made a mistake in the class / annotation name) or not every time : for example your controller is only used in dev env and it uses an annotation that is defined in a vendor bundle that is only under the `require-dev` key in your `composer.json` because you don't need it in your production deployment. So in production, your controller exists but will never be used. However the vendor bundle does not exist.
* You dump your autoload files with composer using the `--optimize` option.
* You end up with an `AnnotationException`.

Why :
* In the [`FrameworkExtension`](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L189), the `**Bundle\Controller\` pattern is added as an "annotated class to compile".
* In the [`AddClassesToCachePass`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/DependencyInjection/AddClassesToCachePass.php#L50), all the classes defined in the `autoload_classmap.php` file are matched against the previous pattern.
* Your controller will match. So even if you don't use it at all in your application, the annotation will still be read and the exception will be thrown.

Commits
-------

6749569 [FrameworkBundle] Ignore AnnotationException exceptions in the AnnotationsCacheWarmer
@nicolas-grekas
Copy link
Member

Yes it is :)

@xabbuh
Copy link
Member

xabbuh commented Dec 27, 2016

Sorry, I missed the fact that the class wasn't present in 3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants