palci12
palci12

Reputation: 319

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?

Upvotes: 16

Views: 12814

Answers (6)

Alejandro Mosca
Alejandro Mosca

Reputation: 1

"php.validate.executablePath": "\\wsl.localhost\\Ubuntu\\usr\\bin\\php"

Upvotes: 0

DauntlessRob
DauntlessRob

Reputation: 795

This is an old question, but I have my own answer that may help someone coming in looking for this.

I run my vscode out of the WSL instance to begin with, so I just needed to install PHP in that instance:

sudo apt-get install php

with that, i could find where it installed it with a simple:

which php

which showed me it installed to the default location:

/usr/bin/php

When putting that into the php.validate.executablePath, everything worked smoothly again.

Hope this helps someone.

Upvotes: 1

Muhammad Zubair Saleem
Muhammad Zubair Saleem

Reputation: 517

If I'm not too late this is the way to go with ubuntu 20.04 WSL 2

@echo OFF
setlocal ENABLEDELAYEDEXPANSION

rem Collect the arguments and replace:
rem  '\' with '/'
rem  'c:' with 'mnt/c'
rem  '"' with '\"'
set v_params=%*
IF DEFINED 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.
@wsl.exe -d Ubuntu-20.04 -- php %v_params%

By Placing this php.bat in

c:/wsl-plugins/

You don't need to do WSL bit.

Upvotes: 2

Alexander Kim
Alexander Kim

Reputation: 18411

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.

Upvotes: 13

Bonvi
Bonvi

Reputation: 53

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.

Upvotes: 3

tooy
tooy

Reputation: 393

This is possible with an old school batch-file.

php.bat

@echo off
c:\windows\sysnative\bash.exe -c "php %*"`

setting.json

"php.validate.executablePath": "c:\\PATH_TO\\php.bat"

Upvotes: 3

Related Questions