Can Someone Clarify to me if i can use my php in WSL ?
In principal yes. However you need to use the c:\windows\sysnative\bash.exe as the php.validate.executablePath and then use the php executable as a command line argument to bash. You might also want to consider passing -c so that the shell exists when the php executable has finished.
Following up on this since I cannot get it to work here using Windows Creators Update, WSL enabled, with latest VSCode. I have set php.validate.executablePath to C:\\Windows\\sysnative\\bash.exe, to C:\\Windows\\sysnative\\bash.exe -c, to C:\\Windows\\sysnative\\bash.exe -c php, and it does not work in VSCode. It either does not recognize the executable path, or it simply does not work. Do you have any further thoughts on this? I'd love to integrate VSCode with WSL php, but there seems to be a translation issue when VSCode tries to find binaries or tries to supply Windows C:\ style file paths into WSL.
I also tried using C:\\Users\\josh\\bin\\RunInBash.exe php, and that did not work. See:
https://neosmart.net/blog/2017/meet-your-new-best-friend-for-wsl/
@codeguy you are right. It doesn't work. I forgot to consider that we besides the pure executable pass additional command line arguments to the executable. These arguments make the bash start fail. To fix this we need to add an additional property to the configuration to request validation in a shell. Something like php.validate.inShell: Boolean | { executable: string; shellArgs: string[] }
@codeguy would you be willing to work on a PR for this. The code is here: https://github.com/Microsoft/vscode/blob/master/extensions/php/src/features/validationProvider.ts#L248
For sure! I'll fork and work on it later today.
@codeguy Great. Will look at the PR.
This is possible with an old school .bat :
php.bat
@echo off
c:\windows\sysnative\bash.exe -c "php %*"
setting.json
"php.validate.executablePath": "c:\\PATH_TO\\php.bat"
While I am happy to see that this problem is being addressed I managed to put together two workaround scripts as I could not get any of the scripts found online to work (like the one posted by @tooy).
This one is called php.bat and is placed 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.
@dbaeumer Is this still needed? Did you look at the PR?
@roblourens that slipped through the cracks. I will look at the PR but in general we should look into a more generic solution to WSL than this.
And what is about the PHP language server. Does that report errors and might make the start of the php executable obsolete ?
I don't believe any of the PHP intellisense extensions have done any work in particular to support WSL.
If this is hard to land, I won't push for it. I'd rather see the extensions implement it, but I don't see anyone filing issues on them for it.
Any news about this? Thanks.
No news yet on this particular item. But we started thinking about how we could better support WSL to make these errors be handled more generically than on a extension by extension base.
Any update? :)
@GioBonvi i've replaced the last line with
C:\Windows\System32\wsl.exe php %v_params%
So we can avoid using the second file.
@neyronius It works, thank you.
"php.validate.executablePath": "wsl php"
I was excited about @GioBonvi & @neyronius batch script fix for this until I tried it and god "Error spawning PHP: Command failed: C:\Dev\Scripts\wslphp.bat --version zsh:1: command not found: php" so I guess if you have zsh or something similar installed, this work-around won't work... Any news on official wsl php support?
@resonancedesigns
I was able to get @GioBonvi script working by using C:\Windows\System32\wsl.exe as @neyronius suggested. But I kept the second file. I am using zsh, but I do not get any errors.
I also added #!/bin/bash to the top of the windows-php script. Just one thing to note - I did change the path from C:\ to D:\ in the replacements.
The scripts are in D:\wsl-tools\ and windows-php is symlinked inside my /usr/local/bin.
windows-php -> /mnt/d/wsl-tools/windows-php
php.bat
@echo OFF
setlocal ENABLEDELAYEDEXPANSION
rem Collect the arguments and replace:
rem '\' with '/'
rem 'd:' with 'mnt/d'
rem '"' with '\"'
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:D:=/mnt/d%
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\system32\bash.exe -l -c "windows-php %v_params%"
windows-php
#!/bin/bash
# Pass all the arguments to PHP.
output=$(php "$@")
# Perform UNIX->WINDOWS syntax replacements.
output="${output//$'\n'/$'\r'$'\n'}"
output="${output//\/mnt\/d/D:}"
output="${output//\//\\}"
# Echo corrected output.
echo $output
And here are the settings inside VSCode:
"php.validate.executablePath": "d:\\wsl-tools\\php.bat",
这么久了,也没有官方的解决方案么。。。
@ianzhi 请看清楚 @GioBonvi 和 @tanmancan 的解决方案。。。
这么久了,也没有官方的解决方案么。。。
@ianzhi 请看清楚 @GioBonvi 和 @tanmancan 的解决方案。。。
ok,tks
We just announced remote development with VS Code, check out the blog post for details https://code.visualstudio.com/blogs/2019/05/02/remote-development
How does remote development solve this issue? What is the setting for php.validateexecutablePath now?
See some details about developing in WSL here: https://code.visualstudio.com/docs/remote/wsl
In that scenario, that setting would be the path to PHP on the WSL side.
See some details about developing in WSL here: https://code.visualstudio.com/docs/remote/wsl
In that scenario, that setting would be the path to PHP on the WSL side.
{
"php.validate.executablePath": "/usr/bin/php",
}
This doesn't work. Im sorry I am not very good at these things.
I run code-insiders from wsl and I have all the remote development extensions installed.
See some details about developing in WSL here: https://code.visualstudio.com/docs/remote/wsl
In that scenario, that setting would be the path to PHP on the WSL side.{ "php.validate.executablePath": "/usr/bin/php", } This doesn't work. Im sorry I am not very good at these things. I run code-insiders from wsl and I have all the remote development extensions installed.
I don't know why the people like to wrote very short words with link which is very long history.
The solution correctly is:
$ code-insiders <path> in the WSL bashNOW YOU CAN ENJOY YOUR VSCODE IN WSL
Most helpful comment
While I am happy to see that this problem is being addressed I managed to put together two workaround scripts as I could not get any of the scripts found online to work (like the one posted by @tooy).
This one is called
php.batand is placed inC:\wsl-tools\:This one is called
windows-phpand is placed somewhere in the WSL path (i chose/usr/local/bin).Setting
"php.validate.executablePath": "c:\\wsl-tools\\php.bat"works for me.