Search code examples
linuxvisual-studio-codeparameters

VSCode - what exactly --user-data-dir is specifiying


What exactly is --user-data-dir specifiying?

From --help parameter:

--user-data-dir <dir> Specifies the directory that user data is kept in. Can be used to open multiple distinct instances of Code.

Is it storing some temporary files there?

Is it about the access path to config files?

I am asking as I want to run VSCode (or Codium to be more exact) with sudo (I want to edit system config file that is read restricted) which requires this parameter for reasons unclear to me.


Solution

  • 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:

    1. An extensions folder (self explanatory) — contained in ~/.vscode*
    2. user-data-dir; a folder for all other personalisable things (settings, keybindings, GitHub/MS account credential caches, themes, tasks.json, you name it)*

    On Linux the latter is located in ~/.config/Code, and has file permissions mode 0700 (unreadable and unwritable by anybody other than the owner).[1]

    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! 😀

    *If you want to know more about the above two folder paths on different OSes, see [2] and [3]

    Hope this was helpful!