vscode-php-cs-fixer icon indicating copy to clipboard operation
vscode-php-cs-fixer copied to clipboard

Extension does not work with php-vs-fixer v3.88.1 and above

Open MoritzLost opened this issue 4 months ago • 1 comments

When formatting any PHP file that is in any directory except the project root, the formatter doesn't work and throws an error. In the output tab, I can see the error:

In ComposerJsonReader.php line 69:
                                        
  Failed to read file "composer.json".  

I think the problem is the introduction of @auto rulesets in php-cs-fixer v3.88.0. Though the problem only manifests in 3.88.1 and above, looks like this one turned on the @auto ruleset by default.

This feature tries to read the composer.json in my project to determine some rules dynamically. The problem is that the extension seems to always set the cwd to the directory of the file being edited:

runAsync: spawn /Users/moritz/projects/myproject/vendor/bin/php-cs-fixer
[
  "fix",
  "--using-cache=no",
  "--format=json",
  "--config=/Users/moritz/projects/myproject/.php-cs-fixer.php",
  "--path-mode=override",
  "/var/folders/t7/y6pmk9ts1hj4pkbl4msm10mw0000gn/T/pcf-tmp0.8335527005692916/SiteModule.php"
]
{
  "cwd": "/Users/moritz/projects/myproject/modules/Site",
  "shell": false
}

In the case above, I'm formatting a file modules/Site/SiteModule.php. The cwd is set to /Users/moritz/projects/myproject/modules/Site. Looks like the ComposerJsonReader just tries to read the composer.json from the current working directory: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/src/ComposerJsonReader.php#L62-L78

This fails, so we get an error.

I think to fix this, the extension needs to always set the working directory to the project/workspace root, instead of the path of the file being formatted.

I haven't found a workaround except downgrading. I've tried setting @auto and @auto:risky to false in my .php-cs-fixer.php config, but that doesn't help.

MoritzLost avatar Oct 27 '25 16:10 MoritzLost

I have an issue that could be relevant. When I run the formatter on save, I get the following warning:

Unable to determine minimum supported PHP version from composer.json: Failed to read file "composer.json".

However the ./vendor/bin/php-cs-fixer fix does not return any error.

I have the following settings:

  "php-cs-fixer.executablePath": "${workspaceFolder}/vendor/bin/php-cs-fixer",
  "php-cs-fixer.onsave": true,

festo avatar Nov 12 '25 14:11 festo