moodlehq / moodle-local_codechecker

A Moodle local plugin providing a simple web UI to run the MoodleCS coding style checks with PHP_CodeSniffer.
63 stars 72 forks source link

Avoid problems under Windows with codechecker reading core .phpcs.xml configuration file #207

Closed stronk7 closed 1 year ago

stronk7 commented 1 year ago

Since some months ago, core includes a .phpcs.xml file, really useful that instructs to any PHP_CodeSniffer about which standard must be used by default.

And the standard is "moodle" how now.

Problem is that PHP_CodeSniffer first looks for local (pwd) directories with that name ("moodle") and, in the case of codechecker, that's currently a soft alias, pointing to the new moodle-cs standard, and kept on purpose to allow other tools to continue working.

While codechecker's bundled PHP_CodeSniffer doesn't need that file at all (because the standards are perfectly configured via .conf file), its default behaviour is to look to those .phpcs.xml files. Then it reads "moodle" from there and resolves it to the soft alias. And soft aliases don't work on Windows, causing an error.

An error occurred during processing; checking has been aborted. The error message was:
Ruleset C:\...\local\codechecker\moodle is not valid - On line 1, column 1: Start tag expected, '<' not found

We need to ensure that codechecker's bundled PHP_CodeSniffer stops looking to core .phpcs.xml, surely by introducing its own .phpcs.xml file (that will cause it to stop looking upwards). At least that's the theory.

Let's see. Ciao :-)

stronk7 commented 1 year ago

Ok, adding a phpcs.xml file with contents:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="moodle" xsi:noNamespaceSchemaLocation="phpcs.xsd">
  <rule ref="MoodleCS/moodle"/>
</ruleset>

seems to be enough. It:

  1. Points to the correct moodle/MoodleCS directory where the moodle-cs imports are.
  2. Prevents PHP_CodeSniffer to go looking for other phpcs.xml files upwards.

Curiously... this shouldn't be needed because, in theory, phpcs.xml files are only searched if not standard has been specified and our runs are, 100% sure, specifying the standard always. Anyway, I won't look more about this. Historically the CLI/API use of PHP_CodeSniffer has been "complex", and sometimes they only look for CLI options that we aren't using. So... who knows.

The important point is that by providing a correct phpcs.xml file like the above... the "moodle" alias is not tried anymore and things come back to work under Windows, that doesn't understand the "moodle" alias.

Going to perform some more test and will create PR.

Ciao :-)

stronk7 commented 1 year ago

Ok, I've tried both Web and the run.php utility, Unix and Windows... and everything seems to be working ok with the phpcs.xml file above. Heading to create a PR...