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

installed_paths depend on PHP executable? #216

Closed
1 task done
kkmuffme opened this issue Jan 16, 2024 · 12 comments
Closed
1 task done

installed_paths depend on PHP executable? #216

kkmuffme opened this issue Jan 16, 2024 · 12 comments

Comments

@kkmuffme
Copy link

kkmuffme commented Jan 16, 2024

Problem/Motivation

The loaded rulesets seems to be PHP-executing version specific, while composer itself is not.
My composer.json has platform.php '8.3.0' set.
My default PHP is 8.0

I install/update normally via composer install and see everything is correct:

PHP CodeSniffer Config installed_paths set to ....,../../wp-coding-standards/wpcs

Then I run phpcbf with php 8.3 like: (my-ruleset.xml contains WordPress ruleset)
/usr/bin/php83 /path/to/vendor/bin/phpcbf --standard=/path/to/my-ruleset.xml /other/path/file.php

Expected behaviour

no error

Actual behavior

Error:

Referenced sniff "WordPress" does not exist

Fix

/usr/bin/php83 -d xdebug.mode=off /usr/bin/composer install

I get the same output as above

PHP CodeSniffer Config installed_paths set to ....,../../wp-coding-standards/wpcs

but then it works correctly.

Proposed changes

The config should be loaded independently of the actually executing PHP script.
This is especially relevant in CI, where 1 PHP version might install packages for various executable PHP versions using platform.php.

Environment

Question Answer
OS Linux
PHP version 8.3
Composer PHPCS plugin version 1.0.0
Install type Composer global

Tested against main branch?

  • I have verified the issue still exists in the main branch.
@jrfnl
Copy link
Member

jrfnl commented Jan 16, 2024

@kkmuffme This issue is completely unclear to me. This plugin will use the PHP version used during the Composer install to run whatever commands it needs, but the end-result is a CodeSniffer.conf file which is not specific to any particular PHP version and the same can be said about running PHPCS, so I don't understand what you are trying to report/what is the perceived problem.

@jrfnl jrfnl added the triage label Jan 16, 2024
@kkmuffme
Copy link
Author

kkmuffme commented Jan 16, 2024

  1. set composer.json platform.php to '8.3.0'
  2. run composer install (the default PHP executable must not be PHP 8.3 but e.g. 8.0)
  3. run phpcbf with non-default PHP executable, e.g. /usr/bin/php83 /path/to/vendor/bin/phpcbf
    => Error:

Referenced sniff "WordPress" does not exist

*seems like this part got somehow removed in my OP, fixed now, this made it unclear

@jrfnl
Copy link
Member

jrfnl commented Jan 16, 2024

Cannot reproduce.

{
	"name": "installes/216",
	"require-dev": {
		"squizlabs/php_codesniffer": "^3.8.0",
		"wp-coding-standards/wpcs": "^3.0.1"
	},
	"config": {
		"allow-plugins": {
			"dealerdirect/phpcodesniffer-composer-installer": true
		},
		"platform": {
			"php": "8.3.0"
		}
	}
}
$ composer install
PHP 8.0.30 (cli) (built: Sep  1 2023 14:15:38) ( ZTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies
    with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans

Composer version 2.6.6 2023-12-08 18:32:26

No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 5 installs, 0 updates, 0 removals
  - Locking dealerdirect/phpcodesniffer-composer-installer (v1.0.0)
  - Locking phpcsstandards/phpcsextra (1.2.1)
  - Locking phpcsstandards/phpcsutils (1.0.9)
  - Locking squizlabs/php_codesniffer (3.8.1)
  - Locking wp-coding-standards/wpcs (3.0.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing squizlabs/php_codesniffer (3.8.1): Extracting archive
  - Installing dealerdirect/phpcodesniffer-composer-installer (v1.0.0): Extracting archive
  - Installing phpcsstandards/phpcsutils (1.0.9): Extracting archive
  - Installing phpcsstandards/phpcsextra (1.2.1): Extracting archive
  - Installing wp-coding-standards/wpcs (3.0.1): Extracting archive
Generating autoload files
4 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
PHP CodeSniffer Config installed_paths set to ../../phpcsstandards/phpcsextra,../../phpcsstandards/phpcsutils,../../wp-coding-standards/wpcs
$ php -v
PHP 8.3.0 (cli) (built: Nov 21 2023 17:48:31) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.3.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.0, Copyright (c), by Zend Technologies
    with Xdebug v3.3.0alpha3, Copyright (c) 2002-2023, by Derick Rethans
$ "vendor/bin/phpcs" -i
The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, Modernize, NormalizedArrays, Universal, PHPCSUtils, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
$ "vendor/bin/phpcs" -ps ./test.php --standard=WordPress
E 1 / 1 (100%)

FILE: test.php
-----------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------
 1 | ERROR | Missing file doc comment (Squiz.Commenting.FileComment.Missing)
-----------------------------------------------------------------------------

Time: 574ms; Memory: 12MB

@jrfnl
Copy link
Member

jrfnl commented Jan 16, 2024

@kkmuffme Only thing I can think of at this moment is that your custom ruleset contains an <config... setting overruling the installed paths or that something in your Composer scripts is screwing things up.

@kkmuffme
Copy link
Author

I have some config directives in the ruleset:

<config name="minimum_wp_version" value="6.2"/>
<config name="testVersion" value="7.4-"/>
<config name="php_version" value="70400"/>

And also some CLI sets:

--runtime-set php_version 80000

For whatever reason this issue only occurred for phpcbf but not for phpcs making this even weirder.

@kkmuffme
Copy link
Author

Ah just saw your reproduction - this isn't my case though: in both your cases your "php" executable was the default one (despite being different versions)

The issue only happens when EXPLICITLY running it with a different PHP executable

@jrfnl
Copy link
Member

jrfnl commented Jan 16, 2024

in both your cases your "php" executable was the default one (despite being different versions)

No, I explicitly run with specific PHP versions, just do so under the hood. Shouldn't make a difference anyhow (and it doesn't).

$ C:\wamp\bin\php\php8.3.0\php.exe "vendor/bin/phpcs" -ps ./test.php --standard=WordPress --basepath=.
E 1 / 1 (100%)

FILE: test.php
-----------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------
 1 | ERROR | Missing file doc comment (Squiz.Commenting.FileComment.Missing)
-----------------------------------------------------------------------------

Time: 623ms; Memory: 12MB

Seriously, please do some debugging yourself before reporting an issue and report it with a proper reproduction case.
I've seen so many issues by you already and only 1 in 20 is valid.

@kkmuffme
Copy link
Author

I can still reproduce this consistently after upgrading phpcs via composer update

I have to explicitly run /usr/bin/php83 -d xdebug.mode=off /usr/bin/composer install again to fix it.
Not a big deal though

@kkmuffme kkmuffme closed this as not planned Won't fix, can't repro, duplicate, stale Jan 16, 2024
@jrfnl
Copy link
Member

jrfnl commented Jan 16, 2024

@kkmuffme There might be something in your dependencies which is incompatible with PHP 8.0 (used during composer install) which is causing a fatal error when the autoload file is requested on PHP 8.0 (which is needed to run any plugins).

Also see: #79

Having said that, if that's the case, than:

  1. You still never provided a valid reproduction case. Purely based on the info provided, this issue cannot be reproduced.
  2. It is a not a problem with this plugin, but a problem with how you run Composer.
    The platform.php setting is not intended to allow you to use a low PHP version with a high platform setting. It is intended for allowing to run Composer on a higher version and prevent locking in dependencies not compatible with your minimum PHP version.

@kkmuffme
Copy link
Author

  1. I can reproduce it with the exact steps outlined above. Since the workaround is easy enough, I don't want to waste your and my time.

  2. That's wrong, see the example from composer's docs :-) (lower PHP version with higher platform setting)

@jrfnl
Copy link
Member

jrfnl commented Jan 16, 2024

  1. I can reproduce it with the exact steps outlined above. Since the workaround is easy enough, I don't want to waste your and my time.

No you can't. There must be something else in your composer.json which is causing the problem. And without that extra information it's a guessing game. (which, yes, has wasted my time)

2. That's wrong, see the example from composer's docs :-) (lower PHP version with higher platform setting)

See my previous answer. You can use it like that, but if anything in your dependencies will have installed something which includes syntax incompatible with the runtime PHP version for Composer, the autoload.php file will throw a fatal and will block all plugins from working.

In other words, know your tools.

@kkmuffme
Copy link
Author

  1. my runtime PHP version matches the platform.php I set as outlined in the OP.

Anyway, wish you a great day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants