Since sudo
-ing VS Code at command-line launch is only a thing on Linux, this question assumes you're on Linux, and restricts its context to Linux.
TL;DR
To answer your question directly: the user-data-dir
parameter points to a folder where all personalisation except extensions resides — unique to each user.
Why does sudo
-ing Code need --user-data-dir
?
In typical installations of either OS and VS Code, this folder owned by the regular user cannot be accessed by the superuser.
Hence a VS Code session running with effective UID=0 tries but fails to write to the invoking user's (not the superuser's) config folder. This is what the error message prevents from happening, by forcing the user to provide an explicit root
-accessible folder.
Detailed Explanation
There are two main folders that VS Code uses to store configuration data:
- An extensions folder (self explanatory) — contained in
~/.vscode
[1]
user-data-dir
; a folder for all other personalisable things (settings, keybindings, GitHub/MS account credential caches, themes, tasks.json
, you name it)[2]
On Linux the latter is located in ~/.config/Code
, and has file permissions mode 0700
(unreadable and unwritable by anybody other than the owner).
This causes issues, as VS Code tries and fails to access said directory. The logical solution is to either modify the permissions (recursively) of ~/.config/Code
to allow root
access, or — arguably saner and objectively more privacy-respecting — to use a separate directory altogether for the sudo
'ed VS Code to access.
The latter strategy is what the community decided to adopt at large; this commit from 2016 started making it compulsory to pass an explicit --user-data-dir
when sudo
-ing VS Code on Linux.
Should You be Doing This in the First Place?
Probably not! If your goal is to modify system config files, then you could stick to an un-elevated instance of Code, which would prompt you to Save as Admin... when you try to save. See this answer on Ask Ubuntu on why you probably want to avoid elevating VS Code without reason (unless you understand the risks and/or have to), and this one on the same thread on what you could do instead.
However, if the concerned file is read-restricted to root
as well, as in the O.P’s case, then you hardly have a choice ; sudo
away!
[1] & [2]: If you want to know more about the above two folder paths on different OSes, see [1] and [2]
Hope this was helpful!