Dependency on a non-existent service "doctrine.orm.metadata.annotation_reader"
Asked Answered
P

7

24

So I have a Symfony 6.2 API, PHP 8.2 codebase.

While trying to run composer install/update the following error is displaying and I'm wondering how to clear it:

In CheckExceptionOnInvalidReferenceBehaviorPass.php line 83:
The service "doctrine.orm.default_annotation_metadata_driver" has a dependency 
on a non-existent service "doctrine.orm.metadata.annotation_reader".

If I comment out the mappings section in doctrine.yaml file (below) composer runs successfully, however all POST requests to the api will then result in the following error:

Could not find the entity manager for class App\Entity\Token.
Check your Doctrine configuration to make sure it is configured 
to load this entity’s metadata. (500 Internal Server Error)

Scratching my head here to understand how to resolve it. I've a feeling it may be doctrine.yaml related but I could be miles off the mark.

composer.json:

"require": {
        "php": ">=8.2",
        ...
        "doctrine/doctrine-bundle": "^2.8",
        "doctrine/doctrine-migrations-bundle": "^3.2",
        "doctrine/orm": "^2.14",
        ...
    },

doctrine.yaml:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
Pressey answered 2/1, 2023 at 16:40 Comment(2)
How did you create your app? A simple symfony new --webapp or some other more involved approach? Sort of sounds like you are missing a doctrine dependency.Whitherward
With annotations being deprecated ideally we should be removing this component.Felipafelipe
W
44

You are missing the doctrine/annotationsdependency. Try to add in your composer.json file:

"doctrine/annotations": "^1.0",

Then run composer update. Or just run:

composer require doctrine/annotations
Wyn answered 2/1, 2023 at 19:8 Comment(1)
that alone doesn't help, and composer require doctrine/annotations is much better.Lutist
S
28

In symfony 6.4 and up, if you get non-existent service "annotation_reader". errors,

you may need to remove annotations: false from config/packages/framework.yaml.

Sassy answered 3/12, 2023 at 20:17 Comment(3)
Thank you ! Save me from days of debugging hell.Bethelbethena
Thnx! Made me the day! Just upgraded from 6.3 to 6.4 and it broke. It should not break.Goth
Nevertheless I changed my mind. See this answer https://mcmap.net/q/542331/-dependency-on-a-non-existent-service-quot-doctrine-orm-metadata-annotation_reader-quotGoth
L
9

It will not be the exact answer to your question, but my advice is to move PHP 8.1 attributes instead of doctrine annotations.

Trying to install doctrine/annotations, which will require version 2.0 gave me conflicts with other tools that required version 1.x.

Set your Symfony DoctrinBundle mapping type to attribute

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

    form:
        ...
        mappings:
            App:
                is_bundle: false
                type: attribute

More info about the attribute settings can be found here:

https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/attributes-reference.html

Lascivious answered 4/1, 2023 at 21:2 Comment(3)
Did you need to do anything else? This alone doesn't resolve the dependency.Felipafelipe
Ah found it, doctrine.orm.auto_mapping needs to be false too.Felipafelipe
Thanks my LORD hahaha you helped me a lotUtricle
K
2

You should use PHP 8 attributes for routing annotations instead of use deprecated doctrine/annotations package as noted in their deprecation notice : https://www.doctrine-project.org/projects/doctrine-annotations/en/2.0/index.html#deprecation-notice

    PHP 8 introduced attributes, which are a native replacement for annotations. 
    As such, this library is considered feature complete, 
    and should receive exclusively bugfixes and security fixes.
Kinny answered 6/2, 2023 at 21:22 Comment(0)
G
2

I have corrected the error by adding:

composer require doctrine/annotations
Geezer answered 18/9, 2023 at 9:41 Comment(0)
W
1

I ran into the same issue while upgrading symfony 5.4.* to 6.4.

In my case, it was caused by using annotations instead of attributes in my Entities.

I fixed it by changing my annotations to attributes, it's quite easy, just takes some manual work.

In my doctrine.yaml I changed the entity mapping type: type: annotation to type: attribute.

After that you have to edit your Entities:

From:

/**
 * @ORM\Table(name="users")
 * @ORM\Entity
 */

class User implements UserInterface
{
     /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private int $id;
}

To:

#[ORM\Table(name: "users")]
#[ORM\Entity]

class User implements UserInterface
{
    #[ORM\Column(name: "id", type: "integer", nullable: false)]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: "IDENTITY")]
    private int $id;
}

Imports stays the same:

use Doctrine\ORM\Mapping as ORM;

Afterwards you can remove doctrine/annotations from your project by removing it from package.json or running composer remove doctrine/annotations

Webber answered 2/1, 2024 at 11:2 Comment(0)
G
-2

In my case the error appeared when upgrading from symfony 6.3 to 6.4.

I initially followed this answer https://mcmap.net/q/542331/-dependency-on-a-non-existent-service-quot-doctrine-orm-metadata-annotation_reader-quot by @simon-epskamp but after that, I saw that the last time I did composer recipes:update symfony/framework-bundle precisely the recipe set annotations: false, so I did not feel well about removing that line.

I finally saw that the one invoking the annotations was the sensio/framework-extra-bundle so I did this:

composer remove sensio/framework-extra-bundle

After that, the error vanished.

Goth answered 17/3, 2024 at 13:36 Comment(2)
This dependency is not installed per defaultRandellrandene
It was in the past, so if you have some old project and you are upgrading and upgrading, at some point you encounter this situation.Goth

© 2022 - 2025 — McMap. All rights reserved.