ObliviousHarmony / vscode-php-codesniffer

A VS Code extension for integrating PHP_CodeSniffer.
https://marketplace.visualstudio.com/items?itemName=obliviousharmony.vscode-php-codesniffer
Other
38 stars 2 forks source link

Format Document On Excluded Files Erases Content #46

Closed kallookoo closed 2 years ago

kallookoo commented 2 years ago

Description

When format document or selection is executed returns an empty document.

System info: php: 7.4
phpcs: 3.7.1 installed globally with WooCommerce Coding Standard

Reproduction Steps

  1. Define the custom standard
  2. Open document and validate on save.
  3. Open context menu and click format document or format selection
  4. After executated the document is empty

Expected Behavior

It should correct errors like spaces, etc... according to the defined standard.

ObliviousHarmony commented 2 years ago

Thanks for the report @kallookoo, I'll take a look this weekend!

ObliviousHarmony commented 2 years ago

Interesting @kallookoo, I can't seem to replicate this error. I did however, find a different one and opened an issue for it here.

In order to track this issue down, could you do a few things for me?

As a final confirmation, by "the document is empty" do you mean that it literally deletes your entire document?

Thanks!

kallookoo commented 2 years ago

Hi @ObliviousHarmony

  • Make sure you're using the PHP_CodeSniffer formatted. You can also use "Format Document With...` and try that. I want to make sure it's this extension that's breaking.

Yeah, I am using it.

  • What operating system are you using? If Windows, are you using WSL2?

I use a mac with the Monterey version

  • Could you share all of your phpCodeSniffer configurations? User, workspace, anything else that might apply?

Yes,

Code settings:

{
    "phpCodeSniffer.lintAction": "Save",
    "phpCodeSniffer.standard": "Custom",
    "phpCodeSniffer.standardCustom": "/Users/sergio/Documents/ruleset.xml",
    "[php]": {
        "editor.defaultFormatter": "obliviousharmony.vscode-php-codesniffer"
    }
}

ruleset.xml

<?xml version="1.0"?>
<ruleset name="WooCommerce-WordPress">
    <description>WooCommerce and WordPress</description>

    <!-- Only use for PHP files -->
    <arg name="extensions" value="php"/>

    <config name="testVersion" value="7.4-"/>
    <config name="minimum_supported_wp_version" value="5.7"/>

    <!-- Exclude directories. -->
    <exclude-pattern>^(.+?/wp-(admin|includes)/.+?)</exclude-pattern>
    <exclude-pattern>^(.+?/vendor/.+?)</exclude-pattern>
    <exclude-pattern>^(.+?/node_modules/.+?)</exclude-pattern>

    <rule ref="WooCommerce-Core">
        <exclude name="WooCommerce.Commenting.CommentHooks" />
    </rule>
</ruleset>

composer.json, It is global, I do not install it per project.

{
    "require": {
        "dealerdirect/phpcodesniffer-composer-installer": "*",
        "phpcompatibility/phpcompatibility-wp": "*",
        "woocommerce/woocommerce-sniffs": "*",
        "roave/security-advisories": "dev-master"
    },
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true
        }
    }
}

As a final confirmation, by "the document is empty" do you mean that it literally deletes your entire document?

Yes. and by the way I also realized that it only happens when the document is in the phpcs exclude-pattern and you run the formatter manually, which is how I usually do it when they are single files.

ObliviousHarmony commented 2 years ago

I also realized that it only happens when the document is in the phpcs exclude-pattern and you run the formatter manually, which is how I usually do it when they are single files.

Thanks @kallookoo, this seems to be the bug. When a file is in exclude-pattern, PHPCS will refuse to process it. Since we're expecting the output from PHPCS to be the new file content, it erases the entire file. I'll add some resiliency here so that it doesn't erase the file in this case.

As for the workflow itself, this actually isn't supported by phpcs. In my testing, I have woocommerce/woocommerce cloned and run phpcs plugins/woocommerce/includes/libraries/class-wc-eval-math.php.

ObliviousHarmony commented 2 years ago

Hey @kallookoo,

I've shipped a fix for the document erasing bug in version 1.7.0.