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

Error Cannot redeclare class Twig_Environment #2495

Closed
Chrysweel opened this issue Jun 1, 2017 · 30 comments
Closed

Error Cannot redeclare class Twig_Environment #2495

Chrysweel opened this issue Jun 1, 2017 · 30 comments

Comments

@Chrysweel
Copy link
Contributor

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...

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"
	},
@stof
Copy link
Member

stof commented Jun 1, 2017

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

@Chrysweel
Copy link
Contributor Author

Chrysweel commented Jun 1, 2017

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

:(

@Chrysweel
Copy link
Contributor Author

Everything works correctly if fixed the commit.

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

@stof
Copy link
Member

stof commented Jun 1, 2017

can you give the stack trace of the exception ?

@Chrysweel
Copy link
Contributor Author

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"}

@baiiko
Copy link

baiiko commented Jun 2, 2017

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

@fabpot
Copy link
Contributor

fabpot commented Jun 2, 2017

/cc @nicolas-grekas

@nicolas-grekas
Copy link
Contributor

@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?

@nicolas-grekas
Copy link
Contributor

And which php version?

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Jun 2, 2017

@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.

@jstanden
Copy link

jstanden commented Jun 2, 2017

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).

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Jun 2, 2017

Can someone tell if #2497 makes a difference?

@nicolas-grekas
Copy link
Contributor

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)

@Chrysweel
Copy link
Contributor Author

Chrysweel commented Jun 5, 2017

@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/*

@nicolas-grekas
Copy link
Contributor

@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?

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Jun 5, 2017

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?

@richard4339
Copy link

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

@TWithers
Copy link

TWithers commented Jun 5, 2017

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.

@nicolas-grekas
Copy link
Contributor

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

@konandrum
Copy link

konandrum commented Jun 5, 2017

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)

@richard4339
Copy link

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

@nicolas-grekas
Copy link
Contributor

@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?

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Jun 5, 2017

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

@nicolas-grekas
Copy link
Contributor

I made the PR :)
See #2501

@fabpot fabpot closed this as completed in 23e64af Jun 5, 2017
@richard4339
Copy link

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

@nicolas-grekas
Copy link
Contributor

cool, thanks for confirming!

@baiiko
Copy link

baiiko commented Jun 6, 2017

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

@baiiko
Copy link

baiiko commented Jun 7, 2017

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

@konandrum
Copy link

app/cache ...

Clear your cache :)

@baiiko
Copy link

baiiko commented Jun 7, 2017

Not fix the problem :)

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

jorissteyn pushed a commit to SURFnet/sp-dashboard that referenced this issue Sep 26, 2017
See: twigphp/Twig#2495

This problem was fixed in twig 1.34.4, so configuring that specific
version as minimum version in composer.json.
jorissteyn pushed a commit to SURFnet/sp-dashboard that referenced this issue Sep 26, 2017
See: twigphp/Twig#2495

This problem was fixed in twig 1.34.4, so configuring that specific
version as minimum version in composer.json.
jorissteyn pushed a commit to SURFnet/sp-dashboard that referenced this issue Sep 26, 2017
See: twigphp/Twig#2495

This problem was fixed in twig 1.34.4, so configuring that specific
version as minimum version in composer.json.
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

9 participants