Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to load class "WebProfilerBundle" from namespace "Symfony\Bundle\WebProfilerBundle" when deploying on Heroku using development dependencies

We have a Symfony 4.3 web application hosted on Heroku. This is a new setup and we never managed to have the dev mode to work correctly. There are plenty of similar issues online but none fixes the exact symptoms we are facing here.

The project was created with a command line:

composer create-project symfony/website-skeleton appName

Let me clarify that: we do not want to change from "dev" to "prod"

We need to be able to use the application in dev mode in order to take advantage of error debugging for PHP as per the article "How to Customize Error Pages" for Symfony 4.3 here

$ php bin/console about
 -------------------- ----------------------------------------------------
  Symfony
 -------------------- ----------------------------------------------------
  Version              4.3.5
  End of maintenance   01/2020
  End of life          07/2020
 -------------------- ----------------------------------------------------
  Kernel
 -------------------- ----------------------------------------------------
  Type                 App\Kernel
  Environment          dev
  Debug                true
  Charset              UTF-8
  Cache directory      ./var/cache/dev (12.1 MiB)
  Log directory        ./var/log (13 KiB)
 -------------------- ----------------------------------------------------
  PHP
 -------------------- ----------------------------------------------------
  Version              7.3.10
  Architecture         64 bits
  Intl locale          n/a
  Timezone             Europe/Berlin (2019-10-28T15:48:05+01:00)
  OPcache              false
  APCu                 false
  Xdebug               false
 -------------------- ----------------------------------------------------
  Environment (.env)
 -------------------- ----------------------------------------------------
  APP_ENV              dev
 *just a few removed before posting for privacy*
 -------------------- ----------------------------------------------------

If we change from 'dev' to 'prod' environment the application works but we cannot see the errors the way we wish to. We could use the command below (but this is not the point we are trying to solve here) in order to use "Symfony Var Dumper Server". We do not want to use this method.

./bin/console server:dump

It seems that we might be missing a dependency but composer update and composer install and composer dump-autoload did not solve anything.

Also note the message "Did you forget a "use" statement for another namespace? in /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/src/Kernel.php:23" - Is it possible that the WebProfilerBundle is broken in the master repository?

To further help understand the situation, below is the composer.json file

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "phpmailer/phpmailer": "^6.1",
        "sensio/framework-extra-bundle": "^5.5",
        "symfony/asset": "4.3.*",
        "symfony/console": "4.3.*",
        "symfony/dotenv": "4.3.*",
        "symfony/expression-language": "4.3.*",
        "symfony/flex": "^1.3.1",
        "symfony/form": "4.3.*",
        "symfony/framework-bundle": "4.3.*",
        "symfony/http-client": "4.3.*",
        "symfony/intl": "4.3.*",
        "symfony/mailer": "4.3.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/orm-pack": "^1.0",
        "symfony/process": "4.3.*",
        "symfony/security-bundle": "4.3.*",
        "symfony/serializer-pack": "*",
        "symfony/swiftmailer-bundle": "^3.1",
        "symfony/translation": "4.3.*",
        "symfony/twig-bundle": "4.3.*",
        "symfony/twig-pack": "^1.0",
        "symfony/validator": "4.3.*",
        "symfony/web-link": "4.3.*",
        "symfony/webpack-encore-bundle": "^1.7",
        "symfony/yaml": "4.3.*"
    },
    "require-dev": {
        "symfony/debug-bundle": "4.3.*",
        "symfony/debug-pack": "*",
        "symfony/maker-bundle": "^1.14",
        "symfony/profiler-pack": "^1.0",
        "symfony/test-pack": "*",
        "symfony/web-profiler-bundle": "4.3.*",
        "symfony/web-server-bundle": "4.3.*"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "4.3.*"
        }
    }
}

Our obstacle: trying to deploy to heroku using dev mode.

git push heroku master

which fails with the following message:

remote:        Executing script cache:clear [KO]
remote:         [KO]
remote:        Script cache:clear returned with error code 255
remote:        !!  PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "WebProfilerBundle" from namespace "Symfony\Bundle\WebProfilerBundle".
remote:        !!  Did you forget a "use" statement for another namespace? in /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/src/Kernel.php:23
remote:        !!  Stack trace:
remote:        !!  #0 /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/vendor/symfony/http-kernel/Kernel.php(429): App\Kernel->registerBundles()
remote:        !!  #1 /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/vendor/symfony/http-kernel/Kernel.php(130): Symfony\Component\HttpKernel\Kernel->initializeBundles()
remote:        !!  #2 /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/vendor/symfony/framework-bundle/Console/Application.php(159): Symfony\Component\HttpKernel\Kernel->boot()
remote:        !!  #3 /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/vendor/symfony/framework-bundle/Console/Application.php(65): Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands()
remote:        !!  #4 /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/vendor/symfony/console/Application.php(149): Symfony\Bundle\FrameworkBundle\Cons in /tmp/build_a45354eb0ee7b20dd7ec870ed4fb1980/src/Kernel.php
on line 23
remote:        !!
remote:        Script @auto-scripts was called via post-install-cmd
remote:  !     WARNING: There was a class not found error in your code
remote:
remote:  !     ERROR: Dependency installation failed!
remote:  !
remote:  !     The 'composer install' process failed with an error. The cause
remote:  !     may be the download or installation of packages, or a pre- or
remote:  !     post-install hook (e.g. a 'post-install-cmd' item in 'scripts')
remote:  !     in your 'composer.json'.
remote:  !
remote:  !     Typical error cases are out-of-date or missing parts of code,
remote:  !     timeouts when making external connections, or memory limits.
remote:  !
remote:  !     Check the above error output closely to determine the cause of
remote:  !     the problem, ensure the code you're pushing is functioning
remote:  !     properly, and that all local changes are committed correctly.
remote:  !
remote:  !     For more information on builds for PHP on Heroku, refer to
remote:  !     https://devcenter.heroku.com/articles/php-support
remote:  !
remote:  !     REMINDER: the following warnings were emitted during the build;
remote:  !     check the details above, as they may be related to this error:
remote:  !     - There was a class not found error in your code
remote:
remote:  !     Push rejected, failed to compile PHP app.
remote:
remote:  !     Push failed

We currently do not know how to install or verify that bundles are indeed present and operational for Symfony 4.3 - ideas in this direction might help but not only.

php bin/console config:dump-reference

Available registered bundles with their extension alias if available
====================================================================

 ---------------------------- ------------------------
  Bundle name                  Extension alias
 ---------------------------- ------------------------
  DebugBundle                  debug
  DoctrineBundle               doctrine
  DoctrineCacheBundle          doctrine_cache
  DoctrineMigrationsBundle     doctrine_migrations
  FrameworkBundle              framework
  MakerBundle                  maker
  MonologBundle                monolog
  SecurityBundle               security
  SensioFrameworkExtraBundle   sensio_framework_extra
  SwiftmailerBundle            swiftmailer
  TwigBundle                   twig
  TwigExtraBundle              twig_extra
  WebProfilerBundle            web_profiler
  WebServerBundle              web_server
  WebpackEncoreBundle          webpack_encore
 ---------------------------- ------------------------

 // Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g.
 // config:dump-reference FrameworkBundle)
 //
 // For dumping a specific option, add its path as the second argument of this command. (e.g.
 // config:dump-reference FrameworkBundle profiler.matcher to dump the
 // framework.profiler.matcher configuration)
like image 757
dankilev Avatar asked Oct 28 '19 15:10

dankilev


3 Answers

Heroku will not install dev dependencies in your dyno. This is documented here: Build behaviour.

Specifically, heroku will run:

composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction

The theory goes that heroku is for production like code, so development dependencies do not belong there. Usually, these dependencies would only be installed on the developers' machines, not on any deployed application instance.

If you really need this on deployed code, I guess you should admit this are not development dependencies but regular dependencies, and move them from require-dev to require.

like image 117
yivi Avatar answered Oct 21 '22 06:10

yivi


Go into composer.json

Change this:

    "post-install-cmd": [
        "@auto-scripts"
    ],

To this:

    "post-install-cmd": [
    ],

Then you don't run the post install scripts.

like image 42
etusm Avatar answered Oct 21 '22 04:10

etusm


As Sylvery said, Adding APP_ENV = prod in config vars fixed it for me.

heroku config:set APP_ENV=prod

or using heroku interface :

  • Settings > Config Vars > button Reveal Vars
  • Add "APP_ENV" in key and "prod" in value
like image 2
Reynadan Avatar answered Oct 21 '22 05:10

Reynadan