Psalm: Required package "nikic/php-parser" is not installed

Created on 13 May 2019  Â·  43Comments  Â·  Source: vimeo/psalm

It throws following error on running command "vendor/bin/psalm --init"

Required package "nikic/php-parser" is not installed: cannot detect its version in [..]/vendor/muglug/package-versions-56/src/PackageVersions/Versions.php on line 281

I tried on dev-master and 3.2.11 versions.

All 43 comments

Hey! Do you have any packages that clash with nikic/php-parser? What does your composer.json look like?

{
    "name": "monofony/symfony_starter",
    "description": "Starter project for Symfony projects",
    "license": "MIT",
    "type": "project",
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "require": {
        "php": "^7.2",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-fixtures-bundle": "^2.3",
        "doctrine/doctrine-migrations-bundle": "^2.0",
        "doctrine/orm": "^2.4.8",
        "eightpoints/guzzle-bundle": "^7.3",
        "friendsofsymfony/oauth-server-bundle": "^1.6",
        "fzaninotto/faker": "^1.8",
        "guzzlehttp/guzzle": "^6.2",
        "incenteev/composer-parameter-handler": "~2.0",
        "knplabs/doctrine-behaviors": "~1.1",
        "sensio/framework-extra-bundle": "^5.1",
        "sensiolabs/security-checker": "^5.0",
        "sonata-project/block-bundle": "^3.3",
        "sylius/customer-bundle": "^1.4",
        "sylius/fixtures-bundle": "^1.4",
        "sylius/grid-bundle": "^1.4",
        "sylius/mailer-bundle": "^1.4",
        "sylius/resource-bundle": "^1.4",
        "sylius/ui-bundle": "^1.4",
        "sylius/user-bundle": "^1.4",
        "symfony/dotenv": "^4.2",
        "symfony/flex": "^1.2",
        "symfony/monolog-bundle": "^3.3",
        "symfony/proxy-manager-bridge": "^4.2",
        "symfony/swiftmailer-bundle": "^3.0",
        "symfony/twig-bundle": "^4.2",
        "twig/extensions": "^1.5",
        "twig/twig": "^2.7"
    },
    "require-dev": {
        "behat/behat": "^3.2",
        "behat/mink": "^1.7@dev",
        "behat/mink-browserkit-driver": "^1.3",
        "behat/mink-extension": "^2.2",
        "behat/mink-selenium2-driver": "^1.3",
        "friends-of-behat/page-object-extension": "^0.3",
        "friends-of-behat/symfony-extension": "^2.0",
        "friends-of-behat/variadic-extension": "^1.1",
        "friendsofphp/php-cs-fixer": "^2.13",
        "infection/infection": "^0.12",
        "lakion/mink-debug-extension": "^1.2",
        "lchrusciel/api-test-case": "^3.1",
        "leanphp/phpspec-code-coverage": "^4.2",
        "nikic/php-parser": "^4.2",
        "phpspec/phpspec": "^4.0",
        "phpstan/phpstan-doctrine": "^0.10",
        "phpstan/phpstan-shim": "^0.10",
        "phpstan/phpstan-symfony": "^0.10",
        "phpstan/phpstan-webmozart-assert": "^0.10",
        "se/selenium-server-standalone": " ^2.52",
        "symfony/debug-bundle": "^4.2",
        "symfony/web-profiler-bundle": "^4.2",
        "symfony/web-server-bundle": "^4.2"
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd",
            "security-checker security:check": "script"
        }
    },
    "config": {
        "sort-packages": true
    },
    "extra": {
        "symfony": {
            "allow-contrib": true
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

what happens when you run composer remove phpstan/phpstan-shim?

what happens when you run composer remove phpstan/phpstan-shim?

Never mind that, it looks like you don't have Psalm installed via composer?

Same problem but during composer remove command.
I commit the branch so you can test it directly.
https://github.com/loic425/SymfonyStarter/tree/features/psalm

Sorry, I copied composer.json details on a reverted status.

You may notice that nikic/php-parser is not actually installed (i.e. not in composer.lock). It's "replaced" with phpstan-shim, but Composer's not able to locate those classes (because they're part of a binary that's only loaded when phpstan is executing). You'll need to raise it here: https://github.com/phpstan/phpstan-shim

Replacing phpstan-shim with phpstan fixes the problem (cc @ondrejmirtes)

I don't believe "nikic/php-parser" needs to be replaced by the shim FWIW

It needs to. PHP-Parser is bundled inside the PHAR because PHPStan needs it. It's a single-file distribution. It works for projects thanks to this file that's called by Composer: https://github.com/phpstan/phpstan-shim/blob/master/bootstrap.php

IMHO Composer or PackageVersions.php should be fixed to also detect versions of "replaced" packages.

PHP-Parser is bundled inside the PHAR because PHPStan needs it. It's a single-file distribution

Yes, but the "replace" .... "nikic/php-parser:^4.0" isn't needed AFAIK - what breaks if both are installed? If the Phar loads its autoloader first, does composer need to load the files in vendor/nikic/php-parser?

It would break if those were two different versions...

@muglug Why keeping to use your muglug/package-versions-56? Cause this project now requires only ^7.0

Replacing phpstan-shim with phpstan fixes the problem (cc @ondrejmirtes)

Which composer commands did you use ?
It doesn't work for me.

@ondrejmirtes seems to work just fine for https://packagist.org/packages/muglug/test-phpstan-shim-no-replace on dev-master

@loic425

Why keeping to use your muglug/package-versions-56

Because the upstream package requires PHP 7.1

I think Psalm 4 might require PHP 7.1, but I likely won't release that until the autumn.

@loic425

Which composer commands did you use ? It doesn't work for me.

I had to remove composer.lock and vendor and re-run Composer. Composer doesn't re-install replaced packages when removing packages that replaced them, I think.

I had to remove composer.lock and vendor and re-run Composer. Composer doesn't re-install replaced packages when removing packages that replaced them, I think.

Yes it works :)

phpstan-shim without replaces for nikic/php-parser does not work that
simply - if the project uses different version of php-parser, the analysis
breaks. PHPStan will see the reflection of its php-parser and not of the
project’s.

On Mon, 13 May 2019 at 18:36, Matthew Brown notifications@github.com
wrote:

It's ticketed here: composer/composer#7573
https://github.com/composer/composer/issues/7573

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/vimeo/psalm/issues/1630?email_source=notifications&email_token=AAAZTODO4ZD73MIXLV3XF3DPVGKI7A5CNFSM4HMQIS72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVI336Y#issuecomment-491896315,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAZTOCO2Z4W7HJAUTOYEODPVGKI7ANCNFSM4HMQIS7Q
.

>

Ondřej Mirtes

if the project uses different version of php-parser, the analysis
breaks

Could you provide an example?

Projects installs nikic/php-parser 3.0, PHPStan uses 4.0.

When trying to analyse the project, PHPStan will work with reflection from
4.0 because that’s the one it loads first. So if there’s some property or
method removed in 4.0, when someone calls it in the project, PHPStan will
complain it does not exist.

That’s the reason to enforce the same version of php-parser in Composer.
Other libraries don’t have this problem because they are prefixed in the
PHAR. PHP-Parser cannot be prefixed because PHPStan extensions reference
it, and they need to work with both shim and non-shim package.

On Mon, 13 May 2019 at 20:05, Matthew Brown notifications@github.com
wrote:

if the project uses different version of php-parser, the analysis
breaks

Could you provide an example?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/vimeo/psalm/issues/1630?email_source=notifications&email_token=AAAZTOELQFEXTUH2UUUXLKTPVGUU3A5CNFSM4HMQIS72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVJDG6Q#issuecomment-491926394,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAZTOB3REWP3GFNB4K2CWTPVGUU3ANCNFSM4HMQIS7Q
.

>

Ondřej Mirtes

Ok, but require enforces that just fine

But why do that and install nikic/php-parser twice if it's already in the PHAR...

There are three options IMO:

  1. keep using replaces, and publicly advocate that people use a package that prevents installation alongside Psalm and other tools that use @nikic's php-parser, putting the onus on those tools' maintainers to explain why their software doesn't work with Composer.
  2. use require, which installs nikic/php-parser in --dev installations, a harmless extra 3MB
  3. don't reference nikic/php-parser at all from the shim's composer.json, but include a check in the Phpstan to ensure that no incompatible version of PHP Parser is installed.

1) Other tools work. The only problem you're experiencing is that PackageVersions do not consider "replaced" package to be installed, which is an error that should be fixed.

I might consider 2) but I don't think it's necessary yet. This problem might get worse because I plan to use PHAR as the default distribution method in 0.12: https://github.com/phpstan/phpstan/issues/2074

Other tools work

Not as far as I can see - check out https://github.com/muglug/phpstan-shim-test

and run composer. Note how nikic/php-parser is not installed. There's no way for Psalm to run if nikic/php-parser is not installed.

I've added another package I had no hand in making to that repo - phpstan-shim also prevents it from working. I realise that was not your intention, but it's nevertheless annoying.

Yeah, goaop's file bootstrap is loaded sooner than phpstan-shim's, that's unfortunate... so maybe 2) will be the way to go, thanks.

goaop's file bootstrap is loaded sooner than phpstan-shim's

Not sure I follow you. replaced packages should be available via vendor/autoload.php, but phpstan-shim's are only available when running PHPStan (so it's breaking the contract entirely).

phpstan-shim/bootstrap.php is in Composer's autoload/files but it gets loaded later than goaop/bootstrap.php and the latter already requires PhpParser to exist.

Would be awesome to have it fixed. Now the only solution I can see is to require nikic/php-parser or use phpstan/phpstan instead of phpstan/phpstan-shim.

Can we at least reopen this issue?

Thanks a lot! :)

Let me know if I can help you with it, but it seems we need mostly your decision.

Actually, require nikic/php-parser don't work, for me, the only way to combine psalm and phpstan was to replace phpstan/phpstan-shim with phpstan/phpstan

I've tried something, please test phpstan-shim dev-master, thanks
https://github.com/phpstan/phpstan-shim/commit/0b4869651ecaf93f489cda6b0b7111d1cb34e59d

@ondrejmirtes same error with dev-master ...

@koriym have you tried to remove your composer.lock? See here

I’ve reverted the change for now so make sure to refer one of the commits
that contain it.

On Mon, 20 May 2019 at 09:38, Loïc Frémont notifications@github.com wrote:

@koriym https://github.com/koriym have you tried to remove your
composer.lock? See here
https://github.com/vimeo/psalm/issues/1630#issuecomment-491871931

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/vimeo/psalm/issues/1630?email_source=notifications&email_token=AAAZTOEN7OUTIEIMPMAR6OLPWJIPXA5CNFSM4HMQIS72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVX6KKI#issuecomment-493872425,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAZTOAEQVLFO2GSB4IS3CLPWJIPXANCNFSM4HMQIS7Q
.

>

Ondřej Mirtes

@ondrejmirtes
PHP Warning: require(/Users/loic/www/monofony/vendor/composer/../phpstan/phpstan-shim/bootstrap.php): failed to open stream: No such file or directory in /Users/loic/www/monofony/vendor/composer/autoload_real.php

I was having the same issue and the workaround I used is to remove phpstan and psalm from my composer requirements. Then I've install https://github.com/bamarni/composer-bin-plugin and created a separate bin for phpstan and for psalm.

This way you can require phpstan and psalm in separate bins. Nice bonus is that you don't have to use a shim since there will be no conflicting dependencies.

@loic425 It worked with local install. But it failed when it is installed in globally (composer global require)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vudaltsov picture vudaltsov  Â·  3Comments

Ocramius picture Ocramius  Â·  3Comments

muglug picture muglug  Â·  3Comments

albe picture albe  Â·  3Comments

ErikBooijCB picture ErikBooijCB  Â·  4Comments