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 with phar "cannot redeclare class composer\composer" #264

Closed
Palleas opened this issue Jan 27, 2012 · 59 comments
Closed

Error with phar "cannot redeclare class composer\composer" #264

Palleas opened this issue Jan 27, 2012 · 59 comments

Comments

@Palleas
Copy link

@Palleas Palleas commented Jan 27, 2012

I downloaded a new composer.phar and created a new composer.json, this what I get when I run "php composer.phar install" :

PHP Fatal error:  require(): Cannot redeclare class composer\composer in phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/vendor/.composer/ClassLoader.php on line 2
PHP Stack trace:
PHP   1. {main}() /Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar:0
PHP   2. require() /Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar:15
PHP   3. Composer\Console\Application->run() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/bin/composer:14
PHP   4. Symfony\Component\Console\Application->run() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Console/Application.php:2
PHP   5. Composer\Console\Application->doRun() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/vendor/symfony/console/Symfony/Component/Console/Application.php:2
PHP   6. Symfony\Component\Console\Application->doRun() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Console/Application.php:2
PHP   7. Symfony\Component\Console\Command\Command->run() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/vendor/symfony/console/Symfony/Component/Console/Application.php:2
PHP   8. Composer\Command\InstallCommand->execute() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:2
PHP   9. Composer\Command\InstallCommand->install() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Command/InstallCommand.php:10
PHP  10. Composer\Command\Command->getComposer() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Command/InstallCommand.php:10
PHP  11. Composer\Console\Application->getComposer() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Command/Command.php:2
PHP  12. Composer\Factory::create() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Console/Application.php:2
PHP  13. Composer\Factory->createComposer() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Factory.php:2
PHP  14. Composer\Package\Loader\RootPackageLoader->load() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Factory.php:2
PHP  15. Composer\Package\Loader\ArrayLoader->load() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Package/Loader/RootPackageLoader.php:2
PHP  16. Composer\Package\Loader\ArrayLoader->loadLinksFromConfig() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Package/Loader/ArrayLoader.php:2
PHP  17. Composer\Package\Version\VersionParser->parseConstraints() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Package/Loader/ArrayLoader.php:2
PHP  18. Composer\Package\Version\VersionParser->parseConstraint() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Package/Version/VersionParser.php:2
PHP  19. Composer\Autoload\ClassLoader->loadClass() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/src/Composer/Package/Version/VersionParser.php:0
PHP  20. require() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/vendor/.composer/ClassLoader.php:2
PHP  21. Composer\Autoload\ClassLoader->loadClass() phar:///Users/romainpouclet/Sites/KNP/knpuniversity_api/composer.phar/vendor/.composer/ClassLoader.php:0
@Seldaek
Copy link
Member

@Seldaek Seldaek commented Jan 28, 2012

Can you give more details? Maybe show the composer file, because I can't reproduce this here.

@Palleas
Copy link
Author

@Palleas Palleas commented Jan 28, 2012

Sure, here is my composer.json (it worked on a projet I worked on a couple of weeks ago) :

{
  "require": {
    "silex/silex": "1.*",
    "behat/behat": ">=2.2.2",
    "behat/mink": ">=1.3.2",
    "twig/twig": "1.6.0-dev",
    "symfony/twig-bridge": "2.1.0-dev",
    "symfony/form": "2.1.0-dev"
  },

  "config": {
    "bin-dir": "bin/"
  },

  "repositories": {
    "behat/mink-deps": { "composer": { "url": "behat.org" } }
  }
}
@Seldaek
Copy link
Member

@Seldaek Seldaek commented Jan 28, 2012

Works for me.. Not sure what is happening on your machine. Can you try
using composer from source see if you get the same error?

@Palleas
Copy link
Author

@Palleas Palleas commented Jan 28, 2012

I will as soon as I get home. This is really weird.

@Palleas
Copy link
Author

@Palleas Palleas commented Jan 30, 2012

From the source it worked just as expected (using the bin/composer in composer folder, right?). I tried re-downloading a phar from getcomposer.org and I got the same error (used wget, curl and even directly from my browser, just in case).
I tried re-building it on my machine and it still crashes...
Could it be something wrong in my php.ini ?

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Jan 30, 2012

I really don't see what could cause this, nor why the classname is lowercased. If you can't provide more info I am afraid I can't help on this one since I can not reproduce it.

@tothimre-zz
Copy link

@tothimre-zz tothimre-zz commented Feb 8, 2012

At one of my machines this happens as well, but not on the other, both has Ubuntu.

@Palleas
Copy link
Author

@Palleas Palleas commented Feb 8, 2012

Glad to see I'm not crazy :)

@tothimre-zz
Copy link

@tothimre-zz tothimre-zz commented Feb 8, 2012

But I found a workaround for the problem just now. cloned the composer from the github run the bin/composer install, it gave some instructions what to do so i followed it .. It has downloaded the composer.phar to resolve ti's own dependencies, it ran ;) (but in my project teh sema file does not run). And from my project root folder I run the aforementioned path_to_the_cloned composer_source/bin/composer install. and it works. It is weird but at least a workaround.

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Feb 8, 2012

I'm glad nobody is crazy, but I still don't understand how it can happen, and I can't reproduce it :/ Maybe try comparing php versions between working and failing installs, you can also try to run this perhaps it gives us a hint:

php -r '$e = new ReflectionExtension("phar"); var_dump($e->getVersion());'
@tothimre-zz
Copy link

@tothimre-zz tothimre-zz commented Feb 8, 2012

On the problematic machine ti says "2.0.1"

@Palleas
Copy link
Author

@Palleas Palleas commented Feb 8, 2012

romainpouclet@syrdahar ~ % php -r '$e = new ReflectionExtension("phar"); var_dump($e->getVersion());'
string(5) "2.0.1"

A colleague of mine told my this (he had a similar issue but not with composer) :

when the autoloader is PSR0 compliant and you miss a \ front of a class in a method signature then the autoloader does not know this class and loads ... twice the same file

Maybe it can help?

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Feb 8, 2012

OK, same here, so I guess that's not it. Php version? aptitude show php5? php -v?

@Palleas
Copy link
Author

@Palleas Palleas commented Feb 8, 2012

PHP 5.3.8 (cli) (built: Sep 14 2011 17:09:48)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with Xdebug v2.1.2, Copyright (c) 2002-2011, by Derick Rethans

@tothimre-zz
Copy link

@tothimre-zz tothimre-zz commented Feb 8, 2012

php -v
PHP 5.3.6-13ubuntu3.3 with Suhosin-Patch (cli) (built: Dec 13 2011 18:18:37)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Feb 8, 2012

@Palleas: what you say makes no sense IMO, type hints, like instanceof, do not trigger the autoloader, they merely check against the object that is passed. And the autoloader always gets a fully qualified class name without "" prefix anyway. The weird thing here is the fact it's lowercased.

@Palleas
Copy link
Author

@Palleas Palleas commented Feb 8, 2012

I just c/p what he told me :)

@FrancoisConstant
Copy link

@FrancoisConstant FrancoisConstant commented Feb 14, 2012

I just had a similar issue so I went back to the previous version.

If that can help:

I'm using MAMP2PRO.
PHP version:
php -v
PHP 5.3.6 (cli) (built: Sep 15 2011 11:22:25)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with XCache v1.3.1, Copyright (c) 2005-2010, by mOo

Fatal error: Cannot redeclare class Composer\Autoload\ClassLoader in phar:///_my-project-path_/src/lib/vendor/mink.phar/vendor/.composer/autoload.php on line 2

@stof
Copy link
Contributor

@stof stof commented Feb 14, 2012

you are not using the latest version. The autoloader is not defined in autoload.php directly anymore but conditionally included in it to avoid this issue.

@sliver
Copy link

@sliver sliver commented Mar 2, 2012

Check the APC setting in php.ini. See if apc.enable_cli=1.

@fabpot
Copy link
Contributor

@fabpot fabpot commented Mar 8, 2012

I can confirm that changing apc.enable_cli=1 to apc.enable_cli=0 fixes the issue.

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Mar 8, 2012

Good to know, but still weird. It'd be nice to have a reproduce case so we can report a bug upstream. Do you have stat=0 too or is it just screwing up real bad?

@sliver
Copy link

@sliver sliver commented Mar 9, 2012

Turn on apc.enable_cli=1 and run php composer.phar install can reproduce this case.
After turn off apc.enable_cli=0, everything works again.

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Mar 9, 2012

Yeah the problem is that it doesn't always fail with apc enabled. For example @fabpot was working with it just fine for a while, then updated the phar and then it started failing. I wonder if the problem is just when you have a phar, and then modify it and start using the new one and it still has some of the old one in cache.

@fabpot please keep the broken phar you have somewhere safe so we can one day debug this maybe. I can't reproduce here even with enable_cli=1.

@jhallbachner
Copy link

@jhallbachner jhallbachner commented Mar 15, 2012

I've encountered a similar problem. It does seem like a problem with caching an old version, but I haven't been able to reproduce it on a clean VM.

PHP 5.3.6-13ubuntu3.6 with Suhosin-Patch (cli) (built: Feb 11 2012 03:26:01)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with Xdebug v2.1.3, Copyright (c) 2002-2012, by Derick Rethans

Fatal error: Class 'Symfony\Component\Console\Input\Input' not found in phar:///home/jhallbachner/composer/composer.phar/vendor/symfony/console/Symfony/Component/Console/Input/ArgvInput.php on line 2

It still happens when apc.enabled_cli = 1, doesn't when APC is off. It also persists after I clear the APC cache in CLI. If I extract the phar contents it doesn't happen with APC on or off.

The kicker: the problem goes away if I rename the .phar to something else: composer-2.phar will work fine. And I get the same results (error with composer.phar name, working with different names) using both Composer versions 59449fe and e50fbf3 (the two copies of the phar I had available to test.)

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Mar 16, 2012

Quite weird, but then again the php docs seem to acknowledge that APC should not really be enabled on the CLI. It seems to be a phar+apc bug, see https://bugs.php.net/bug.php?id=59398 https://bugs.php.net/bug.php?id=59829 https://bugs.php.net/bug.php?id=59907 as well.

@fixe
Copy link
Contributor

@fixe fixe commented Jun 11, 2012

I was having this issue as well:

PHP Fatal error: Cannot redeclare class Composer\Repository\InstalledRepositoryInterface

Changing apc.enable_cli=1 to apc.enable_cli=0 fixed the problem

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Jun 11, 2012

Yup, this is taken care of by the installer now, so closing this.

@Seldaek Seldaek closed this Jun 11, 2012
@ghost
Copy link

@ghost ghost commented Jul 9, 2012

how to fix it in a environnement where php.ini is not on dev's control ?

i've tried php_flag apc.enable_cli 1 in .htaccess, same result.

Fatal error: include(): Cannot redeclare class composer\command\showcommand in phar:///data_path/composer.phar/vendor/composer/ClassLoader.php on line 150

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Dec 19, 2012

Yes there are valid reasons to need it for sure, but unfortunately we can't do anything to work around it, so if you need it, you need to make sure it's either only on when you run your code, or only off when running composer.

@danez
Copy link

@danez danez commented Apr 12, 2013

Just for Info, I had the same problem with symfony process and composer was NOT warning about apc. Although enabled.

@relaxnow
Copy link

@relaxnow relaxnow commented Apr 24, 2014

Just ran into this issue with the latest build of Composer, maybe the Phar of Composer could detect and warn against running with (or better yet refuse to run with) apc.enable_cli=1?

@stof
Copy link
Contributor

@stof stof commented Apr 24, 2014

well, the installer script, which is responsible for checking system requirements, is already aware of this

@relaxnow
Copy link

@relaxnow relaxnow commented Apr 24, 2014

I had already installed composer and later turned on the setting to debug an issue with APC. Being as it is off by default I would assume that that is the normal way it gets turned on.
Someone starts work on a project, installs composer, later finds has issues with APC, turns on the setting, does a composer update and things break.

The default apc.ini even specifies that as the likely use-case:

; Setting this enables APC for the CLI version of PHP (Mostly for testing and debugging).

But then again, Googling the error finds this issue rather quickly and it's an edge case with a extension that is deprecated anyway.
Just thought that a warning or block might help the next dev who runs into this.

@Seldaek
Copy link
Member

@Seldaek Seldaek commented Apr 24, 2014

The problem is we can not detect it from within the phar since having enable_cli on just crashes the phar before it even executes. That's why it's in the installer, and yes shit happens sometimes when people change their settings, it's unfortunate.

@stof
Copy link
Contributor

@stof stof commented Apr 24, 2014

And this is why the composer troubleshooting documentation recommends to check the requirements again when you face an issue: https://getcomposer.org/doc/articles/troubleshooting.md

@relaxnow
Copy link

@relaxnow relaxnow commented Apr 24, 2014

Ah okay, it only segfaults for me when writing out the autoloader, but yeah, an edge case that will go away soon enough.

@lieut-data
Copy link

@lieut-data lieut-data commented Jun 6, 2014

For those looking to use APC purely for the user cache, I've found that setting apc.cache_by_default=0 or apc.max_file_size=0 also works around this problem.

@AlexeyKupershtokh
Copy link
Contributor

@AlexeyKupershtokh AlexeyKupershtokh commented Sep 25, 2014

The problem is we can not detect it from within the phar since having enable_cli on just crashes the phar before it even executes.

I faced the problem today and googled this issue.
It seems you are not right, at least in my environment.
Just calling composer works ok:

wicked@wicked-desktop:~/project $ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 69afedb49c48239e4bb7219ec1bc2e1ee5b83595 2014-09-24 18:19:25

Usage:
  [options] command [arguments]
...

But calling composer install fails:

wicked@wicked-desktop:~/project $ composer install
PHP Fatal error:  include(): Cannot redeclare class composer\command\licensescommand in phar:///usr/local/bin/composer/vendor/composer/ClassLoader.php on line 269

Fatal error: include(): Cannot redeclare class composer\command\licensescommand in phar:///usr/local/bin/composer/vendor/composer/ClassLoader.php on line 269
@Seldaek
Copy link
Member

@Seldaek Seldaek commented Sep 25, 2014

@AlexeyKupershtokh I think it depends on the exact conditions and what's in the apc cache. I guess it's less and less likely to be a problem though given apc is on its way out.

@lieut-data
Copy link

@lieut-data lieut-data commented Oct 1, 2014

This doesn't seem to be an issue with APCu, the user-cache-only replacement for APC. Perhaps this warning can be isolated to installations of PHP (<=5.4) with the APC extension actually enabled?

@stof
Copy link
Contributor

@stof stof commented Oct 1, 2014

@Seldaek I suggest changing it to extension_loaded('apc') && !extension_loaded('apcu')

Seldaek added a commit that referenced this issue Oct 2, 2014
@Seldaek
Copy link
Member

@Seldaek Seldaek commented Oct 2, 2014

I adjusted the check in Diagnose and the installer to exclude apcu

acoulton added a commit to kohana/cache that referenced this issue Nov 6, 2014
There is an intermittent fatal error in composer if the APC opcode 
cache is enabled, per composer/composer#264 - this config change
prevents opcode caching but allows access to the user cache.
acoulton added a commit to acoulton/composer that referenced this issue Dec 3, 2014
Update the phar stub to prevent APC attempting to run the composer classes through 
the opcode cache on supported APC versions to avoid the random fatal "duplicate
class" errors at runtime as reported in composer#264.

Prior to APC version 3.0.12 this was a PHP_INI_SYSTEM directive, so output a 
warning instead.
@acoulton
Copy link
Contributor

@acoulton acoulton commented Dec 3, 2014

FYI there is a way to fix this (not the neatest, perhaps) with a check and ini_set in the phar stub - see #3502

acoulton added a commit to acoulton/composer that referenced this issue Dec 3, 2014
Update the phar stub to prevent APC attempting to run the composer classes through
the opcode cache on supported APC versions to avoid the random fatal "duplicate
class" errors at runtime as reported in composer#264.

Prior to APC version 3.0.12 this was a PHP_INI_SYSTEM directive, so output a
warning instead.
acoulton added a commit to acoulton/composer that referenced this issue Dec 3, 2014
Update the phar stub to prevent APC attempting to run the composer classes through
the opcode cache on supported APC versions to avoid the random fatal "duplicate
class" errors at runtime as reported in composer#264.

Prior to APC version 3.0.12 this was a PHP_INI_SYSTEM directive, so output a
warning instead.
acoulton added a commit to acoulton/composer that referenced this issue Dec 3, 2014
Update the phar stub to prevent APC attempting to run the composer classes through
the opcode cache on supported APC versions to avoid the random fatal "duplicate
class" errors at runtime as reported in composer#264.

Prior to APC version 3.0.12 this was a PHP_INI_SYSTEM directive, so output a
warning instead.
Seldaek added a commit that referenced this issue Dec 3, 2014
Prevent APC causing random fatal errors [fixes #264]
enov added a commit to kohana/cache that referenced this issue Jan 22, 2016
Remove `cachedrivers.php.ini` and use existing INI files.
Install Memcache only for PHP 5.*

This is largely inspired from the `.travis.yml` of the Symfony project.

Existing directives in the original `cachedrivers.php.ini` are kept:
apc.max_file_size = 0
apc.cache_by_default = 0

Notes from the original `cachedrivers.php.ini` regarding the above
directives are copied here:

The APC opcode cache needs to be disabled to avoid
composer/composer#264
This allows us to use the user cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet