0

I am still able to launch files with the code command, but if I try using sudo along with it I get sudo: code: command not found. It worked fine in the past, not sure how long it's been broken for me. It was nice being able to edit .rc files in code instead of nano, but I need root privileges to save those files.

I have tried uninstalling/reinstalling the WSL extensions in VSC, adding export PATH="/usr/share/code/bin:$PATH" in my .zshrc, and adding new aliases per this guide.

Stephan
  • 345
  • 2
  • 14
  • 1
    Does this answer your question? [VSCode in WSL: how to sudo a root file so I can edit it](https://stackoverflow.com/questions/58980356/vscode-in-wsl-how-to-sudo-a-root-file-so-i-can-edit-it). Perhaps you used one of those methods in the past? Unfortunately, Windows applications *always* (well, almost always) run under the "default user" in WSL. The only exception that I've found is when the WSL distribution is first run after installation (the "default user" is still, apparently, root). From the answers in that question, I tend to favor `sudo -e ` after setting the `VISUAL` variable. – NotTheDr01ds Jan 08 '23 at 17:48
  • I couldn't get this to work, but I used `sudo chown -R $USER:$USER /path/to/group` so I won't need to add `sudo` to the `code` command anymore for my files to save using vs code. – Stephan Jan 15 '23 at 19:58

1 Answers1

2

sudo likely resets your environment including PATH for safety purposes (I believe this is default on Ubuntu and maybe other distros). Even if you extend PATH to include VSCode in your .zshrc it will be removed by using sudo. To verify this you can do sudo zsh and then type echo $PATH.

To keep environment you can either use sudo -E switch:

-E, --preserve-env
             Indicates to the security policy that the user wishes to preserve their
             existing environment variables.  The security policy may return an error
             if the user does not have

or run visudo and add following configuration to your sudoers file that will make it default behavior limited to PATH environment variable:

Defaults env_keep += "PATH"
blami
  • 6,588
  • 2
  • 23
  • 31