Solving the ‘Retry as Admin’ while saving files in Visual Studio Code on a Mac

TL;DR

If you ever get an error that you can’t save a file in Visual Studio Code, and it prompts you to ‘Retry as Admin’ (where you then have to enter your admin password) then the solution is super easy by using the chown command.

Namely, you just have to run sudo chown -R $USER . in the folder where your file is located and that will ‘fix it’.

Yep, not much else to it really 💪

Why does this happen?

Now, this may have happened if you were using sudo when installing some Node.js packages with npm (or some other package manager), or if you created the folder (not sure why, but still plausible) using the sudo command.

A bit more about the chown command

The chown command in UNIX and Linux-based systems (including MacOS), stands for ‘change owner’ and it’s used to change the ownership of files and directories.

A detailed breakdown of the above command is as follows:

  • sudo: shorthand for ‘superuser do’; it grants administrative privileges for the command that follows. And yes, you’re right, you should use this command with caution
  • chown: the command to change file’s owner
    R: this option makes the command recursive, which means it will apply to all files and directories within the current directory
  • $USER: This is a variable that represents the current logged-in user (aka you). By using $USER, you’re changing the ownership of the files to yourself, eliminating the need for using sudo later
  • .: the dot represents the current directory. So, the command affects the files and directories where it’s executed

Thread with caution

It’s important to understand why you’re using chown, because incorrect usage can lead to permission issues or security risks. Generally, use it when you need to restore file ownership to the correct user after it’s been changed (often inadvertently). Be very vary of following tutorials (or any other instructions) that tell you to set the file permissions to anything else but you ($USER).

Learn even more about chown

If you want to learn more about the chown command and its options, you can run the man chown command in your terminal and you’ll get way more detailed documentation. Or, of course, just Google.

Hope this quick tip helps 🙌

Written by Nikola Brežnjak