Skip to content
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

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

Open
norkunas opened this issue Apr 12, 2024 · 13 comments

Comments

@norkunas
Copy link

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
Copy link
Contributor

dmitryuk commented Apr 12, 2024

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
Copy link
Author

#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
Copy link
Contributor

can you send your services.yaml ?

@norkunas
Copy link
Author

# 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
Copy link
Contributor

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

@norkunas
Copy link
Author

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

@dmitryuk
Copy link
Contributor

I don't see When attribute in your config

@norkunas
Copy link
Author

Because attributes are declared on the factories itself

@dmitryuk
Copy link
Contributor

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

@kbond
Copy link
Member

kbond commented Apr 12, 2024

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

@kbond
Copy link
Member

kbond commented Apr 12, 2024

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

@norkunas
Copy link
Author

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
Copy link
Author

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

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

No branches or pull requests

3 participants