WSL环境下GrumPHP调用php-cs-fixer路径及版本异常求助
问题描述
在Laravel 9项目中使用GrumPHP时,php-cs-fixer被错误调用Windows路径下的可执行文件,而非WSL目录中的实际文件,报错信息如下:
phpcsfixerPHP needs to be a minimum version of PHP 7.1.0 and maximum version of PHP 7.4.*.
You can fix errors by running the following command:
'/windir/f/wamp64/vendor/bin//php-cs-fixer' '--config=./home/testuser/php-cs-config.php_cs' '--verbose' 'fix'
已将php-cs-fixer升级至v3.10版本,且在grumphp.yml中配置了GRUMPHP_BIN_DIR,但问题未解决,当前配置如下:
grumphp: environment: variables: GRUMPHP_PROJECT_DIR: "." GRUMPHP_BIN_DIR: "./home/testuser/tools/vendor/bin/" paths: - './home/plustime/tools' tasks: phpcsfixer: config: "./home/testuser/php-cs-config.php_cs" allow_risky: ~ cache_file: ~ rules: [] using_cache: ~ config_contains_finder: true verbose: true diff: false triggered_by: ['php']
解决方案
1. 直接指定php-cs-fixer的绝对路径
在grumphp.yml的phpcsfixer任务中添加executable参数,明确指向WSL环境中php-cs-fixer的实际绝对路径:
grumphp: # 其他配置保持不变 tasks: phpcsfixer: executable: "/home/testuser/tools/vendor/bin/php-cs-fixer" config: "./home/testuser/php-cs-config.php_cs" # 其余任务配置保持不变
2. 修正GRUMPHP_BIN_DIR的路径格式
将GRUMPHP_BIN_DIR改为WSL的绝对路径,而非相对路径,避免被解析为Windows下的路径:
grumphp: environment: variables: GRUMPHP_PROJECT_DIR: "." GRUMPHP_BIN_DIR: "/home/testuser/tools/vendor/bin/" # 其余环境配置保持不变
3. 调整WSL环境变量优先级
在WSL终端中执行which php-cs-fixer,确认输出为WSL下的正确路径。随后检查环境变量PATH,确保Windows相关的bin目录(如/mnt/c/Windows/System32等)没有排在WSL的vendor/bin路径之前,防止GrumPHP优先找到Windows版本的php-cs-fixer。
4. 安装项目本地版php-cs-fixer
直接在Laravel项目根目录执行以下命令,将php-cs-fixer安装到项目本地依赖中:
composer require --dev friendsofphp/php-cs-fixer
此时GrumPHP会自动优先调用项目根目录下vendor/bin/php-cs-fixer,无需额外配置路径。
内容的提问来源于stack exchange,提问作者Second2None

