phpro / grumphp

A PHP code-quality tool
MIT License
4.15k stars 431 forks source link

Doesn't run phpCS task command on pre-commit #534

Closed Sanguigui closed 6 years ago

Sanguigui commented 6 years ago
Q A
Version 0.14.1
Bug? yes
New feature? no
Question? no
Documentation? no
Related tickets #515

When I run ./vendor/bin/grumphp run, my phpcs tasks is well executed and I got the errors I'm expecting but when I run ./vendor/bin/grumphp git:pre-commit the phpcs task is green and 'All good face' is there.

My configuration

# grumphp.yml
parameters:
    bin_dir: ./vendor/bin
    git_dir: ../
    hooks_preset: local
    tasks:
        phpcs:
            standard: PSR2
            severity: 5
            error_severity: 5
            warning_severity: 5
            ignore_patterns: [./tests/*, ./resources/*, ./database/*, ./bootstrap/*, ./public/*, ./auradocs/*, ./composer-setup]

The composer.json file

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.2.0",
        "barryvdh/laravel-debugbar": "^3.1",
        "doctrine/dbal": "*",
        "erusev/parsedown": "^1.7",
        "fideloper/proxy": "~4.0",
        "guzzlehttp/guzzle": "~6.3",
        "jenssegers/mongodb": "^3.4.2",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "5.6.*",
        "maatwebsite/excel": "~2.1.28",
        "spatie/laravel-permission": "^2.12",
        "webpatser/laravel-uuid": "^3.0"
    },
    "require-dev": {
        "barryvdh/laravel-ide-helper": "^2.4",
        "filp/whoops": "^2.2",
        "fzaninotto/faker": "~1.7",
        "laravel/browser-kit-testing": "^4.0",
        "laravel/dusk": "^3.0",
        "mockery/mockery": "^1.1",
        "nunomaduro/collision": "~2.0",
        "nunomaduro/larastan": "^0.2.4",
        "phpro/grumphp": "v0.14.1",
        "phpunit/phpunit": "~7.2",
        "spatie/laravel-tail": "^3.1",
        "squizlabs/php_codesniffer": "^3.3",
        "phpstan/phpstan": "^0.10.2",
        "phpmd/phpmd": "@stable"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "HP\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "@php artisan ide-helper:generate",
            "@php artisan ide-helper:meta",
            "@php artisan ide-helper:models -N -I \"HP\\Models\\GlobalTags,HP\\Models\\ClusterCandidate\""
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        },
        "grumphp": {
            "config-default-path": "/var/www/auraengine.io/auraengine/grumphp.yml"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

My actions

With the ./vendor/bin/grumphp run -vvv command without the whole project listing:

vendor/bin/grumphp run -vvv
[2018-08-21 09:02:02] GrumPHP.DEBUG: Repository created (git dir: "/var/www/auraengine.io/.git", working dir: "/var/www/auraengine.io") [] []
GrumPHP is sniffing your code!
Running task 1/1: Phpcs... 
Command: '/var/www/auraengine.io/auraengine/vendor/bin/phpcs' '--standard=PSR2' '--severity=5' '--error-severity=5' '--warning-severity=5' '--ignore=./tests/*,./resources/*,./database/*,./bootstrap/*,./public/*,./auradocs/*,./composer-setup' '--report-full' '--report-json'
✘
FILE: ...ngine.io/auraengine/app/ServiceHelpers/Exports/ClusterExport.php
----------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------
 45 | WARNING | [ ] Line exceeds 120 characters; contains 131
    |         |     characters
 50 | ERROR   | [x] Each PHP statement must be on a line by itself
 50 | ERROR   | [x] Each PHP statement must be on a line by itself
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

with ./vendor/bin/grumphp git:pre-commit -vvv:

[2018-08-21 09:15:37] GrumPHP.DEBUG: Repository created (git dir: "/var/www/auraengine.io/.git", working dir: "/var/www/auraengine.io") [] []
[2018-08-21 09:15:37] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2018-08-21 09:15:37] GrumPHP.DEBUG: last command (diff) duration: 47.46ms [] []
[2018-08-21 09:15:37] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2018-08-21 09:15:37] GrumPHP.DEBUG: last command (diff) output:  [] []
GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/1: Phpcs... ✔
             ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
           ▄▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌           
         ▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
        ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
        ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
  ▄▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
 ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
 ▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
   ▀█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
     ▀▀▓▓▓▓▓▓▓▓▓▓▓▓█▀▀▀▀▀▀▀▀▀▀▀▀▀▀████████████▄
      ▄████████▀▀▀▀▀                 ██████████   
     ███████▀                         ██████▀
      ▐████      ██▌          ██       ████▌      
        ▐█▌                            ███        
         █▌           ▄▄ ▄▄           ▐███        
        ███       ▄▄▄▄▄▄▄▄▄▄▄▄       ▐███         
         ██▄ ▐███████████████████████████
        █▀█████████▌▀▀▀▀▀▀▀▀▀██████████▌▐         
          ███████████▄▄▄▄▄▄▄███████████▌          
         ▐█████████████████████████████           
          █████████████████████████████           
           ██ █████████████████████▐██▀           
            ▀ ▐███████████████████▌ ▐▀            
                ████▀████████▀▐███                
                 ▀█▌  ▐█████  ▐█▌                 
                        ██▀   ▐▀                  
       _    _ _                         _ _
      / \  | | |   __ _  ___   ___   __| | |
     / _ \ | | |  / _` |/ _ \ / _ \ / _` | |
    / ___ \| | | | (_| | (_) | (_) | (_| |_|
   /_/   \_\_|_|  \__, |\___/ \___/ \__,_(_)
                  |___/

I don't know why it doesn't run the same command on the commit

jyggen commented 6 years ago

The pre-commit command, on most tasks, only checks the staged files that are about to be commited.

Sanguigui commented 6 years ago

Yes, I staged a file with obvious errors that you can see listed when I ran ./vendor/bin/grumphp run first. The diff is given to the pre-commit command but the phpcs command isn't run in this case.

jyggen commented 6 years ago

So what does git diff -r -p -m -M --full-index --staged output? Because, by the looks of it, the pre-commit command isn't even executing PHPCS due to no PHP files being staged (otherwise it would output the command being executed, as you can see in the run command output).

Sanguigui commented 6 years ago

git diff -r -p -m -M --full-index --staged outputs:

diff --git a/auraengine/app/ServiceHelpers/Exports/ClusterExport.php b/auraengine/app/ServiceHelpers/Exports/ClusterExport.php
index e29fff66f4d7b798dc5b4e9107989da2648e95b6..9fcc5ee37a0921b49208ed8537fc735727a7aaf2 100755
--- a/auraengine/app/ServiceHelpers/Exports/ClusterExport.php
+++ b/auraengine/app/ServiceHelpers/Exports/ClusterExport.php
@@ -46,5 +46,6 @@ class ClusterExport extends Base
                 }
             }
         )->download('xlsx');
+        echo 'sth';;;
     }
 }

And when I commit these changes I get:

GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/2: Phpcs... ✔
Running task 2/2: PhpMd... ✔
GrumPHP detected a commit-msg command.
GrumPHP is sniffing your code!
             ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
           ▄▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌           
         ▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
        ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
        ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
  ▄▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
 ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
 ▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
   ▀█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
     ▀▀▓▓▓▓▓▓▓▓▓▓▓▓█▀▀▀▀▀▀▀▀▀▀▀▀▀▀████████████▄
      ▄████████▀▀▀▀▀                 ██████████   
     ███████▀                         ██████▀
      ▐████      ██▌          ██       ████▌      
        ▐█▌                            ███        
         █▌           ▄▄ ▄▄           ▐███        
        ███       ▄▄▄▄▄▄▄▄▄▄▄▄       ▐███         
         ██▄ ▐███████████████████████████
        █▀█████████▌▀▀▀▀▀▀▀▀▀██████████▌▐         
          ███████████▄▄▄▄▄▄▄███████████▌          
         ▐█████████████████████████████           
          █████████████████████████████           
           ██ █████████████████████▐██▀           
            ▀ ▐███████████████████▌ ▐▀            
                ████▀████████▀▐███                
                 ▀█▌  ▐█████  ▐█▌                 
                        ██▀   ▐▀                  
       _    _ _                         _ _
      / \  | | |   __ _  ___   ___   __| | |
     / _ \ | | |  / _` |/ _ \ / _ \ / _` | |
    / ___ \| | | | (_| | (_) | (_) | (_| |_|
   /_/   \_\_|_|  \__, |\___/ \___/ \__,_(_)
                  |___/
veewee commented 6 years ago

Hi @Sanguigui,

As you can see in the -vvv log, no files are shown in the output of the git diff command:

[2018-08-21 09:15:37] GrumPHP.DEBUG: Repository created (git dir: "/var/www/auraengine.io/.git", working dir: "/var/www/auraengine.io") [] []
[2018-08-21 09:15:37] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2018-08-21 09:15:37] GrumPHP.DEBUG: last command (diff) duration: 47.46ms [] []
[2018-08-21 09:15:37] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2018-08-21 09:15:37] GrumPHP.DEBUG: last command (diff) output:  [] []

This means that the files you are trying to validate are not properly staged while running the command. The first line displays that .git is created in the repository. Is it possible that you are running those commands on a virtualized environment in which the .git files are not being synced?

Sanguigui commented 6 years ago

Hi @veewee ,

Yes I run grumPHP in a Vagrant box and my .git files are not in my synced folder. Is there a fix related to this type of configuration?

And what's happening when I run grumPHP in this type of configuration compared to a local project ?

veewee commented 6 years ago

Hi @Sanguigui,

Since this issue is related to your setup, fixing the issue is up to you.

GrumPHP requires the git repository to list the files and the changes. Those git commands are being used to run the tasks on. There are 2 options:

Sanguigui commented 6 years ago

Hi @veewee ,

After double checking with the team here's the configuration:

Here's the pre-commit git hook:

#!/bin/sh

#
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#

# Fetch the GIT diff and format it as command input:
DIFF=$(git -c diff.mnemonicprefix=false --no-pager diff -r -p -m -M --full-index --no-color --staged | cat)

# Output the DIFF
echo ${DIFF};

# Run GrumPHP
(cd "auraengine/" && printf "%s\n" "${DIFF}" | exec '/var/www/auraengine.io/auraengine/vendor/bin/grumphp' 'git:pre-commit' '--skip-success-output' '-vvv')

I added an echo and a -vvv to see what happens and as you can see below, the diff is given to grumphp when commiting inside the Vagrant box:

git add -A && git commit -m "test"
diff --git a/auraengine/app/Http/Controllers/Auth/test.php b/auraengine/app/Http/Controllers/Auth/test.php index 72e7dc2dd6c0bf8540e32ecc694c5a1e0a55624a..449c3893984f68fd97698fc2e9210cf3b16f7cbb 100644 --- a/auraengine/app/Http/Controllers/Auth/test.php +++ b/auraengine/app/Http/Controllers/Auth/test.php @@ -1,3 +1,3 @@ <?php -echo 'truc';;; \ No newline at end of file +echo 'truc';; \ No newline at end of file
[2018-09-04 06:00:09] GrumPHP.DEBUG: Repository created (git dir: "/var/www/auraengine.io/.git", working dir: "/var/www/auraengine.io") [] []
GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/1: Phpcs... ✔
GrumPHP detected a commit-msg command.
GrumPHP is sniffing your code!
             ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
           ▄▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌           
         ▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
        ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
        ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
  ▄▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌
 ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
 ▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
   ▀█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▌        
     ▀▀▓▓▓▓▓▓▓▓▓▓▓▓█▀▀▀▀▀▀▀▀▀▀▀▀▀▀████████████▄
      ▄████████▀▀▀▀▀                 ██████████   
     ███████▀                         ██████▀
      ▐████      ██▌          ██       ████▌      
        ▐█▌                            ███        
         █▌           ▄▄ ▄▄           ▐███        
        ███       ▄▄▄▄▄▄▄▄▄▄▄▄       ▐███         
         ██▄ ▐███████████████████████████
        █▀█████████▌▀▀▀▀▀▀▀▀▀██████████▌▐         
          ███████████▄▄▄▄▄▄▄███████████▌          
         ▐█████████████████████████████           
          █████████████████████████████           
           ██ █████████████████████▐██▀           
            ▀ ▐███████████████████▌ ▐▀            
                ████▀████████▀▐███                
                 ▀█▌  ▐█████  ▐█▌                 
                        ██▀   ▐▀                  
       _    _ _                         _ _
      / \  | | |   __ _  ___   ___   __| | |
     / _ \ | | |  / _` |/ _ \ / _ \ / _` | |
    / ___ \| | | | (_| | (_) | (_) | (_| |_|
   /_/   \_\_|_|  \__, |\___/ \___/ \__,_(_)
                  |___/
[feature/static-analysis-linting a89bab7a] test
 1 file changed, 1 insertion(+), 1 deletion(-)

So it looks like the diff is given ?