Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate PHP in VS code using WSL php

In VS code you can validate php using php executable. But is there a way to use php installed on WSL instead?

like image 345
palci12 Avatar asked Sep 24 '16 15:09

palci12


People also ask

Can you use WSL in VS Code?

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.

What is php validate executablePath in VS Code?

php. validate. executablePath : points to the PHP executable on disk. Set this if the PHP executable is not on the system path.


2 Answers

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.

like image 52
Alexander Kim Avatar answered Oct 27 '22 20:10

Alexander Kim


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.

like image 3
Bonvi Avatar answered Oct 27 '22 21:10

Bonvi