composer: Platform check uses wrong PHP version on Windows

Hi! I’ve just been experimenting with the scripts options in composer.json and noticed that when running e.g. PHPUnit this way, I run into a PHP version mismatch: composer.phar is invoked with a specific PHP executab, usually from within PHPStorm but executes the script with the PHP found in PATH. After a bit of digging, i think the culprit is Symfony’s ExecutableFinder (called around here in EventDispatcher), which seems to guess the PHP from the environment variables exclusively, ignoring PHP_BINARY (which is available since PHP 5.4).

My composer.json:

{
	"name": "chillerlan/php-oauth-core",
	"description": "A PHP7.4+ OAuth client core library",
	"homepage": "https://github.com/chillerlan/php-oauth-core",
	"license": "MIT",
	"type": "library",
	"minimum-stability": "stable",
	"keywords": [
		"oauth", "oauth1", "oauth2", "authorization", "authentication",
		"client", "psr-7", "psr-17", "psr-18", "rfc5849", "rfc6749"
	],
	"authors": [
		{
			"name": "smiley",
			"email": "smiley@chillerlan.net",
			"homepage": "https://github.com/codemasher"
		}
	],
	"support": {
		"issues": "https://github.com/chillerlan/php-oauth-core/issues",
		"source": "https://github.com/chillerlan/php-oauth-core"
	},
	"provide": {
		"psr/http-client-implementation": "1.0"
	},
	"require": {
		"php": "^7.4 || ^8.0",
		"ext-curl":"*",
		"ext-json":"*",
		"ext-simplexml":"*",
		"ext-zlib": "*",
		"chillerlan/php-httpinterface": "^5.0",
		"psr/http-client":"^1.0",
		"psr/http-message": "^1.0",
		"psr/log": "^1.1 || ^2.0 || ^3.0"
	},
	"require-dev": {
		"phan/phan": "^5.2",
		"phpunit/phpunit": "^9.5"
	},
	"autoload": {
		"psr-4": {
			"chillerlan\\OAuth\\": "src/"
		}
	},
	"autoload-dev": {
		"psr-4": {
			"chillerlan\\OAuthTest\\": "tests/"
		}
	},
	"scripts": {
		"phpunit": "vendor/bin/phpunit",
		"phan": "vendor/bin/phan"
	},
	"config": {
		"lock": false,
		"sort-packages": true,
		"platform-check": true
	}
}

Output of composer diagnose:

Checking composer.json: OK
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 2.1.12
PHP version: 7.4.3
PHP binary path: C:\amp\php\7.4.3\php.exe
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019
cURL version: 7.68.0 libz 1.2.11 ssl OpenSSL/1.1.1d
zip: extension present, unzip not available, 7-Zip present (7z)

When I run this command:

C:\php\8.0.0\php.exe C:/<project>/composer.phar run-script phpunit

I get the following output:

> vendor/bin/phpunit
PHP Fatal error:  Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0". You are running 7.4.3. in <path>\vendor\composer\platform_check.php on line 33

And I expected this to happen:

I’d expect the script executed by run-script to use the same php executable that was used to invoke Composer (8.0.0 instead of 7.4.3 from PATH in this case).

Update: i added the @php to the script line, which seems to have it resolved. However i feel like this is a bit unintuitive.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

The best way to fix this with composer 2.1+ is this (i.e. prefixing with @php):

	"scripts": {
		"phpunit": "@php vendor/bin/phpunit",
		"phan": "@php vendor/bin/phan"
	},

This will make composer execute the binary with PHP directly instead of letting the shell figure out which PHP process. Composer will reuse the currently running PHP process’ path when doing so.

@codemasher updated Monolog