In VS code you can validate php using php executable. But is there a way to use php installed on WSL instead?
To get started with using WSL in VS Code, you'll need to download the WSL extension from the Extension Marketplace. You'll also need WSL and a Linux distribution installed. We recommend using WSL 2, which is the newest version of WSL, as you will benefit from significant performance advantages over WSL 1.
php. validate. executablePath : points to the PHP executable on disk. Set this if the PHP executable is not on the system path.
What worked for me:
Create php.bat file:
@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
set v_params=%v_params:"=\"%
@bash -c "php %v_params%"
put it in C:\WSL-Tools\php.bat
Then, in VScode settings (Ctrl+comma):
"php.validate.executablePath": "C:\\WSL-Tools\\php.bat"
And it works.
The other answer did not work for me neither: after some work I came up with these two scripts:
This one is called php.bat
and I placed it in C:\wsl-tools\
:
@echo OFF
setlocal ENABLEDELAYEDEXPANSION
rem Collect the arguments and replace:
rem '\' with '/'
rem 'c:' with 'mnt/c'
rem '"' with '\"'
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:C:=/mnt/c%
set v_params=%v_params%
set v_params=%v_params:"=\"%
rem Call the windows-php inside WSL.
rem windows-php is just a script which passes the arguments onto
rem the original php executable and converts its output from UNIX
rem syntax to Windows syntax.
C:\Windows\sysnative\bash.exe -l -c "windows-php %v_params%"
This one is called windows-php and is placed somewhere in the WSL path (i chose /usr/local/bin
).
# Pass all the arguments to PHP.
output=$(php "$@")
# Perform UNIX->WINDOWS syntax replacements.
output="${output//$'\n'/$'\r'$'\n'}"
output="${output//\/mnt\/c/C:}"
output="${output//\//\\}"
# Echo corrected output.
echo $output
Setting "php.validate.executablePath": "c:\\wsl-tools\\php.bat"
works for me.
Note:
You might want to follow this issue and this pull request as it looks like this problem is going to be officially addressed in one of the next releases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With