zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
607 stars 62 forks source link

Invalid service "App\Foundry\PostFactory": class "Zenstruck\Foundry\ModelFactory" not found while loading "App\Foundry\PostFactory" #593

Closed norkunas closed 1 day ago

norkunas commented 2 months ago

This already happened to me already in 2 projects. Factories are basically set stone and not changing, maybe adding only new ones.

And just randomly with one project this started to occur on the docker image building step.

In the second project with deployer now I'm seeing this also on the cache:clear.

Current workaround is to move this library requirement to non-dev? I've defined on all factories #[When('dev')] and #[When('test')] and don't use them in production code.. So I don't understand why it fails, or this is something that should be fixed in Symfony?

dmitryuk commented 2 months ago

The library must be within dev composer section.

I've defined on all factories #[When('dev')] and #[When('test')] and don't use them in production code

I think you called cache:clear in dev environment, but compose install was with --no-dev Try APP_ENV=production composer install

norkunas commented 2 months ago
#30 [frankenphp_prod 8/8] RUN set -eux;     mkdir -p var/cache var/log var/storage;     composer dump-autoload --classmap-authoritative --no-dev;   composer dump-env prod;     composer run-script --no-dev post-install-cmd;  chmod +x bin/console; sync;
#30 0.061 + mkdir -p var/cache var/log var/storage
#30 0.062 + composer dump-autoload --classmap-authoritative --no-dev
#30 0.225 Generating optimized autoload files (authoritative)
#30 9.151 Generated optimized autoload files (authoritative) containing 19049 classes
#30 9.164 + composer dump-env prod
#30 9.375 Successfully dumped .env files in .env.local.php
#30 9.382 + composer run-script --no-dev post-install-cmd
#30 9.549 
#30 9.549 Run composer recipes at any time to see the status of your Symfony recipes.
#30 9.549 
#30 9.555 Executing script cache:clear [KO]
#30 10.02  [KO]
#30 10.02 Script cache:clear returned with error code 1
#30 10.02 !!  
#30 10.02 !!  In AbstractRecursivePass.php line 177:
#30 10.02 !!                                                                                 
#30 10.02 !!    Invalid service "App\Foundry\PostFactory": class "Zenstru  
#30 10.02 !!    ck\Foundry\ModelFactory" not found while loading "App\Foundry\Factory\PostFactory".                                                           

I think it runs with prod env, because in dunglas/symfony-docker there is ENV APP_ENV=prod set above the failed instructions

dmitryuk commented 2 months ago

can you send your services.yaml ?

norkunas commented 2 months ago
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Security/SecurityIdentity.php'

    app.redis:
        class: Redis
        factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
        arguments: ['%env(REDIS_DSN)%']

when@prod:
    parameters:
        .container.dumper.inline_factories: true
dmitryuk commented 2 months ago

You should exclude Factory path for all environments and include into the test/dev

norkunas commented 2 months ago

That's what #[When] attribute does it, no? :slightly_smiling_face:

dmitryuk commented 2 months ago

I don't see When attribute in your config

norkunas commented 2 months ago

Because attributes are declared on the factories itself

dmitryuk commented 2 months ago

bin/console debug:container —env=production | grep SomeFactory

kbond commented 2 months ago

This does feel like a Symfony issue especially if it just started happening... Did you upgrade Symfony recently?

kbond commented 2 months ago

Possibly related: https://github.com/symfony/symfony/issues/51106 & https://github.com/symfony/symfony/issues/32995

norkunas commented 2 months ago

Sorry, forgot to reply.

bin/console debug:container —env=production | grep SomeFactory

it does not return it

This does feel like a Symfony issue especially if it just started happening... Did you upgrade Symfony recently?

Possibly related: symfony/symfony#51106 & symfony/symfony#32995

Thanks, that must be it.. Then the workaround probably will be not using attributes :(

norkunas commented 2 months ago

But still I don't understand why it started to occur randomly :disappointed:

nikophil commented 5 days ago

Hey @norkunas, does this problem still occur with Foundry v2 / last Symfony version?

norkunas commented 1 day ago

It is still the same.

norkunas commented 1 day ago

But I am going to close this one as this is a Symfony specific issue