JustBlackBird / gulp-phpcs

Gulp plugin for running PHP Code Sniffer
MIT License
47 stars 12 forks source link

Error: Command failed: /bin/sh -c path/to/composer/bin/phpcs #16

Closed onlyskyguy closed 8 years ago

onlyskyguy commented 8 years ago

Hi there, was hoping to see if anyone could shed some light on an issue I'm having while trying to implement wordpress coding standards with phpcs and phpcbf.

I get the following error message when running gulp-phpcs task:

 Error: Command failed: /bin/sh -c ./lib/vendor/composer/bin/phpcs --standard="WordPress"

I installed phpcs via composer using the following steps:

In the directory with my gulpfile.js (my project root) I installed composer:

curl -sS https://getcomposer.org/installer | php

I've also tried installing composer globally, by moving composer.phar to /usr/local/bin/composer:

mv composer.phar /usr/local/bin/composer

This is my composer.json:

{
    "config": {
           "vendor-dir": "lib/vendor/composer"
       },
    "require-dev": {
        "squizlabs/php_codesniffer": "2.*",
        "wp-coding-standards/wpcs": "dev-master"
    },
    "scripts": {
        "post-install-cmd": [
            "lib/vendor/composer/bin/phpcs --config-set installed_paths lib/vendor/composer/wp-coding-standards/wpcs/"
        ]
    }
}

You can see the installation directory is in "lib/vendor/composer/..." (relative to project root). I ran the install script "composer install" (php composer.phar install), and the files seem to be in the right place.

Here is my gulp task:

gulp.task( 'phpcs', function() {
    return gulp.src([assets.templates.src + '*.php'])
        .pipe(plugins.phpcs({
            bin: './lib/vendor/composer/bin/phpcs',
            standard: 'WordPress'
        }))
        .pipe(plugins.phpcs.reporter( 'log' ));
});

I'm loading plugins with gulp-load-plugins. When I run the task, phpcs seems to work but I get an error message:

[14:52:23] PHP Code Sniffer found a problem in path/to/project/src/templates/page.php
Message:
    Error: Command failed: /bin/sh -c ./lib/vendor/composer/bin/phpcs --standard="WordPress"

    FILE: ...path/to/project/src/templates/page.php
    ----------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------
     26 | ERROR | [x] Line indented incorrectly; expected 4 tabs, found 5
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------

    Time: 40ms; Memory: 4.5Mb

What's up with the error?? It sounds like it's not finding the script (reference to it) or can't run it?

I get the same error when running my phpcbf task too:

gulp.task( 'phpcbf', function() {
    return gulp.src([assets.templates.src + '**/*.php'])
        .pipe(plugins.phpcbf({
            bin: paths.lib +'vendor/composer/bin/phpcbf',
            standard: 'WordPress',
            warningSeverity: 0
        }))
        .pipe(gulp.dest(build));
});

Would appreciate any help debugging this!

BTW I'm on ubuntu running node 4.1.1 via NVM.

JustBlackBird commented 8 years ago

I can say nothing about gulp-phpcbf (because I'm not related with it) but as for gulp-phpcs it seems that it works as designed.

One of you file (path/to/project/src/templates/page.php) has problems with codding style (namely, an extra tab at line 26). PHPCodeSniffer found this error and gulp-phpcs just output it to console (because you've used log reporter). As for me, everything is fine.

Am I missed something?

onlyskyguy commented 8 years ago

Thanks for getting back on this!

I definitely considered that a possibility, that the error was in reference to the php error, not the plugin, since it definitely also actually does the phpcs task and shows php errors, what threw me off was that I got the same error for phpcbf and it didn't work.

Looks resolved then, thanks for your help!

vonkanehoffen commented 8 years ago

Hey sorry to reopen this but I'm getting the same issue and I think it's a bug. PHPCS is running OK and showing output as expected, but I'm still getting:

Error: Command failed: /bin/sh -c phpcs --standard="ruleset.xml" --warning-severity=0

above that output. ....despite the command appearing to run as planned. Any ideas?

To clarify, the whole output from a file is something like:

[18:40:37] PHP Code Sniffer found a problem in /Users/cormorant/Sites/Daisy/daisygroup.com/htdocsv2/wp-content/themes/daisy/404.php
Message:
    Error: Command failed: /bin/sh -c phpcs --standard="ruleset.xml" --warning-severity=0

    FILE: ...es/Daisy/daisygroup.com/htdocsv2/wp-content/themes/daisy/404.php
    ----------------------------------------------------------------------
    FOUND 5 ERRORS AFFECTING 5 LINES
    ----------------------------------------------------------------------
     33 | ERROR | [x] Multi-line function call not indented correctly;
        |       |     expected 18 spaces but found 16
     34 | ERROR | [x] Multi-line function call not indented correctly;
        |       |     expected 18 spaces but found 16
     35 | ERROR | [x] Multi-line function call not indented correctly;
        |       |     expected 18 spaces but found 16
     36 | ERROR | [x] Multi-line function call not indented correctly;
        |       |     expected 18 spaces but found 16
     37 | ERROR | [x] Multi-line function call not indented correctly;
        |       |     expected 18 spaces but found 16
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------

    Time: 48ms; Memory: 3.5Mb

...note the "command failed" bit, despitethe command not failing!

JustBlackBird commented 8 years ago

Do you just worry about Error: Command failed: line? Are the codding style errors found by PHPCS running with and without plugin different?

vonkanehoffen commented 8 years ago

Yeah it's just that line in the output. Style errors found are identical.

It's important for me to get to the bottom of this though as my Gulp script will be used by much less technical web designers who will freak out about it saying Command failed ;-)

Can you suggest any debugging I can do? There aren't any errors being logged by php, which was my first port of call...

JustBlackBird commented 8 years ago

Well, you see it's not a real bug it's an implementation detail. PHPCS exits with non-zero exit code when a style error is found. It's not a error inside PHPCS it's just how it works. At the same time PHPCS returns non-zero exit code when a real error occurs.

I'll try to learn more about PHPCS exact exit codes and will try to find a way to suppress the line you're worrying about.

JustBlackBird commented 8 years ago

@vonkanehoffen it seems that I finally found a workaround to separate codding style problems of other errors and non-zero exit codes. After some tests I'll release new version of the plugin.

vonkanehoffen commented 8 years ago

Ah that's awesome! Look forward to seeing it!

Hopefully one day I'll get the time to learn more about these things so I can patch such issues myself eh ;-)

Great plugin btw - even with the error it's proving very useful, so thanks very much.