junstyle / vscode-php-cs-fixer

PHP CS Fixer extension for VS Code
https://marketplace.visualstudio.com/items?itemName=junstyle.php-cs-fixer
MIT License
354 stars 42 forks source link

Add options to choose which `php` binary for executing `php-cs-fixer` #153

Open mmn6d6d6e opened 3 years ago

mmn6d6d6e commented 3 years ago

Machine

OS Ubuntu 20.04.2 php -v -> php5.6 has php7.2 installed, binary on php7.2

Project

friendsofphp/php-cs-fixer on composer.json v3.0.0

Editor

extension v0.2.5 php-cs-fixer.executablePath set to ${workspaceFolder}/vendor/bin/php-cs-fixer

Issue

On formatting php document, it got error.

PHP needs to be a minimum version of PHP 7.1.3 and maximum version of PHP 8.0.*.Current PHP version: 5.6.40-52+ubuntu20.04.1+deb.sury.org+1.To ignore this requirement please set PHP_CS_FIXER_IGNORE_ENV.If you use PHP version higher than supported, you may experience code modified in a wrong way.Please report such cases at https://github.com/FriendsOfPHP/PHP-CS-Fixer . ...

Tested manually executing php7.2 ./vendor/bin/php-cs-fixer fix server.php, it ran fine.

Loaded config default.

Fixed all files in 0.127 seconds, 14.000 MB memory used

Tested manually executing php ./vendor/bin/php-cs-fixer fix server.php, it got similar error.

PHP needs to be a minimum version of PHP 7.1.3 and maximum version of PHP 8.0.*. Current PHP version: 5.6.40-52+ubuntu20.04.1+deb.sury.org+1. Ignoring environment requirements because PHP_CS_FIXER_IGNORE_ENV is set. Execution may be unstable. Composer detected issues in your platform:

Your Composer dependencies require a PHP version ">= 7.2.5". You are running 5.6.40-52+ubuntu20.04.1+deb.sury.org+1. ...

mmn6d6d6e commented 3 years ago

current workaround:

edit ./vendor/bin/php-cs-fixer, change #!/usr/bin/env php into #!/usr/bin/env php7.2

Hakihiro commented 2 years ago

This issue didn't receive an answer, but when you are working on multiple projects with multiple PHP versions it's a must-have.

junstyle commented 2 years ago

@Hakihiro add settings.json to .vscode folder

{
    "php-cs-fixer.executablePath": "/path/of/your/php/file",
}
Hakihiro commented 2 years ago

@Hakihiro add settings.json to .vscode folder

{
    "php-cs-fixer.executablePath": "/path/of/your/php/file",
}

php-cs-fixer.executablePath is a path to the php-cs-fixer file right? Not the path for the PHP version used by the php-cs-fixer file.

junstyle commented 2 years ago

@Hakihiro if you use phar, you can add settings.json to

{
    "php.validate.executablePath": "/path/of/your/php/file",
}

it will use this php binary to execute php-cs-fixer phar

Hakihiro commented 2 years ago

Good to know, but unfortunately this parameter is already used to specify the php-cs-fixer in each project under the vendor/bin/php-cs-fixer, so I can't have both?

mmn6d6d6e commented 2 years ago

@junstyle I've tried set php.validate.executablePath to point at correct php version, for the example /usr/bin/php7.2. but the extension still use the php, which in my case is php5.6.

I think this is because of the #!/usr/bin/env php on the first line of the ./vendor/bin/php-cs-fixer.

Because the extension execute the php-cs-fixer like this: ./vendor/bin/php-cs-fixer fix server.php, it uses the default interpreter set on the first line of the fixer, which is /usr/bin/env php, which in my case is php5.6.

If the fixer executed like this: php7.2 ./vendor/bin/php-cs-fixer fix server.php, it will use the php7.2 instead.

Unfortunately I cannot set the php-cs-fixer.executablePath to php7.2 php-cs-fixer because It'll throw an error: PHP CS Fixer: executablePath not found, please check your settings. It will set to build-in php-cs-fixer.phar. Try again!

jorismak commented 2 years ago

I create a file in vendor/bin in a project that I name 'my-php-cs-fixer' or something similar.

#!/bin/bash

basedir=$(dirname $0)
symfony php ${basedir}/php-cs-fixer $@

(remember to make it executable with chmod u+x my-php-cs-fixer or similar.)

In this case, it executes php-cs-fixer executable from the same folder as the file itself is in, but executes it as symfony php php-cs-fixer.

For people not knowing the symfony-cli, this takes the configured php-version for the project (through a .php-version file) and uses that as the php-interpreter.

For people not using symfony-cli, just change the symfony php part with something like php8.1 to use that interpreter.

If you then edit the vscode workspace settings to change the php-cs-fixer.executablePath key to ${workspaceFolder}/vendor/bin/my-php-cs-fixer, the formatter will use your custom executable.

mmn6d6d6e commented 2 years ago

@jorismak I found that directly modifying the content of the vendor folder isn't a good idea since it is typically put in the .gitignore. Because of that, it needs to be re-created every time the vendor folder is re-initiated for example when the project is freshly cloned.

So maybe the custom php-cs-fixer should be put outside the vendor folder or just on the root folder alongside composer.json or something like that. Then set php-cs-fixer.executablePath to pointing at it. That way both your and my customized php-cs-fixer will work.

But I wish there is a way without needing to create a custom script. Maybe just a setting on the plugin. Turning off the executable path checking so it can accept something like php7.4 php-cs-fixer might solve this.

ghost commented 2 years ago

shrug The vendor folder is not in your source repo normally , but this is also a local setup thing. So it's fine like this. I don't need to go bother my colleagues with custom scripts only i need since them with vscode , but they don't need it with phpstorm (just an example ).

The vscode workspace settings also are not in your repo .. local stuff shouldn't be there is the general idea.

And yes, it is meant as a workaround. A nice setting or something that would fix this would help. But I have the same issue with the phpstan extension IIRC.

BinaryAlan commented 1 year ago

I have multiple projects that require different versions of PHP. For the VS Code terminal, I added the following code in .zshrc file

PROJECT_BASHRC=$(pwd)/.project_bashrc
if [ -f $PROJECT_BASHRC ]; then
    source $PROJECT_BASHRC
fi

and then in the project's root directory, I added the following script to prepend PHP to the $PATH.

#!/bin/bash
export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"
export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"

This way, when I open different projects in VS Code, I can use the correct PHP version in the terminal. Regarding php-cs-fixer, is there a way to read PHP from the current $PATH variable?

junstyle commented 1 year ago

I have multiple projects that require different versions of PHP. For the VS Code terminal, I added the following code in .zshrc file

PROJECT_BASHRC=$(pwd)/.project_bashrc
if [ -f $PROJECT_BASHRC ]; then
    source $PROJECT_BASHRC
fi

and then in the project's root directory, I added the following script to prepend PHP to the $PATH.

#!/bin/bash
export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"
export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"

This way, when I open different projects in VS Code, I can use the correct PHP version in the terminal. Regarding php-cs-fixer, is there a way to read PHP from the current $PATH variable?

if you used phar version, you can add setting in file .vscode/settings.json "php.validate.executablePath": "/php/executable/path/...."

if you used from compose, you can write a shell to execute the php-cs-fixer, and set the "php-cs-fixer.executablePath": "the/path/of/the/shell/file"

BinaryAlan commented 1 year ago

I have multiple projects that require different versions of PHP. For the VS Code terminal, I added the following code in .zshrc file

PROJECT_BASHRC=$(pwd)/.project_bashrc
if [ -f $PROJECT_BASHRC ]; then
    source $PROJECT_BASHRC
fi

and then in the project's root directory, I added the following script to prepend PHP to the $PATH.

#!/bin/bash
export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"
export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"

This way, when I open different projects in VS Code, I can use the correct PHP version in the terminal. Regarding php-cs-fixer, is there a way to read PHP from the current $PATH variable?

if you used phar version, you can add setting in file .vscode/settings.json "php.validate.executablePath": "/php/executable/path/...."

if you used from compose, you can write a shell to execute the php-cs-fixer, and set the "php-cs-fixer.executablePath": "the/path/of/the/shell/file"

The settings.json file is a shared project configuration, but since each person has a different PHP path, it's not suitable to add it there. In the example I provided, .zshrc is for personal computer configuration, and .project_bashrc (similar to .env) is not added to the version control repository. This allows each member of our team to have their own configuration. Is there a good method for configuring php-cs-fixer in VS Code?

jorismak commented 1 year ago

Almost all vscode settings - including phpcsfixer - can be set in the workspace settings.json file, so every user can make their own overrides per project .

On Fri, Jul 21, 2023, 07:51 Binary Alan @.***> wrote:

I have multiple projects that require different versions of PHP. For the VS Code terminal, I added the following code in .zshrc file

PROJECT_BASHRC=$(pwd)/.project_bashrcif [ -f $PROJECT_BASHRC ]; then source $PROJECT_BASHRCfi

and then in the project's root directory, I added the following script to prepend PHP to the $PATH.

!/bin/bashexport @./bin:$PATH"export @./sbin:$PATH"

This way, when I open different projects in VS Code, I can use the correct PHP version in the terminal. Regarding php-cs-fixer, is there a way to read PHP from the current $PATH variable?

if you used phar version, you can add setting in file .vscode/settings.json "php.validate.executablePath": "/php/executable/path/...."

if you used from compose, you can write a shell to execute the php-cs-fixer, and set the "php-cs-fixer.executablePath": "the/path/of/the/shell/file"

The settings.json file is a shared project configuration, but since each person has a different PHP path, it's not suitable to add it there. In the example I provided, .zshrc is for personal computer configuration, and .project_bashrc (similar to .env) is not added to the version control repository. This allows each member of our team to have their own configuration. Is there a good method for configuring php-cs-fixer in VS Code?

— Reply to this email directly, view it on GitHub https://github.com/junstyle/vscode-php-cs-fixer/issues/153#issuecomment-1645018510, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALMD2DXKF3ZUSPFIRUIGII3XRIKHBANCNFSM5BMMOTJQ . You are receiving this because you were mentioned.Message ID: @.***>

boumanb commented 5 months ago

In my .vscode/php-cs-fixer executable:

command="warden env exec php-fpm vendor/bin/php-cs-fixer "$@""
$command

Running it:

❯ ./.vscode/php-cs-fixer
PHP CS Fixer 3.22.0 Chips & Pizza by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.2.15

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  describe     Describe rule / ruleset.
  fix          Fixes a directory or a file.
  help         Display help for a command
  list         List commands
  list-files   List all files being fixed by the given config.
  list-sets    List all available RuleSets.
  self-update  [selfupdate] Update php-cs-fixer.phar to the latest stable version.

In my .vscode/settings.json:

{
    "php-cs-fixer.executablePath": "${workspaceFolder}/.vscode/php-cs-fixer",
    "php-cs-fixer.config": "${workspaceFolder}/.php-cs-fixer.php",
}

Output when running fix on a file:

[Extension Host]% (6) ['fix', '--using-cache=no', '--format=json', '--rules=@PSR12', '--path-mode=override', '/var/folders/lf/km_cd9nx2hz1dr_xp0x_q7pc0000gn/T/pcf-tmp0.27587822908092563/DatabaseSeeder.php']
console.ts:137 [Extension Host] Error: spawn Unknown system error -8
    at ChildProcess.spawn (node:internal/child_process:413:11)
    at spawn (node:child_process:795:9)
    at Rn (/Users/user/.vscode/extensions/junstyle.php-cs-fixer-0.3.13/index.js:76:34872)
    at /Users/user/.vscode/extensions/junstyle.php-cs-fixer-0.3.13/index.js:76:39035
    at new Promise (<anonymous>)
    at Dn.format (/Users/user/.vscode/extensions/junstyle.php-cs-fixer-0.3.13/index.js:76:39015)
    at /Users/user/.vscode/extensions/junstyle.php-cs-fixer-0.3.13/index.js:80:490
    at new Promise (<anonymous>)
    at Dn.formattingProvider (/Users/user/.vscode/extensions/junstyle.php-cs-fixer-0.3.13/index.js:80:276)
    at Object.provideDocumentFormattingEdits (/Users/user/.vscode/extensions/junstyle.php-cs-fixer-0.3.13/index.js:81:2081)
    at j.provideDocumentFormattingEdits (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:150:101999)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:150:127097
    at Oe.s (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:150:121610)
    at Oe.$provideDocumentFormattingEdits (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:150:127084)
    at s.S (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:147:5519)
    at s.Q (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:147:5285)
    at s.M (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:147:4337)
    at s.L (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:147:3454)
    at w.value (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:147:2241)
    at n.y (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:80:1902)
    at n.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:80:2119)
    at r.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:105:14091)
    at w.value (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:173:8050)
    at n.y (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:80:1902)
    at n.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:80:2119)
    at r.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:105:14091)
    at MessagePortMain.<anonymous> (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:173:6330)
    at MessagePortMain.emit (node:events:514:28)
    at Object.emit (node:electron/js2c/utility_init:2:2285)
log.ts:437   ERR undefined

Did someone get it to work using a binary that points towards the php-cs-fixer executable inside a container?

mmn6d6d6e commented 5 months ago

@boumanb i think you should use php instead of php-fpm

boumanb commented 5 months ago

@boumanb i think you should use php instead of php-fpm

I had to add a shebang to the executable.

#!/bin/zsh

command="/opt/homebrew/bin/warden env exec php-fpm vendor/bin/php-cs-fixer "$@""
eval $command

Now I'm getting the following error:

[
  "fix",
  "--using-cache=no",
  "--format=json",
  "--rules=@PSR12",
  "--path-mode=override",
  "/var/folders/lf/km_cd9nx2hz1dr_xp0x_q7pc0000gn/T/pcf-tmp0.6886828952386743/DatabaseSeeder.php"
]

In ConfigurationResolver.php line 391:

  The path "/var/folders/lf/km_cd9nx2hz1dr_xp0x_q7pc0000gn/T/pcf-tmp0.6886828  
  952386743/DatabaseSeeder.php" is not readable.                               

fix [--path-mode PATH-MODE] [--allow-risky ALLOW-RISKY] [--config CONFIG] [--dry-run] [--rules RULES] [--using-cache USING-CACHE] [--cache-file CACHE-FILE] [--diff] [--format FORMAT] [--stop-on-violation] [--show-progress SHOW-PROGRESS] [--] [<path>...]