Skip to content

Add cwd to codesniffer paths - #218

Open
indykoning wants to merge 4 commits into
tighten:3.xfrom
indykoning:bugfix/phpcodesniffer-error-in-build-folder
Open

Add cwd to codesniffer paths#218
indykoning wants to merge 4 commits into
tighten:3.xfrom
indykoning:bugfix/phpcodesniffer-error-in-build-folder

Conversation

@indykoning

Copy link
Copy Markdown

Fixes: #201

PHP_CodeSniffer uses regex in their --ignore list transforming * into .*
Thus we can prefix <cwd>/* for all ignore rules to ensure the main folder is ignored.

Running the original duster before these changes on a repo without /build/ in the project path

 => Linting using PHP_CodeSniffer 
............................................................  60 / 122 (49%)
............................................................ 120 / 122 (98%)
..                                                           122 / 122 (100%)

Running the original duster before these changes on a repo with /build/ in the project path

 => Linting using PHP_CodeSniffer 
ERROR: No files were checked.
All specified files were excluded or did not match filtering rules.

Running the changed duster on a repo with /build/ in the repo path

=> Linting using PHP_CodeSniffer 
............................................................  60 / 122 (49%)
............................................................ 120 / 122 (98%)
..                                                           122 / 122 (100%)

Since only PHP_CodeSniffer complains and i am certain how it handles ignores i am only applying it there.
with build in the project path Tlint, PHP CS Fixer, and Pint all give feedback. Only PHP_CodeSniffer gave these issues

@mattstauffer

Copy link
Copy Markdown
Member

Thanks for tracking this down! The fix is right in spirit, but the Windows CI failures come from the separator handling.

TL;DR: We need to convert everything to Mac-style on Windows machines, and let PHPCS handle conversions for us.


Longer story, with the aide of my buddy Claude for writing it out more fully:

PHPCS treats each --ignore value as a regex, and on Windows getcwd() returns a backslash path (e.g. C:\Users\...\duster). With DIRECTORY_SEPARATOR (\) those backslashes go into the regex unescaped, so \U, \u, etc. become invalid escapes and PCRE2 can't compile the pattern — that's the PCRE2 does not support \F, \L, \l, \N{name}, \U, or \u error, which then aborts the whole PHPCS run.

The key detail is that PHPCS expects / separators and translates them for Windows itself (Filter.php):

// We assume a / directory separator, as do the exclude rules
// most developers write, so we need a special case for any system that is different.
if (DIRECTORY_SEPARATOR === '\\') {
    $replacements['/'] = '\\\\';
}

So the pattern should always use forward slashes, and getcwd() needs normalizing too. This version compiles cleanly on Windows and still fixes the original /build/-in-path bug:

$cwd = str_replace('\\', '/', getcwd());

$ignore = $this->dusterConfig->get('exclude')
    ? ['--ignore=' . implode(',',
        array_map(fn ($path) => str_contains($path, $cwd) ? $path : $cwd . '/*' . $path, $this->dusterConfig->get('exclude')))]
    : [];

i.e. drop DIRECTORY_SEPARATOR for a literal /, and normalize getcwd() with str_replace('\\', '/', ...). I ran this locally — full suite passes and the /build/-path repro is still fixed.

Comment thread app/Support/PhpCodeSniffer.php
@indykoning

Copy link
Copy Markdown
Author

The key detail is that PHPCS expects / separators and translates them for Windows itself

That one was unexpected, thanks for the tip! Confirmed to still work on my device (unfortunately i have no windows device to test on)
I'll push the change in a moment!

@indykoning

Copy link
Copy Markdown
Author

Tests are now succeeding! I've also added 2 small tests to check that it actually lints in /build/ (or other) root paths. And one to make sure no errors occur if /build/ is in the path even if the file is correctly formatted.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Running duster lint fails when no files checked

2 participants