foundry icon indicating copy to clipboard operation
foundry copied to clipboard

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

Open norkunas opened this issue 1 year ago • 13 comments

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?

norkunas avatar Apr 12 '24 04:04 norkunas

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

dmitryuk avatar Apr 12 '24 07:04 dmitryuk

#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

norkunas avatar Apr 12 '24 07:04 norkunas

can you send your services.yaml ?

dmitryuk avatar Apr 12 '24 08:04 dmitryuk

# 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

norkunas avatar Apr 12 '24 08:04 norkunas

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

dmitryuk avatar Apr 12 '24 11:04 dmitryuk

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

norkunas avatar Apr 12 '24 11:04 norkunas

I don't see When attribute in your config

dmitryuk avatar Apr 12 '24 13:04 dmitryuk

Because attributes are declared on the factories itself

norkunas avatar Apr 12 '24 13:04 norkunas

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

dmitryuk avatar Apr 12 '24 14:04 dmitryuk

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

kbond avatar Apr 12 '24 14:04 kbond

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

kbond avatar Apr 12 '24 15:04 kbond

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 avatar Apr 17 '24 04:04 norkunas

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

norkunas avatar Apr 17 '24 05:04 norkunas

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

nikophil avatar Jun 21 '24 15:06 nikophil

It is still the same.

norkunas avatar Jun 25 '24 06:06 norkunas

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

norkunas avatar Jun 25 '24 06:06 norkunas