PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Error in case file with rulesets name exists in project root

Open martinssipenko opened this issue 4 years ago • 2 comments

Describe the bug

In case there is a file in project root that happens to match a ruleset name the file is loaded instead as rulesets configuration.

ERROR: Ruleset /path/to/project/PSR12 is not valid
- On line 1, column 1: Start tag expected, '<' not found

To reproduce Steps to reproduce the behavior:

  1. Create a file called PSR12 in the project root and foo as it's content.
  2. Create phpcs.xml file with the following contents:
<?xml version="1.0"?>
<ruleset>
    <rule ref="PSR12"/>
</ruleset>

  1. Assuming that phpcs was installed using composer run ./vendor/bin/phpcs.
  2. See error message displayed
$ ./vendor/bin/phpcs
ERROR: Ruleset /path/to/project/PSR12 is not valid
- On line 1, column 1: Start tag expected, '<' not found


Run "phpcs --help" for usage information

Expected behavior The PSR12 standard should be used.

Versions (please complete the following information):

  • OS: MacOS 10.15
  • PHP: 7.4
  • PHPCS: 3.5.8
  • Standard: any

martinssipenko avatar Jan 06 '21 10:01 martinssipenko

Can definitely confirm this issue! We use https://github.com/cakephp/cakephp-codesniffer which is based upon this package but If I also create a cakephp file in my "root" directoy and then call vendor/bin/phpcs --colors --parallel=16 -p src/ tests/ I get the error

ERROR: Ruleset /Users/kevinpfeifer/Documents/CakePHP/cakephp/CakePHP is not valid

After I delete my cakephp file it works again as well.

But the weird thing is also, that I can circumvent this issue if I specify the used standard via

vendor/bin/phpcs --colors --parallel=16 -p --standard=CakePHP src/ tests/

Part of this whole issue may also be dependet/related on the used operating system / the used filesystem since I am on MacOS with a case-insensitive filesystem.

LordSimal avatar Jun 04 '22 21:06 LordSimal

Alright, the main problem here is the fact, that the rule name = file name and we therefore go into this if https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Ruleset.php#L720

Therefore we don't parse/load the set value in installed_paths and load the rules from that folder.

LordSimal avatar Jun 04 '22 21:06 LordSimal