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

Bundle dependencies of dev dependencies are registered in all environments #329

Closed
jakzal opened this issue Mar 6, 2018 · 22 comments
Closed

Comments

@jakzal
Copy link
Member

@jakzal jakzal commented Mar 6, 2018

I stumbled upon this while trying to install debug to a project based off symfony/skeleton. Everything worked fine in a dev environment, but when trying to run in prod it blows up due to some bundles missing.

Steps to reproduce:

composer create-project symfony/skeleton demo
cd demo
composer req --dev debug
APP_ENV=prod composer update --no-dev

The last command will fail with:

!!  PHP Fatal error:  Uncaught Error: Class 'Symfony\Bundle\TwigBundle\TwigBundle' not found in /Users/jakubzal/Projects/demo/src/Kernel.php:32
!!  Stack trace:
!!  #0 /Users/jakubzal/Projects/demo/vendor/symfony/http-kernel/Kernel.php(403): App\Kernel->registerBundles()
!!  #1 /Users/jakubzal/Projects/demo/vendor/symfony/http-kernel/Kernel.php(122): Symfony\Component\HttpKernel\Kernel->initializeBundles()
!!  #2 /Users/jakubzal/Projects/demo/vendor/symfony/framework-bundle/Console/Application.php(64): Symfony\Component\HttpKernel\Kernel->boot()
!!  #3 /Users/jakubzal/Projects/demo/vendor/symfony/console/Application.php(143): Symfony\Bundle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
!!  #4 /Users/jakubzal/Projects/demo/bin/console(39): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput))
!!  #5 {main}
!!    thrown in /Users/jakubzal/Projects/demo/src/Kernel.php on line 32

That's because TwigBundle and MonologBundle were brought by the WebProfilerBundle.

Honestly, I don't think this can be easily fixed in flex, but maybe someone will have an idea.

A manual solution is to update bundles.php to register twig and monolog bundles in dev/test environments only, and then move the config files.

@Pierstoval
Copy link
Contributor

@Pierstoval Pierstoval commented Mar 6, 2018

Maybe we should add a feature in Flex that allow specifying somewhere that a package should be registered in require-dev instead of require, but I have absolutely no idea of how to do this 🤔 Because the --dev in Composer is not enough, it seems

@jakzal
Copy link
Member Author

@jakzal jakzal commented Mar 6, 2018

@Pierstoval I think it has more to do with the fact that recipes for profiler's dependencies register their bundles in all environments. Which makes sense when you installl TwigBundle. A bit less when you install something else that depends on TwigBundle.

@xsuntel
Copy link

@xsuntel xsuntel commented Mar 10, 2018

Hi, guys
I try to release my web application using Symfony 4.0.6 on Elastic Beanstalk of Amazon Web Service. However, I have a problem about "Script cache:clear returned with error code 255 !! (Executor::NonZeroExitStatus)" when the application is deploying

Could you please give me an advice?

EC2 : /var/log/eb-activity.log

Installing dependencies (including require-dev) from lock file
  Package operations: 157 installs, 0 updates, 0 removals
    .... 
    - Installing symfony/twig-bundle (v4.0.6): Loading from cache
    - Installing symfony/profiler-pack (v1.0.3): Loading from cache
  Generating autoload files
  ocramius/package-versions:  Generating version class...
  ocramius/package-versions: ...done generating version class
  Executing script cache:clear [KO]
   [KO]
  Script cache:clear returned with error code 255
  !!   (Executor::NonZeroExitStatus)

[2018-03-09T13:38:10.167Z] INFO  [3189]  - [Application update app-0ee2-180309_223714@54/AppDeployStage0/AppDeployPreHook/10_composer_install.sh] : Activity failed.
[2018-03-09T13:38:10.168Z] INFO  [3189]  - [Application update app-0ee2-180309_223714@54/AppDeployStage0/AppDeployPreHook] : Activity failed.
[2018-03-09T13:38:10.168Z] INFO  [3189]  - [Application update app-0ee2-180309_223714@54/AppDeployStage0] : Activity failed.
[2018-03-09T13:38:10.168Z] INFO  [3189]  - [Application update app-0ee2-180309_223714@54] : Completed activity. Result:
  Application update - Command CMD-AppDeploy failed
    "require": {
        "php": "^7.1.3",
        "doctrine/doctrine-bundle": "^1.8",
        "doctrine/doctrine-cache-bundle": "^1.3",
        "doctrine/doctrine-fixtures-bundle": "^3.0",
        "doctrine/doctrine-migrations-bundle": "^1.0",
        "facebook/graph-sdk": "^5.6",
        "friendsofsymfony/elastica-bundle": "^5.0",
        "friendsofsymfony/jsrouting-bundle": "^2.2",
        "friendsofsymfony/rest-bundle": "^2.3",
        "google/apiclient": "^2.0",
        "guzzlehttp/guzzle": "~6.0",
        "hwi/oauth-bundle": "^0.6.0",
        "nexylan/slack-bundle": "^2.0",
        "php-http/guzzle6-adapter": "^1.1",
        "php-http/httplug-bundle": "^1.8",
        "php-http/httplug-pack": "^1.1",
        "sensio/framework-extra-bundle": "^5.0",
        "sensiolabs/security-checker": "^4.1",
        "symfony/asset": "^4.0",
        "symfony/console": "^4.0",
        "symfony/expression-language": "^4.0",
        "symfony/finder": "^4.0",
        "symfony/flex": "^1.0",
        "symfony/form": "^4.0",
        "symfony/framework-bundle": "^4.0",
        "symfony/lts": "^4@dev",
        "symfony/maker-bundle": "^1.1",
        "symfony/monolog-bundle": "^3.1",
        "symfony/orm-pack": "^1.0",
        "symfony/polyfill-apcu": "^1.7",
        "symfony/requirements-checker": "^1.0",
        "symfony/security-bundle": "^4.0",
        "symfony/serializer-pack": "^1.0",
        "symfony/swiftmailer-bundle": "^3.1",
        "symfony/translation": "^4.0",
        "symfony/validator": "^4.0",
        "symfony/webpack-encore-pack": "^1.0",
        "symfony/yaml": "^4.0",
        "twig/extensions": "^1.5",
        "twig/twig": "^2.4"
    },
    "require-dev": {
        "friendsofphp/php-cs-fixer": "^2.7",
        "phpunit/phpunit": "^6.5",
        "symfony/browser-kit": "^4.0",
        "symfony/css-selector": "^4.0",
        "symfony/debug-bundle": "^4.0",
        "symfony/dotenv": "^4.0",
        "symfony/phpunit-bridge": "^4.0",
        "symfony/profiler-pack": "^1.0"
    },

I have checked the error message following

$composer install --no-dev --optimize-autoloader

Generating optimized autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 255
!!  PHP Fatal error:  Uncaught Error: Class 'Symfony\Bundle\TwigBundle\TwigBundle' not found in C:\Users\xsunt\PhpstormProjec
ts\xsun\tv\4.0\src\Kernel.php:32
!!  Stack trace:
!!  #0 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\http-kernel\Kernel.php(403): App\Kernel->registerBundles()
!!  #1 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\http-kernel\Kernel.php(122): Symfony\Component\HttpKernel\K
ernel->initializeBundles()
!!  #2 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\framework-bundle\Console\Application.php(64): Symfony\Compo
nent\HttpKernel\Kernel->boot()
!!  #3 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\console\Application.php(143): Symfony\Bundle\FrameworkBundl
e\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\Conso
leOutput))
!!  #4 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\bin\console(39): Symfony\Component\Console\Application->run(Object(Symfony
\Component\Console\Input\ArgvInput))
!!  #5 {main}
!!    thrown in C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\src\Kernel.php on line 32
!!
@xsuntel
Copy link

@xsuntel xsuntel commented Mar 11, 2018

I have updated the symfony/flex (v1.0.70 => v1.0.71) and I have checked it following.

###> symfony/framework-bundle ###
APP_ENV=prod

$ composer install --no-dev --optimize-autoloader
~~~~
  - Removing symfony/process (v4.0.6)
Generating optimized autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 255
!!  PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "DebugBundle" from namespace "Symfony\Bundle\DebugBundle".
!!  Did you forget a "use" statement for another namespace? in C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\src\Kernel.php:32
!!  Stack trace:
!!  #0 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\http-kernel\Kernel.php(403): App\Kernel->registerBundles()
!!  #1 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\http-kernel\Kernel.php(122): Symfony\Component\HttpKernel\Kernel->initializeBundles()
!!  #2 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\framework-bundle\Console\Application.php(64): Symfony\Component\HttpKernel\Kernel->boot()
!!  #3 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\console\Application.php(143): Symfony\Bundle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component
\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
!!  #4 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\bin\console(39) in C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\src\Kernel.php on line 32
!!

I think that we need to check three bundles following

class Kernel extends BaseKernel
{
    public function registerBundles()
    {
        $contents = require $this->getProjectDir().'/config/bundles.php';
        foreach ($contents as $class => $envs) {
            if (isset($envs['all']) || isset($envs[$this->environment])) {
                **yield new $class();**
            }
        }
    }
cat /config/bundles.php

    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],

Could you please give me an advice?

@weaverryan
Copy link
Member

@weaverryan weaverryan commented Mar 15, 2018

Make sure you APP_ENV environment bar is set to prod on your server before running composer install. I believe it’s not, so the cache clear post-install cmd is trying to boot Symfony in the dev environment and thus reference the DebugBundle

@weaverryan
Copy link
Member

@weaverryan weaverryan commented Mar 15, 2018

About the original issue, yea, I don’t like this :/. It seems like Flex would need to become a bit “dynamic” and change the recipes based on if it’s a dev install or not... which is probably not something we want to do.

What if Flex could detect when a recipe will enable a bundle in “all” environments, but that dependency was installed as a dev dependency? It could then throw an annoying warning that you should require that demo as a non dev dep, at least

@jvasseur
Copy link

@jvasseur jvasseur commented Mar 15, 2018

Maybe we aren't looking at this problem through the right angle.

Most of the time this problem will happens because of the profiler -> twig bundle dependency. Maybe this is this dependency that should be solved. (It would solve the problem of people requiring the profiler bundle instead of the profiler pack at the same time).

It could be solved by making the profiler use its own twig instance. This would have the additional benefits of stopping the profiler twig extension from beeing leaked into the "application" twig environment (this hurts the dev/prod parity). And in the opposite direction prevent user extensions from breaking the profiler.

This would need some work to do it without breaking BC (because some bundles could have profiler templates that require a extension registered into the application twig instance) but I think it can be done.

@xsuntel
Copy link

@xsuntel xsuntel commented Mar 16, 2018

Hi @weaverryan , @jvasseur

Thank you for your information.

I hope that this issue can be resolved. In my case, I can't deploy my new web application using SF4.0.6 to Elastic Beanstalk on AWS. Maybe, If someone had the same issue, their application would not be deployed to production servers.

Let me know if you need any information about testing it.

@xsuntel
Copy link

@xsuntel xsuntel commented Mar 16, 2018

I have tested this issue after installing Symfony Demo Application 4.0.6 . but I am considering that It seems to be the same issue.

  • Windows 10 64bit
  • PhpStorm 2017.3
  • JRE: 1.8.0_152-release-1024-b15 amd64
  • JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
xsunt@Ryan MINGW64 ~/PhpstormProjects/xsun/tv/demo (master)
$ php bin/symfony_requirements

Symfony Requirements Checker
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> PHP is using the following php.ini file:
  C:\Bitnami\wampstack-7.1.12-0\php\php.ini

> Checking Symfony requirements:
  ...........................W.W......


 [OK]
 Your system is ready to run Symfony projects


Optional recommendations to improve your setup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 * intl extension should be available
   > Install and enable the intl extension (used for validators).

 * realpath_cache_size should be at least 5M in php.ini
   > Setting "realpath_cache_size" to e.g. "5242880" or "5M" in
   > php.ini* may improve performance on Windows significantly in some
   > cases.


Note  The command console could use a different php.ini file
~~~~  than the one used with your web server. To be on the
      safe side, please check the requirements from your web
      server using the web/config.php script.


xsunt@Ryan MINGW64 ~/PhpstormProjects/xsun/tv/demo (master)
$ php bin/console cache:clear --env=prod --no-debug

 // Clearing the cache for the prod environment with debug false

 [WARNING] Calling cache:clear without the --no-warmup option is deprecated since version 3.3. Cache warmup should be
           done with the cache:warmup command instead.

 [OK] Cache for the "prod" environment (debug=false) was successfully cleared.


xsunt@Ryan MINGW64 ~/PhpstormProjects/xsun/tv/demo (master)
$ composer install --no-dev --optimize-autoloader
Loading composer repositories with package information
Installing dependencies from lock file
Package operations: 0 installs, 0 updates, 10 removals
  - Removing symfony/phpunit-bridge (v3.3.6)
  - Removing sensio/generator-bundle (v3.1.6)
  - Removing friendsofphp/php-cs-fixer (v2.2.5)
  - Removing gecko-packages/gecko-php-unit (v2.1)
  - Removing sebastian/diff (1.4.3)
  - Removing symfony/polyfill-php54 (v1.4.0)
  - Removing symfony/polyfill-php55 (v1.4.0)
  - Removing ircmaxell/password-compat (v1.0.4)
  - Removing symfony/polyfill-php72 (v1.4.0)
  - Removing dama/doctrine-test-bundle (v3.1.0)
Generating optimized autoload files
    Skipped installation of bin bin/doctrine-dbal for package doctrine/dbal: file not found in package
    Skipped installation of bin bin/doctrine for package doctrine/orm: file not found in package
    Skipped installation of bin bin/doctrine.php for package doctrine/orm: file not found in package
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "SensioGeneratorBundle
" from namespace "Sensio\Bundle\GeneratorBundle".
Did you forget a "use" statement for another namespace? in C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\app\AppKernel.php:48
Stack trace:
#0 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php(450): AppKernel-
>registerBundles()
#1 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php(116): Symfony\Co
mponent\HttpKernel\Kernel->initializeBundles()
#2 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php(1
37): Symfony\Component\HttpKernel\Kernel->boot()
#3 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php(1
24): Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands()
#4 C:\Us in C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\app\AppKernel.php on line 48
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the symfony-scripts event terminated with an ex
ception


  [RuntimeException]
  An error occurred when executing the ""cache:clear --no-warmup"" command:
  PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "SensioGeneratorB
  undle" from namespace "Sensio\Bundle\GeneratorBundle".
  Did you forget a "use" statement for another namespace? in C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\app\AppKernel.php:48
  Stack trace:
  #0 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php(450): AppKe
  rnel->registerBundles()
  #1 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php(116): Symfo
  ny\Component\HttpKernel\Kernel->initializeBundles()
  #2 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.
  php(137): Symfony\Component\HttpKernel\Kernel->boot()
  #3 C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.
  php(124): Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands()
  #4 C:\Us in C:\Users\xsunt\PhpstormProjects\xsun\tv\demo\app\AppKernel.php on line 48


install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts]
[--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [
--ignore-platform-reqs] [--] [<packages>]...

composer.json

{
    "name": "symfony/symfony-demo",
    "license": "MIT",
    "type": "project",
    "description": "Symfony Demo Application",
    "autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle/",
            "CodeExplorerBundle\\": "src/CodeExplorerBundle/"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        },
        "files": [
            "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
        ]
    },
    "require": {
        "php": ">=5.5.9",
        "ext-pdo_sqlite": "*",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "doctrine/doctrine-fixtures-bundle": "^2.2",
        "doctrine/orm": "^2.5",
        "erusev/parsedown": "^1.5",
        "ezyang/htmlpurifier": "^4.7",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "symfony/monolog-bundle": "^3.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/symfony": "^3.3",
        "twig/extensions": "^1.5",
        "twig/twig": "^1.28 || ^2.0",
        "white-october/pagerfanta-bundle": "^1.0"
    },
    "require-dev": {
        "dama/doctrine-test-bundle": "^3.0",
        "friendsofphp/php-cs-fixer": "^2.0",
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0"
    },
    "scripts": {
        "symfony-scripts": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-install-cmd": [
            "@symfony-scripts"
        ],
        "post-update-cmd": [
            "@symfony-scripts"
        ]
    },
    "config": {
        "sort-packages": true
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "web",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        }
    }
}

package.json

{
  "devDependencies": {
    "@symfony/webpack-encore": "^0.9.1",
    "bloodhound-js": "^1.2.1",
    "bootstrap-sass": "^3.3.7",
    "bootstrap-tagsinput": "^0.7.1",
    "bootswatch": "^3.3.7",
    "eonasdan-bootstrap-datetimepicker": "^4.17.47",
    "font-awesome": "^4.7.0",
    "highlight.js": "^9.12.0",
    "imports-loader": "^0.7.1",
    "jquery": "^3.2.1",
    "lato-font": "^3.0.0",
    "node-sass": "^4.5.3",
    "sass-loader": "^6.0.5",
    "typeahead.js": "^0.11.1"
  },
  "scripts": {
    "dev-server": "./node_modules/.bin/encore dev-server",
    "dev": "./node_modules/.bin/encore dev",
    "watch": "./node_modules/.bin/encore dev --watch",
    "build": "./node_modules/.bin/encore production"
  }
}

@xsuntel
Copy link

@xsuntel xsuntel commented Mar 18, 2018

Hi guys,

I am testing to check the issue after updating some package following.

$ ./update.sh
#### STEP 1 #### Update Composer
Loading composer repositories with package information
Updating dependencies (including require-dev)

Prefetching 2 packages
  - Downloading (100%)

Package operations: 1 install, 1 update, 0 removals
  - Installing nikic/php-parser (v4.0.0): Loading from cache
  - Updating symfony/maker-bundle (v1.2.0 => v1.3.0): Loading from cache
Writing lock file
Generating autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class

What about running composer global require symfony/thanks && composer thanks now?
This will spread some love by sending a star to the GitHub repositories of your fellow package maintainers.

Executing script cache:clear [OK]
Executing script assets:install --symlink --relative public [OK]
Executing script security-checker security:check
Executing script requirements-checker [OK]

$ cat ./.env
###> symfony/framework-bundle ###
APP_ENV=prod

$ APP_ENV=prod composer install --no-dev --optimize-autoloader
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating optimized autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [OK]
Executing script assets:install --symlink --relative public [OK]
Executing script security-checker security:check [OK]
Executing script requirements-checker [OK]

$ composer install --no-dev --optimize-autoloader
Loading composer repositories with package information
Installing dependencies from lock file
Package operations: 0 installs, 0 updates, 38 removals
  - Removing symfony/profiler-pack (v1.0.3)
.....
  - Removing symfony/process (v4.0.6)
Generating optimized autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 255
!!  PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load
 class "DebugBundle" from namespace "Symfony\Bundle\DebugBundle".
!!  Did you forget a "use" statement for another namespace? in C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0
\src\Kernel.php:32
!!  Stack trace:
!!  #0 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\http-kernel\Kernel.php(403): App\Kernel-
>registerBundles()
!!  #1 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\http-kernel\Kernel.php(122): Symfony\Com
ponent\HttpKernel\Kernel->initializeBundles()
!!  #2 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\framework-bundle\Console\Application.php
(64): Symfony\Component\HttpKernel\Kernel->boot()
!!  #3 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\vendor\symfony\console\Application.php(143): Symfony\Bu
ndle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(
Symfony\Component\Console\Output\ConsoleOutput))
!!  #4 C:\Users\xsunt\PhpstormProjects\xsun\tv\4.0\bin\console(39) in C:\Users\xsunt\PhpstormProjects\xsun
\tv\4.0\src\Kernel.php on line 32

I don't understand what it is different following

$ cat ./.env
###> symfony/framework-bundle ###
APP_ENV=prod

$ APP_ENV=prod composer install --no-dev --optimize-autoloader

$ composer install --no-dev --optimize-autoloader

A minute ago, I have checked that it is normal when I use a command "$ APP_ENV=prod composer install --no-dev --optimize-autoloader" on PhpStorm, but I can't deploy my application to Elastic Beanstalk of AWS.

AWS : EC2

$ cat /opt/elasticbeanstalk/support/envvars
export COMPOSER_HOME="/root"
export PHP_MEMORY_LIMIT="256M"
export PHP_MAX_EXECUTION_TIME="60"
export PHP_DISPLAY_ERRORS="Off"
export PHP_COMPOSER_OPTIONS=""
export PHP_ALLOW_URL_FOPEN="On"
export PHP_ZLIB_OUTPUT_COMPRESSION="On"
export PHP_DOCUMENT_ROOT="/public"
export PHP_DATE_TIMEZONE="UTC"

$ cat /var/log/eb-activity.log
~~
    - Installing symfony/profiler-pack (v1.0.3): Loading from cache
  Generating autoload files
  ocramius/package-versions:  Generating version class...
  ocramius/package-versions: ...done generating version class
  Executing script cache:clear [KO]
   [KO]
  Script cache:clear returned with error code 255
  !!   (Executor::NonZeroExitStatus)

[2018-03-18T07:08:41.337Z] INFO  [4174]  - [Application update app-c89d-180318_160743@77/AppDeployStage0/AppDeployPreHook/10_composer_install.sh] : Activity failed.
[2018-03-18T07:08:41.337Z] INFO  [4174]  - [Application update app-c89d-180318_160743@77/AppDeployStage0/AppDeployPreHook] : Activity failed.
[2018-03-18T07:08:41.337Z] INFO  [4174]  - [Application update app-c89d-180318_160743@77/AppDeployStage0] : Activity failed.
[2018-03-18T07:08:41.338Z] INFO  [4174]  - [Application update app-c89d-180318_160743@77] : Completed activity. Result:

I think it that It is the related issue.

$ cat ./composer.json

"scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd",
            "security-checker security:check": "script",
            "requirements-checker": "script"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },

I am going to check what is different mean online. and I try to search information about "auto-scripts" from composer.json.

@msheakoski
Copy link

@msheakoski msheakoski commented Apr 1, 2018

I think the only way to fix this issue is for Flex to pass down a context of whether the package was installed as a regular dependency or a dev dependency (if Composer makes that information available).

Two possible approaches:

  1. If in a dev context and the bundle specifies "all", it should be converted to ['dev' => true]. Later on if the package is installed as a regular dependency, the entry in bundles.php would be rewritten with the usual ['all' => true]. It's a little bit of magic but it maintains compatibility with the current recipes.

  2. manifest.json could have a "bundles-dev" directive that lives alongside "bundles" which would override "bundles" if the recipe is being run in a dev context. I don't think there would be any other way to change manifest.json with this approach because overloading/changing the "bundles" directive would probably be a BC break for the current users.

@xsuntel
Copy link

@xsuntel xsuntel commented Apr 1, 2018

I am writing to share my information how to deploy my application using SF4.0 to Elastic Beanstalk on AWS.

(1) I have modified a file that It was ".gitignore" following.

###> symfony/framework-bundle ###
/node_modules/
!/public/build/manifest.*
/public/check.php
/tests/
/var/
/var/*
!/var/cache
/var/cache/*
!var/cache/.gitkeep
!/var/data
!/var/log
/var/log/*
!var/log/.gitkeep
!/var/sessions
/var/sessions/*
!var/sessions/.gitkeep
/vendor/

###> friendsofphp/php-cs-fixer ###
.php_cs

###> Elastic Beanstalk Files  ###
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml

###> symfony/phpunit-bridge ###
phpunit.xml.dist
/bin/.phpunit
/bin/phpunit

I think that we need to consider updating some code of the file. because If There was following, I could have a problem that an application can't be deployed.

###> symfony/framework-bundle ###
~
!/public/build/manifest.*

(2) I have been using a command when an application is deployed following.

$ cat ./.env
###> symfony/framework-bundle ###
APP_ENV=prod

$ APP_ENV=prod composer install --no-dev --optimize-autoloader

so, I have updated a shell script file on my laptop

#!/bin/sh

#---------------------------------------------------------------------------------------------------------------------#
# Update Environment : Prod
#---------------------------------------------------------------------------------------------------------------------#
 sed -i "6s/dev/prod/" ./.env
 ENV=$(cat ./.env | sed -n 6p)
 echo "#### STEP 1 #### Check Environment :" $ENV

 FILE=".env.dist"
 if [ -f $FILE ]; then
     rm -f ./.env.dist
     echo "remove a file : .env.dist"
 fi

#---------------------------------------------------------------------------------------------------------------------#
# Install/Update Vendors
#---------------------------------------------------------------------------------------------------------------------#
 echo "#### STEP 2 #### Install Composer"
 APP_ENV=prod composer dump-autoload
 APP_ENV=prod composer install --no-dev --optimize-autoloader

#---------------------------------------------------------------------------------------------------------------------#
# Remove Cac
#---------------------------------------------------------------------------------------------------------------------#
 echo "#### STEP 3 #### Remove Files"
 rm -Rf ./var/cache/*
 chmod 775 ./var/*

#---------------------------------------------------------------------------------------------------------------------#
# Clear Caches
#---------------------------------------------------------------------------------------------------------------------#
 echo "#### STEP 4 #### Clear Caches"
 APP_ENV=prod php bin/console cache:clear --env=prod --no-debug

#---------------------------------------------------------------------------------------------------------------------#
# Update Encore
#---------------------------------------------------------------------------------------------------------------------#
 echo "#### STEP 5 #### Clear Encore"
 yarn run encore production

#---------------------------------------------------------------------------------------------------------------------#
# Git
#---------------------------------------------------------------------------------------------------------------------#
 echo "#### STEP 6 #### Commit"
 git rm -r --cached .
 git add .
 git commit -m "Ryan Lim"

#---------------------------------------------------------------------------------------------------------------------#
# Elastic Beanstalk
#---------------------------------------------------------------------------------------------------------------------#
 echo "#### STEP 7 #### Deploy : Elastic Beanstalk"
 eb deploy prod-env

#---------------------------------------------------------------------------------------------------------------------#
# Update Environment : Dev
#---------------------------------------------------------------------------------------------------------------------#
 sed -i "6s/prod/dev/" ./.env
 ENV=$(cat ./.env | sed -n 6p)
 echo "#### STEP 8 #### Check Environment :" $ENV

 echo "#### STEP 9 #### Install : Packages"
 rm -Rf ./vendor/*
 composer install

#---------------------------------------------------------------------------------------------------------------------#
# Remove Cache Files
#---------------------------------------------------------------------------------------------------------------------#
echo "#### STEP 2 #### Remove Files"
rm -Rf ./var/cache/*
chmod 775 ./var/*

#---------------------------------------------------------------------------------------------------------------------#
# Clear Caches
#---------------------------------------------------------------------------------------------------------------------#
echo "#### STEP 3 #### Clear Caches"
APP_ENV=dev php ./bin/console cache:clear --env=dev

#---------------------------------------------------------------------------------------------------------------------#
# Update Encore
#---------------------------------------------------------------------------------------------------------------------#
echo "#### STEP 4 #### Clear Encore"
yarn run encore dev
#yarn run encore dev --watch

I have been using this way for 3 weeks. as result, I have not found any issues with deploying Apps on AWS.

@msheakoski
Copy link

@msheakoski msheakoski commented Apr 1, 2018

@xsuntel You do not need to use sed. Your error "PHP Fatal error: Uncaught Error: Class 'Symfony\Bundle\TwigBundle\TwigBundle' not found" is related to the main issue being discussed. You have debug-bundle and profiler-pack installed as dev dependencies, in turn, they have a dependency on twig-bundle (and monolog-bundle). Flex adds twig-bundle to your config/bundles.php and tells it to load for all environments (dev/test/prod).

Here is where things break. When you run composer install --no-dev ..., twig-bundle never gets installed because its parent packages (debug and profiler) are dev dependencies and they also are not installed. To fix your issue, run composer require symfony/twig-bundle so that it is installed as a regular dependency and is available in all environments.

@xsuntel
Copy link

@xsuntel xsuntel commented Apr 1, 2018

Hi @msheakoski
Thank you for your replay.

@jvasseur
Copy link

@jvasseur jvasseur commented Apr 2, 2018

@msheakoski the solution of switching 'all' => true to 'dev' => true is not enough you also need to switch the installed config files to beeing dev only or else you will get an error in prod env because you have some config for an unloaded bundle.

And then if you manage to implement that logic it became even more complicated because if you install the profiler pack and then the twig bundle in non-dev environment, you have to revert these changes to make the bundle work as expected.

@ghost
Copy link

@ghost ghost commented Oct 3, 2018

I think we're running into the same issues.

<?php
return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
    FOS\RestBundle\FOSRestBundle::class => ['all' => true],
    JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
    Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
];

As a solution, we are only registring the "all" bundles at this moment and not the dev-bundles anymore in bundles.php. In our case it was the Maker-bundle which causes problems when running composer install --no-dev ... although it was registered with ['dev' => true].

{
"type": "project",
"license": "proprietary",
"require": {
    "php": "^7.1.3",
    "ext-ctype": "*",
    "ext-dom": "*",
    "ext-iconv": "*",
    "ext-json": "*",
    "friendsofsymfony/rest-bundle": "^2.3",
    "jms/serializer-bundle": "^2.4",
    "nelmio/api-doc-bundle": "^3.2",
    "sensio/framework-extra-bundle": "^5.2",
    "symfony/asset": "^4.1",
    "symfony/console": "^4.1",
    "symfony/dotenv": "^4.1",
    "symfony/flex": "^1.0",
    "symfony/framework-bundle": "^4.1",
    "symfony/translation": "^4.1",
    "symfony/monolog-bundle": "^3.3",
    "symfony/orm-pack": "^1.0",
    "symfony/twig-bundle": "^4.1",
    "symfony/yaml": "^4.1"
},
"require-dev": {
    "symfony/browser-kit": "^4.1",
    "symfony/maker-bundle": "^1.5",
    "symfony/phpunit-bridge": "^4.1",
    "symfony/web-server-bundle": "^4.1"
},

By reverting bundles.php after doing a composer require --dev ..., it works as well on development as on production, although it doesn't seem to be a real solution, but more a workaround.
We prefer Symfony being as intelligent to "skip" the ['dev' => true] bundles when trying to run composer in a production environment.

@BernardA
Copy link

@BernardA BernardA commented Nov 28, 2018

I am having same issue as above. I did try to follow some of the suggestions but without success.
That's particularly the case of twig.

Just to recap, when running:

composer install --no-dev --optimize-autoloader

The following error is thrown:

Executing script cache:clear [KO]
Script cache:clear returned with error code 255
Script @auto-scripts was called via post-install-cmd`

I appreciate any help.

My environment:

php bin/console about

 -------------------- ------------------------------------------------------ 
  Symfony                                                                    
 -------------------- ------------------------------------------------------ 
  Version              4.1.8                                                 
  .......                                      
  -------------------- ------------------------------------------------------ 
  Kernel                                                                     
  -------------------- ------------------------------------------------------ 
  Type                 App\Kernel                                            
  Name                 src                                                   
  Environment          prod                                                  
   .......                                                                  
  -------------------- ------------------------------------------------------ 
  PHP                                                                        
  -------------------- ------------------------------------------------------ 
  Version              7.2.1                                                 
   .....                                             
 -------------------- ------------------------------------------------------ 
  Environment (.env)                                                         
 -------------------- ------------------------------------------------------ 
  APP_ENV              prod                                                  
  ......                               
 -------------------- ------------------------------------------------------ 

The bundles.php file:

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['all' => true],
FOS\UserBundle\FOSUserBundle::class => ['all' => true],
Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],
];

And finally, relevant portions of composer.json

    "require": {
    "php": "^7.1.3",
    "ext-iconv": "*",
    "beberlei/DoctrineExtensions": "^1.1",
    "doctrine/doctrine-migrations-bundle": "^1.3",
    "friendsofsymfony/user-bundle": "^2.1",
    "sensio/framework-extra-bundle": "^5.1",
    "stof/doctrine-extensions-bundle": "^1.3",
    "symfony/asset": "^4.0",
    "symfony/console": "^4.0",
    "symfony/debug-pack": "*",
    "symfony/expression-language": "^4.0",
    "symfony/flex": "^1.0",
    "symfony/form": "^4.0",
    "symfony/framework-bundle": "^4.0",
    "symfony/lts": "^4@dev",
    "symfony/monolog-bundle": "^3.1",
    "symfony/orm-pack": "^1.0",
    "symfony/process": "^4.0",
    "symfony/securitcompy-bundle": "^4.0",
    "symfony/serializer-pack": "*",
    "symfony/swiftmailer-bundle": "^3.1",
    "symfony/twig-bundle": "^4.0",
    "symfony/validator": "^4.0",
    "symfony/web-link": "^4.0",
    "symfony/web-server-bundle": "^4.0",
    "symfony/yaml": "^4.0"
},
"require-dev": {
    "symfony/browser-kit": "^4.0",
    "symfony/css-selector": "^4.0",
    "symfony/dotenv": "^4.0",
    "symfony/maker-bundle": "^1.5",
    "symfony/phpunit-bridge": "^4.0",
    "symfony/profiler-pack": "*",
    "symfony/web-server-bundle": "^4.0"
},
@nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Dec 11, 2018

I think there is nothing we can do here - that's just normal composer behavior - nothing related to flex itself.

My recommendation to proper deps management in this situation would be one of:

  • don't add packs to require-dev
  • or unpack packs
@BernardA
Copy link

@BernardA BernardA commented Dec 12, 2018

For future reference, what solved the issue for me was moving symfony/dotenv from require-dev to require, as per the documentation .

  composer remove symfony/dotenv
  composer require symfony/dotenv
@ersin-demirtas
Copy link

@ersin-demirtas ersin-demirtas commented Mar 21, 2019

@BernardA Thank you for your reply, I had .env missing which means the APP_ENV was missing. So Symfony was falling back to dev mode.

Thanks.

@kwisatz
Copy link

@kwisatz kwisatz commented May 26, 2020

What I don't get is how symfony decides (for the bundles.php) what is and what is not a dev or production environment?
The docs say that you can create any new environment (which is what we've done), but when deploying, how does it map the new custom environments to being a dev or non-dev environment which the environment statements in bundles.php would respect?

To illustrate, the relevant parts of composer.json:

"require": {
        "php": "^7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "symfony/console": "5.0.*",
        "symfony/dotenv": "5.0.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "5.0.*",
        "symfony/http-client": "5.0.*",
        "symfony/orm-pack": "^1.0",
        "symfony/yaml": "5.0.*"
    },
    "require-dev": {
        "symfony/debug-pack": "^1.0",
        "symfony/maker-bundle": "^1.18"
    },

and the bundles.php (with a modified setting for TwigBundle):

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['dev' => true],
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];

Now when I deploy with an APP_ENV of somerandomname it will run and fail on cache:clear. So why is somerandomname taken to be a dev environment? What piece of the puzzle am I missing?

@kwisatz
Copy link

@kwisatz kwisatz commented May 26, 2020

Update: There indeed doesn't seem to be such a notion. @weaverryan's comment was confusing in that regard.
In my case, the issue was having a twig.yaml inside the config/packages directory, rather than inside config/packages/dev.

I don't have any knowledge about flex to far, but in this case, where the debug-pack was installed as a --dev dependency, IMHO the twig.yaml file shouldn't have been created in the config/packages directory in the first place.

The next problem will then most likely be the monolog-bundle which also came with debug-pack. What @nicolas-grekas says thus makes sense, unpack your packages so you can move the individual bundles into the respective sections.

In this specific case, monolog was installed as a dependency of the debug-pack using the --dev flag to composer, but the developer doesn't necessarily know that and uses monolog in runtime code. bundles.php will have 'all' => true set for the MonologBundle, but the debug-pack is rightfully so only in require-dev.

I don't know how best to fix this, but I don't think all those related tickets on this behavior warrant to be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants
You can’t perform that action at this time.