Twig: Error Cannot redeclare class Twig_Environment

Created on 1 Jun 2017  Â·  30Comments  Â·  Source: twigphp/Twig

I updated vendors, and only update version twig.

 - Removing twig/twig (1.x-dev 460712d)
  - Installing twig/twig (1.x-dev 04ef7a1)
    Loading from cache

But now I get the following error:

FatalErrorException in Environment.php line 17: Error: Cannot redeclare class Twig_Environment

I cleared cache... but the error continue... I did a compare two commits... but I dont see nothing unusual...

https://github.com/twigphp/Twig/compare/460712d...04ef7a1#diff-d4416d290253c72cec40c63f54354b16

any idea ?

This is my composer

    "require" : {
        "php" : "^5.6|^7.0",
        "ext-mcrypt" : "*",
        "twig/extensions" : "^1.4",
        "sensio/framework-extra-bundle" : "~3.0",
        "sensio/generator-bundle" : "~3.0",
        "symfony/assetic-bundle": "2.8.1",
        "incenteev/composer-parameter-handler" : "~2.0",
        "twbs/bootstrap" : "dev-master",
        "knplabs/knp-paginator-bundle" : "dev-master",
        "craue/formflow-bundle" : "2.1.8",
        "shtumi/useful-bundle" : "dev-master",
        "sonata-project/formatter-bundle" : "2.3.2",
        "sonata-project/doctrine-extensions" : "1.0.2",
        "frequence-web/contact-bundle" : "*",
        "jms/serializer-bundle" : "^1.1",
        "sylius/sylius": "1.0.0-alpha.2",
        "presta/sitemap-bundle" : "~1.3@dev",
        "beelab/recaptcha2-bundle" : "1.2.1",
        "dizda/cloud-backup-bundle" : "^3.2",
        "aws/aws-sdk-php" : "2.8.24",
        "lexik/maintenance-bundle" : "2.1.1",
        "hwi/oauth-bundle" : "0.3.x",
        "friendsofsymfony/rest-bundle" : "1.8.x",
        "crevillo/payum-redsys" : "^1.0",
        "payum/paypal-express-checkout-nvp" : "^1.3",
        "php-http/guzzle6-adapter" : "^1.1",
        "oyejorge/less.php": "v1.7.0.10",
        "doctrine/doctrine-cache-bundle": "^1.3",
        "predis/predis": "^1.1",
        "snc/redis-bundle": "^2.0",
        "knplabs/knp-gaufrette-bundle": "^0.3.0",
        "liip/imagine-bundle": "1.7.2",
        "phpdocumentor/reflection-docblock": "2.0.4"
    },
    "require-dev" : {
        "raulfraile/ladybug-bundle" : "~1.0",
        "behat/behat" : "~3.0",
        "behat/symfony2-extension" : "~2.0@dev",
        "behat/mink-extension" : "~2.0@dev",
        "behat/mink-browserkit-driver" : "~1.2@dev",
        "behat/mink-selenium2-driver" : "@dev",
        "behat/mink" : "~1.6@dev",
        "fzaninotto/faker" : "~1.2",
        "phpunit/phpunit" : "<=5.7",
        "visithor/visithor-bundle" : "^0.1.0@dev",
        "michaelmoussa/doctrine-qbmocker": "^0.12.0"
    },

Most helpful comment

Composer just updated me to 2.4.0 which I see just got released 20 minutes ago, and immediately received the error as well. I had 2.3.2 before

Here's a stack trace:

in Environment.php line 17
in classes.php line 5503
in ClassCollectionLoader.php line 93
at ClassCollectionLoader::load() in Kernel.php line 384
at AppKernel->doLoadClassCache() in Kernel.php line 109
at AppKernel->boot() in Kernel.php line 165
at AppKernel->handle() in app_dev.php line 28
at {main}() in app_dev.php line 0

All 30 comments

Please try with the latest version of the branch (a bugfix was merged in 1.x after the 04ef7a1 commit) ? and make sure you clear your OPCache cache

I updated and I removed cache

  - Removing twig/twig (1.x-dev 460712d)
  - Installing twig/twig (1.x-dev f6e0000)
    Downloading: 100% 

And the problem continue:

FatalErrorException in Environment.php line 17: Error: Cannot redeclare class Twig_Environment

:(

Everything works correctly if fixed the commit.

"twig/twig": "1.x-dev#460712d"

can you give the stack trace of the exception ?

I dont have profiler.

I only see the following error:

error_twig

And in the logs... only I see:

{"log":"[Thu Jun 01 15:09:33.352470 2017] [:error] [pid 2125] [client ip:48272] PHP Fatal error: Cannot redeclare class Twig_Environment in /var/www/html/vendor/twig/twig/lib/Twig/Environment.php on line 17\n","stream":"stderr","time":"2017-06-01T15:09:33.352595325Z"}

Hey

I've a same problem.
I fix it when I force twig to 1.33.2 in my composer.json

The 1.x.dev is not a release version, you can have bugs.

BR

/cc @nicolas-grekas

@GuigZ- or @Chrysweel, in src/Environment.php, can you try replacing the "require" by "require_once" please? Any change?
Would you be able to provide a reproducer?

And which php version?

@GuigZ- or @Chrysweel I tried reproducing the issue but I can't sorry. If you can't provide a reproducer, I'll need your help to do "remote" debugging :)
(waiting for answers to previous questions, I'd like to have your OS/env also please).
If you can debug the issue and find the reason, that'd be awesome.

I've seen a similar error in the past when a client had more than one opcache extension enabled at the same time (e.g. PHP opcache + XCache).

Can someone tell if #2497 makes a difference?

Without feedback, and while I wondered about this without finding any way this can happen, I'm inclined to think this is related to a badly resetted OPcache.
Which means we can close this IMHO and consider everything is fine.
Please reopen or resubmit if you think otherwise - and please provide insights while doing so! (see above)

@nicolas-grekas I updated vendors again, now my commit is:

  - Removing twig/twig (1.x-dev 460712d)
  - Installing twig/twig (1.x-dev 3c132d0)
    Downloading: 100%     

And I changed require -> require_once:

<?php

namespace Twig;

require_once __DIR__.'/../lib/Twig/Environment.php';

if (\false) {
    class Environment extends \Twig_Environment
    {
    }
}

My php version:

>php -v
PHP 5.6.28 (cli) (built: Dec  6 2016 22:18:26) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

I get the same error. I am in environment app_dev.php in my project symfony.
And I did

sudo rm -r app/cache/*

@Chrysweel thanks for the feedback. Are you using Apache or PHP-FPM? Can you try restarting it?
Are you able to provide a reproducer, like a series of command I could run to reproduce?
Could you provide a backtrace for this failure also?
Can you reproduce the issue if you disable OPCache?

Another question: how does it behave if you replace the "require" line in src/Environment.php by this?
class_alias('Twig_Environment', 'Twig\Environment', true);
If you have an error, is it exactly the same message?

Composer just updated me to 2.4.0 which I see just got released 20 minutes ago, and immediately received the error as well. I had 2.3.2 before

Here's a stack trace:

in Environment.php line 17
in classes.php line 5503
in ClassCollectionLoader.php line 93
at ClassCollectionLoader::load() in Kernel.php line 384
at AppKernel->doLoadClassCache() in Kernel.php line 109
at AppKernel->boot() in Kernel.php line 165
at AppKernel->handle() in app_dev.php line 28
at {main}() in app_dev.php line 0

Similar to @richard4339 it just updated from 1.33.2 to 1.34.0 in a composer update and the same 500 error occurred. I reverted back to 1.33.2 and it has resolved the issue. Using Symfony 3.1 for reference.

@richard4339 thanks for the report, can you try the advice just before?

Like this ?

<?php

namespace Twig;

class_alias('Twig_Environment', 'Twig\Environment', true);

if (\false) {
    class Environment extends \Twig_Environment
    {
    }
}

Nothing change, same error (Using symfony 2.8)

I'm failing to find the require line in Environment.php to change @nicolas-grekas

@richard4339 in the "src" folder of twig (not in "lib")
@konandrum yes like that. can you post the backtrace again, with the leading directory please?
is the error message exactly the same?
how does it behave if you enclose this class_alias (or the previous "require") in a class_exists check?

Another idea, might be the one:
in the lib folder, there are a few "require" also:

lib/Twig/BaseNodeVisitor.php
lib/Twig/TokenParserInterface.php
lib/Twig/Extension.php
lib/Twig/Node.php
lib/Twig/Compiler.php
lib/Twig/ExtensionInterface.php
lib/Twig/Test/NodeTestCase.php
lib/Twig/Parser.php
lib/Twig/Extension/Profiler.php
lib/Twig/NodeVisitorInterface.php
lib/Twig/SimpleFunction.php
lib/Twig/SimpleTest.php
lib/Twig/Error.php
lib/Twig/Environment.php
lib/Twig/Node/Expression/Call.php
lib/Twig/SimpleFilter.php

can you try replacing them by a class_exists check for the corresponding class?

If it works, I invite you to open a PR :) (against branch 1.x)

I made the PR :)
See #2501

2.4.1 works beautifully @nicolas-grekas!!! Thanks!

cool, thanks for confirming!

Hey,

Your fix does not work. I try the last version (v1.34.2) and i've this error :

PHP message: PHP Fatal error: Cannot redeclare class Twig_Node in app/cache/recette/classes.php on line xxxx

capture d ecran 2017-06-07 a 11 31 49

app/cache ...

Clear your cache :)

Not fix the problem :)

I clear all cache ;). I create an PR to fix the problem. #2506

Was this page helpful?
0 / 5 - 0 ratings