doctrine / DoctrineFixturesBundle

Symfony integration for the doctrine/data-fixtures library
MIT License
2.45k stars 202 forks source link

Autowiring (as per docs) does not work when `composer install` was run with `--no-dev` #339

Closed mpdude closed 3 years ago

mpdude commented 3 years ago

The documentation at https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html suggests to install this bundle as a --dev dependency and to use Symfony Autowiring to register fixtures. That is, the services section for example in config.yml looks similar to this:

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: '../../*'
        exclude: '../../{DependencyInjection,Entity,Tests}'

This way, fixtures from the App/Fixtures directory will be defined as services and tagged appropriately.

When using composer install --no-dev in production, the entire bundle is not installed, but the Fixtures directory is still scanned. This causes the following error:

$ APP_ENV=prod bin/console  -vvv

In DefinitionErrorExceptionPass.php line 37:

  [Symfony\Component\DependencyInjection\Exception\RuntimeException]
  Class "Doctrine\Bundle\FixturesBundle\ORMFixtureInterface" not found while loading "App\Fixtures\MyFixture".

Exception trace:
  at [path]/vendor/symfony/dependency-injection/Compiler/DefinitionErrorExceptionPass.php:37
 Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass->processValue() at [path]/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php:60
 Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue() at [path]/vendor/symfony/dependency-injection/Compiler/DefinitionErrorExceptionPass.php:30
 Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass->processValue() at [path]/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php:39
 Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->process() at [path]/vendor/symfony/dependency-injection/Compiler/Compiler.php:140
 Symfony\Component\DependencyInjection\Compiler\Compiler->compile() at [path]/vendor/symfony/dependency-injection/ContainerBuilder.php:789
 Symfony\Component\DependencyInjection\ContainerBuilder->compile() at [path]/vendor/symfony/http-kernel/Kernel.php:643
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at [path]/vendor/symfony/http-kernel/Kernel.php:135
 Symfony\Component\HttpKernel\Kernel->boot() at [path]/vendor/symfony/framework-bundle/Console/Application.php:64
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at [path]/vendor/symfony/console/Application.php:148
 Symfony\Component\Console\Application->run() at [path]/bin/console:41

Is this a configuration issue on my side or a glitch in the documentation?

As a workaround, you can exclude the Fixtures directory from autowiring and use a dedicated section in config_dev.yml (for example) to add it back again. Are there any better solutions?

borodiliz commented 3 years ago

@mpdude what's your Symfony version? I'm facing a similar problem after upgrading from Symfony 5.2 to 5.3

arneee commented 3 years ago

Same for me after upgrading to SF 5.3, I couldn't find any good solution beside of this dirty hack:

if($_ENV["APP_ENV"] !== "test") {
    class AppFixtures {
      //EMPTY
    }
} else {
    class AppFixtures extends Fixture {
       //Real fixtures go here
   }
}

Edit: Nevermind, the workaround in the first post is better (autowire the classes in the services_dev.yaml).

borodiliz commented 3 years ago

@arneee another hack: remove your fixtures folder on your deployment script/process

ker0x commented 3 years ago

I'm experiencing a similar issue after upgrading to Symfony 5.3.

When I run the following commands:

composer install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction
composer dump-autoload --classmap-authoritative --no-dev
composer symfony:dump-env prod
composer run-script --no-dev post-install-cmd

I get this error:

Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 1
!!  
!!  In ClassExistenceResource.php line 74:
!!                                                                                 
!!    Class "Doctrine\Bundle\FixturesBundle\Fixture" not found while loading "App  
!!    \DataFixtures\AppFixtures".                                                  
!!                                                                                 
!!  
!!  
Script @auto-scripts was called via post-install-cmd

Steps to reproduce:

With Symfony 5.2

symfony new symfony-5.2 --version=5.2 --full

cd symfony-5.2

composer require --dev orm-fixtures

composer install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction
composer dump-autoload --classmap-authoritative --no-dev
composer symfony:dump-env prod
composer run-script --no-dev post-install-cmd

With Symfony 5.3

symfony new symfony-5.3 --version=5.3 --full

cd symfony-5.3

composer require --dev orm-fixtures

composer install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction
composer dump-autoload --classmap-authoritative --no-dev
composer symfony:dump-env prod
composer run-script --no-dev post-install-cmd
rogamoore commented 3 years ago

Here another workaround without creating a separate services_dev.yaml

// services.yaml

services:
// ...
    App\:
        resource: "../src/"
        exclude:
            - "../src/DataFixtures/" # exclude by default
            - "../src/DependencyInjection/"
            - "../src/Entity/"
            - "../src/Kernel.php"
            - "../src/Tests/"
// ...
when@dev:
    services:
        _defaults:
            autowire: true
            autoconfigure: true

        App\DataFixtures\:
            resource: '../src/DataFixtures/'
nicolas-grekas commented 3 years ago

Duplicate of https://github.com/symfony/symfony/issues/41548, fixed by https://github.com/symfony/symfony/pull/41673