Permission issue with VSCode and WSL (code: command not found) - visual-studio-code

I am new to WSL and have installed VS code natively on Windows 10 with the installer. Currently, when I try to run VS Code command code from WSL, I receive the following message:
bash: /mnt/c/Users/user/AppData/Local/Programs/Microsoft VS Code/bin/code: Permission denied
Also, when I try to run VS Code Server for WSL, I am getting a similar message.
[2020-06-29 17:41:41.640] Launching C:\windows\System32\wsl.exe -d Ubuntu sh -c '"$VSCODE_WSL_EXT_LOCATION/scripts/wslServer.sh" cd9ea6488829f560dc949a8b2fb789f3cdc05f5d stable .vscode-server 0 ' in c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-wsl-0.44.4}
[2020-06-29 17:41:41.779] sh: 1: /mnt/c/Users/user/.vscode/extensions/ms-vscode-remote.remote-wsl-0.44.4/scripts/wslServer.sh: Permission denied
[2020-06-29 17:41:41.780] VS Code Server for WSL closed unexpectedly.
I have tried configuring the permissions for wsl # wsl.conf:
[automount]
enabled = true
options = "uid=1000,gid=1000,umask=022,fmask=11,metadata"
Not sure if this is relevant, but I am able to run code from Windows Powershell.
I am currently using Microsoft Windows 1909 (OS Build 18363.836) with WSL v1.
Any help will be appreciated, thanks in advance!

Run wsl.exe or PowerShell as an Administrator Runs as Admin Windows

None of the previous answers worked for me. Instead after a lot of trial and error, I discovered that the code.exe was set in a way that it was being run as Administrator. This is what prevented it from running in the WSL. Find the VC Code executable, and ensure it doesn't have the admin Shield on it's icon.

It has to do with your /etc/wsl.conf. Change the contents of the file to:
[automount]
enabled=true
root = /
options="metadata,uid=1000,gid=1000,umask=002,dmask=002,fmask=002"
umask,dmask, and fmask all have to do with permissions. A pretty good explanation of them can be found here : https://askubuntu.com/questions/429848/dmask-and-fmask-mount-options#:~:text=fmask%20and%20dmask%20are%20mount,files%20and%20dmask%20to%20directories

Maybe for someone will helpfull...
For example i have mistake like this:
$ code .
/c/Users/admin/AppData/Local/Programs/Microsoft VS Code/bin/code: line 61: /c/Users/admin/AppData/Local/Programs/Microsoft VS Code/Code.exe: Permission denied
My error resolving was by click right button mouse on icon VS Code and chosen settings then "compatibility" tab and finaly disabled "run the program as an administrator"

I solved it by putting my Ubuntu user in the www-data group. I edited the /etc/group. That www-data group is the one in my configuration belongs to Apache2. Hope this helps.

Related

WSL vscode command returning error "not found"

I have this message whenenver i try to type code .
Can someone help me please ?
Thanks in advance
Fixed it
Open VS Code on Windows
Open Extensions and then search on WSL
It should say the extension needs to be reloaded - go ahead and reload it
Open WSL and type code
This should force another upgrade of code and this time it should launch.
I just now had the same issue on WSL2 Kali. I tried everything given here: https://github.com/microsoft/vscode-remote-release/issues/2962.
My problem was VS Code was not on Kali's path. Here's how I fixed it :)
echo $PATH
# Confirm PATH is missing this:
/mnt/c/Users/wcd/AppData/Local/Programs/Microsoft VS Code/bin/
# Confirm VS Code's location then export:
export PATH=$PATH:"/mnt/c/Users/wcd/AppData/Local/Programs/Microsoft VS Code/bin/"
# It's temporarily added to path...
echo $PATH
# This should now trigger VS Code Server install,
# then open ~/ in VS Code.
code .
# If above works, make it permanent:
echo 'export PATH=$PATH:"/mnt/c/Users/wcd/AppData/Local/Programs/Microsoft VS Code/bin/"' >> ~/.bashrc
# Restart shell + test
exec "$SHELL"
code .
I solved this problem by opening windows powershell in admin mode and typing the following command:
wsl --update
Then I typed this command to force restart the WSL and all done
wsl --shutdown
I have same issue when I trying export my old wsl and move whole project to nvme ssd using new distro.
I able to run the code . on root , but when i switch to my name then it doesn't work, I guess is WSL PATH problem.
I not really familiar to the linux, the below image may easier for you to understand what i am saying.
When i use echo $PATH on wsl to compare root and my account, and i notice that VS code directory only found on root instead of my account.
So i use export PATH=$PATH:"/mnt/c/Users/ricso/AppData/Local/Programs/Microsoft VS Code/bin" to add the VS Code path to my account* and code . is working once i export this.
But the code . will not be able to run again if you restart the wsl, so we need to put in to bashrc.
But when i try to update my account's* PATH using
echo "PATH=$PATH:/mnt/c/Users/ricso/AppData/Local/Programs/Microsoft\ VS\ Code/bin" >> ~/.bashrc and restart the wsl to login my account, i notice an error msg popup when run the code .
cannot create /tmp/remote-wsl-loc.txt: Permission denied
And i found this.
https://github.com/microsoft/vscode-remote-release/issues/5389
Seem they only giving the workaround for now and hopefully can fix it soon.
*disclaimer above: this code need to run under your account instead of root
**For editing bashrc, instead of using echo and >> write to the file, you also can use code ~/.bashrc to update/edit, when you have the vscode access on wsl.
This also happens if you disable the Windows PATH on WSL. I had my VS Code working fine, then when I disable the windows PATH (by editing/creating the /etc/wsl.conf file and inserting the [interop] appendWindowsPath = false parameter) the code command stopped working.
The ek1n8 solution solves my problem, addind the export manually to the WSL Path:
export PATH=$PATH:"/mnt/c/Users/%USERNAME%/AppData/Local/Programs/Microsoft VS Code/bin"
For me adding the binary to Path was enough:
export PATH=$PATH:"/mnt/c/Users/%USERNAME%/AppData/Local/Programs/Microsoft VS Code/bin"
For me it was wsl using the wrong container:
wsl --list -v
NAME STATE VERSION
* docker-desktop-data Stopped 2
Ubuntu Running 2
I did wsl -s ubuntu
wsl --list -v
NAME STATE VERSION
* Ubuntu Running 2
docker-desktop-data Stopped 2
and it solved the problem

install vscode extension on WSL or EMR from the command line

I am trying to force install from the command line. Obviously, this is after opening WSL or ssh tunnel opened from VSCode Windows side. Tried below:
[hadoop#ip-172-31-82-184 bin]$ pwd
/home/hadoop/.vscode-server/bin/e5a624b788d92b8d34d1392e4c4d9789406efe8f/bin
[hadoop#ip-172-31-82-184 bin]$ ./code --install-extensions VisualStudioExptTeam.vscodeintellicode
./code: line 12: ./node: No such file or directory
Kind regards
Here's a simple answer. Yeh!
Just guessed & it was there all along. Auto installed by ssh-tool itself.

VSCode changes permission from 664 to 644 (and 775 to 755)

We have this problem using VSCode:
creating files using either GUI or console with VSCode gives them permission 644 (on directory it is 755)
creating files using Putty and by Windows CMD gives required permission 664 (and 775)
Considering this it seems like the problem is connected to VSCode and not the Linux settings.
We are using „Remote Development” extension (Remote – Containers / SSH / SSH: Editing Configuration Files / WSL also installed) to do this with VSCode.
System where the files are created:
Debian 9.12
Shell: bash 4.4.12
System where the VSCode is run:
Windows 10
version 1903
We have tried setting umask, but nothing changes.
Ok,
my friend found the solution (thank you too Abdullah Al Farooq - it was good starting point to where to look for - WSL indeed was the problem):
Solution:
run PowerShell (as admin)
type: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
restart system
This worked on two machines.
Link:
https://www.computerhope.com/issues/ch001879.htm
For me - it still was not working at this point. I had to make "clean instal" on VSCode because the umask was still setting to default 0022 every time.
Link:
https://www.logicbig.com/how-to/visual-studio-code/remove-vs-code-completely.html
After that - it works perfectly.

WSL2 terminal does not recognize Visual Studio Code

When I try to execute code from WSL, like this:
cmd.exe
wsl code .
I get the following error:
/bin/bash: code: command not found
However, if I execute the same command but first run cmd.exe as an Administrator, vscode opens as expected.
The question is why "code" is not recognizable when I run as non-admin, and how can I solve it?
Additional info:
WSL2 version 41959
vscode version 1.38
vscode Remote WSL extension version 0.39.5
As #Biswapriyo mentioned, this is an open bug where WSL cannot access Windows C drive.
Workaround that helped me is to restart WSL like this:
wsl --shutdown
wsl
Open the PowerShell administrator window
wsl --list --version
Show as wsl 2
Enter the Linux console
wsl
Change related files' rights
chmod u+x vscode_dir/code.exe
chmod u+x vscode_dir/bin/code
Create symbolic link
ln -s vscode_dir/bin/code code
mv code ~/.local/bin
Modify .bashrc, add a line as follows:
export PATH="$HOME/.local/bin:$PATH"
source .bashrc or restart terminal.
Then you can happily play code in wsl2-ubuntu environment or terminal of VSCode.
My problem was that I was using the root user after running sudo -i. Exiting and using my normal user on WSL solved the error.
In my case I am using Debian and echo %PATH% did not output anything.
I used the second part of suggested solution from resolved github issue
For Debian, /etc/profile contributed to this problem.
Here is the path definition in /etc/profile
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
Option 1:
You can delete above lines, then wsl --shutdown to restart Debian.
Option 2:
If you would like to keep these lines, you can also append ":$PATH" to each path like below, then wsl --shutdown
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:$PATH"
fi
Thank you to licanchua
I'd recommend checking /etc/wsl.conf to see if appendWindowsPath setting shares the Windows PATH with WSL, and also if it is specifically setting a particular user.
For the config settings, see https://learn.microsoft.com/en-us/windows/wsl/wsl-config

vscode-remote-release and wsl 2

Has anyone already tried vscode-remote-release with the pre release of wsl 2?
I am getting the following error when starting a new wsl server:
[2019-06-15 11:14:05.702] Starting VS Code Server inside WSL.
[2019-06-15 11:14:06.526] Launching C:\WINDOWS\System32\wsl.exe bash -c "./scripts/wslServer.sh 15b7c86d86319b187a5ef255c170184a8519c5af insider .vscode-server-insiders " in c:\Users\User Name\.vscode-insiders\extensions\ms-vscode-remote.remote-wsl-0.38.0
[2019-06-15 11:14:07.126] bash: ./scripts/wslServer.sh: No such file or directory
[2019-06-15 11:15:38.680] VS Code Server for WSL failed to start. No messages received for 90s
[2019-06-15 11:15:38.680] For help with startup problems, go to
[2019-06-15 11:15:38.680] https://code.visualstudio.com/docs/remote/troubleshooting#_wsl-tips
It seems wsl2 entry path problem, try execute wsl in any folder. If your shell enter your linux's user home and not the /mnt/ path to your windows's folder path.
Currently I found reboot can resolve this problem for a while