ikappas / vscode-phpcs

PHP CodeSniffer for Visual Studio Code
MIT License
128 stars 56 forks source link

Error message "phpcs: Unexpected token F in JSON at position 0" on ignored files #93

Closed wongjn closed 6 years ago

wongjn commented 6 years ago

I'm getting this error message when I open some files that have been ignored via the phpcs.ignorePatterns setting and that have no code sniffing errors.

Extension version 1.0.2

ikappas commented 6 years ago

@wongjn Thank you for your feedback. Please specify the following information:

  1. PHP Version used.
  2. PHP_CodeSniffer version used.
  3. VSCode phpcs settings used for user, workspace and folder.
  4. Execute the phpcs command in your terminal with -q --report=json and check whether the output contains anything other than json.
wongjn commented 6 years ago

PHP 7.0.10 PHP_CodeSniffer 2.9.1 VS PHPCS settings (only globals set, otherwise default):

{
    "phpcs.standard": "Drupal,DrupalPractice",
    "phpcs.ignorePatterns": [
        "*/node_modules/*",
        "*/dist/*",
        "*/vendor/*",
        "*/public_html/core/*",
        "*/contrib/*"
    ]
}

Running a stripped-down equivalent command works fine for a file that when opened in VS, brings up the error:

> phpcs --standard="Drupal,DrupalPractice" --ignore="*/public_html/core/*" public_html/core/modules/node/src/Entity/NodeType.php -
q --report=json
{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{}}
ikappas commented 6 years ago

@wongjn Drupal and coding standards version?

ikappas commented 6 years ago

@wongjn Also please update vscode-phpcs to 1.0.4

wongjn commented 6 years ago

Updated extension. Drupal and DrupalPractice come from the drupal/coder composer package, version 8.2.12.

ikappas commented 6 years ago

@wongjn Do you think you can set up a reference repo to reproduce this issue? I don't follow your folder structure and I am not that familiar with Drupal 8.

wongjn commented 6 years ago

https://github.com/wongjn/repro

Reproduction steps:

  1. Run composer install
  2. Open public_html/core/modules/node/src/Entity/NodeType.php (as an example file that exhibits the erroneous behaviour).
ikappas commented 6 years ago

@wongjn Thank you for the reproduction repo. Following your instructions I am getting this error:

phpcs: Referenced sniff "../vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Array/DisallowLongArraySyntaxSniff.php" does not exist

ikappas commented 6 years ago

@wongjn Can you try the following configuration:

composer.json

{
    "name": "wongjn/repro",
    "require": {
        "composer/installers": "^1.2",
        "drupal/core": "^8.4",
        "neronmoon/scriptsdev": "0.1.*"
    },
    "require-dev": {
        "drupal/coder": "^8.2.12"
    },
    "extra": {
        "scripts-dev": {
            "post-install-cmd": [
                "\"vendor/bin/phpcs\" --config-set installed_paths vendor/drupal/coder/coder_sniffer"
            ],
            "post-update-cmd": [
                "\"vendor/bin/phpcs\" --config-set installed_paths vendor/drupal/coder/coder_sniffer"
            ]
        },
        "installer-paths": {
            "public_html/core": [
                "type:drupal-core"
            ]
        }
    }
}

.vscode/settings.json

{
    "phpcs.standard": "Drupal,DrupalPractice",
    "phpcs.autoConfigSearch": false,
    "phpcs.ignorePatterns": [
        "*/node_modules/*",
        "*/dist/*",
        "*/vendor/*",
        "*/public_html/core/*",
        "*/contrib/*"
    ]
}
wongjn commented 6 years ago

Not seeming to get the error anymore - so something to do with global vs local?

ikappas commented 6 years ago

Short answer yes.

I noticed that the phpc.autoConfigSearch picks up /public_html/core/phpcs.xml.dist which uses relative paths to itself whereas phpcs CWD the workspace root. I am not sure that this is relevant or a bug. Also I didn't test with your version of php (7.0.10) which is several versions behind latest (7.0.27)

ikappas commented 6 years ago

With phpcs.autoConfigSearch I get error:

phpcs: Uncaught PHP_CodeSniffer_Exception: Referenced sniff "../vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Array/DisallowLongArraySyntaxSniff.php"

Which is clear that PHP_Codesniffer doesn't resolve the ../vendor/... properly, probably due to the ' .' directive in 'phpcs.xml.dist'.

ikappas commented 6 years ago

@wongjn Can you report the output of the following command:

phpcs --standard=public_html/core/phpcs.xml.dist -q --encoding=UTF-8 --report=json --ignore="*/public_html/core/*" public_html/core/modules/node/src/Entity/NodeType.php
wongjn commented 6 years ago

Using original reproduction repo:

phpcs --standard=public_html/core/phpcs.xml.dist -q --encoding=UTF-8 --report=json --ignore="*/public_html/core/*" public_html/core/modules/node/src/Entity/NodeType.php

Fatal error: Uncaught PHP_CodeSniffer_Exception: Referenced sniff "../vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Array/DisallowLongArraySyntaxSniff.php" does not exist in C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer.php:1167
Stack trace:
#0 C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer.php(780): PHP_CodeSniffer->_expandRulesetReference(Object(SimpleXMLElement), 'C:\\Users\\justin...', 0)
#1 C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer.php(578): PHP_CodeSniffer->processRuleset('C:\\Users\\justin...')
#2 C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer\CLI.php(956): PHP_CodeSniffer->initStandard(Array, Array, Array)
#3 C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer\CLI.php(113): PHP_CodeSniffer_CLI->process()
#4 C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\scripts\phpcs(25 in C:\Users\justinwong\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer.php on line 1167

So it seems setting phpc.autoConfigSearch to false seems to be the fix?

ikappas commented 6 years ago

For your specific setup yes.

I would suggest you include a .vscode/settings.json in your project and use the suggested local composer configuration.

The issue is that PHP_CodeSniffer throws this error before processing the --ignore flag so it assumes that your --standard is valid.

ikappas commented 6 years ago

PS: I never got to see your reported error "phpcs: Unexpected token F in JSON at position 0" on ignored files using vscode-phpcs 1.0.4

wongjn commented 6 years ago

I got the updated error message you introduced when I updated to 1.0.4. Anyways, thank you for your time, happy to say that it is fixed.

ikappas commented 6 years ago

@wongjn You are welcome. I will see whether I should apply the ignorePatterns to the autoConfigSearch settings which would probably remove the necessity to turn it off.

ikappas commented 6 years ago

@wongjn I just released 1.0.5 which fixed ignorePatterns issues so they are taken into account for autoConfigSearch. So you can test out removing the autoConfigSearch setting (re-enabling it)