VScode unable to save files inside my WSL2 home folder

Viewed 31900

I'm trying to make WSL2 work for my web projects development and this is driving me crazy!

Basically, i have two options. 1. Save my project files into c drive and access those with WSL (which makes the responses extremelly slow) 2. save my project files into /home/ which makes the project run super smooth, but i'm unable to edit those files with VSCode.

The error that is what follows:

Failed to save 'DefaultSeeder.php': Unable to write file 'vscode-remote://wsl+ubuntu-20.04/home/lucas/Projetos/API/src/database/seeds/DefaultSeeder.php' (NoPermissions (FileSystemError): Error: EACCES: permission denied, open '/home/lucas/Projetos/API/src/database/seeds/DefaultSeeder.php')
5 Answers

Well, turns out I was being stupid and posted a stupid question.

After many hours trying to make this damn thing work, I was able to do so, by setting the ownership of the folder to my user.

Here is the shell snippet to change the ownership to the current logged in user:

$ sudo find /~~folder-path~~/ -type d -user root -exec sudo chown -R $USER: {} +~

This solved it for me

sudo find /home/ -type d -user root -exec sudo chown -R $USER: {}  \;

I had the same problem and spent hours to find this working solution.
For me, it seems to be right one because it doesn't produce problems with 1. too generous file permissions sent to git afterwards and 2. change ubuntu config to fit windows environment (coupling to windows user).

I found the source of this solution on github. Thanks to saltazaur! https://github.com/microsoft/WSL/issues/4260#issuecomment-729594527

Steps to solve the problem:

  • Reset (or reinstall) the Ubuntu distro via Windows "Programs and Features"
    Note: If you have data in your home directory already, copy it to windows before
  • Open Ubuntu & follow initial setup (create user)
  • Add file "/etc/wsl.conf" with:
cd /etc
sudo touch wsl.conf
sudo nano wsl.conf
  • copy and paste the content from microsoft docs
  • save with CTRL+X > "Y" > ENTER
  • restart wsl (or windows, to be sure ;))

At next startup of ubuntu, the settings in wsl.conf will be applied.

The important part in this case is the following line:

options = "metadata,umask=22,fmask=11"

It sets, that all newly created files will use umask 22 (chmod 775) and fmask 11 (chmod 644)

This settings also applies for IDEs like VSCode & PHPStorm.
Happy coding - finally! ^^

In my case, I cannot edit/delete any file directly by File Explorer too, not just VSCode.

After opening Ubuntu 22.04.1 LTS on Windows, run

sudo chown -R my_username my_folder

Note the my_username is the username in the Ubuntu.

This solves my issue.

Related