7

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.

reducing activity
  • 1,985
  • 2
  • 36
  • 64
  • I think for "mortal" user, it would default to: `~/.vscode`, as "root" you'd have to specify it accordingly ... [see also](https://askubuntu.com/q/803343/707903) – xerx593 Dec 20 '21 at 09:52
  • VSC saves some stuff for each Workspace/Folder it has opened, it also needs a place to store the extensions you have installed (maybe a different directory argument) – rioV8 Dec 20 '21 at 10:47

2 Answers2

8

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[1]
  2. 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!

mavenor
  • 168
  • 8
  • "Save as Admin" is not solving problem of failing to open such file. Though "The absolute best thing to do to is make a copy of a restricted file, edit it, and copy it back when you're absolutely sure it's finished." is probably a superior solution. – reducing activity Dec 22 '21 at 19:58
  • 1
    Ahhh, so the system config file concerned here is also read-restricted? Now I see why you're going through all this xD :/ – mavenor Dec 23 '21 at 03:19
  • Changed my answer as well to reflect the possibility of read-restricted-to-root files :) – mavenor Dec 23 '21 at 03:27
  • FYI, on macOS the default `user-data-dir` is **`$HOME/Library/Application Support/Code`** – ryenus Jan 11 '23 at 10:30
  • That’s true! A valuable addition to those reading this thread, perhaps. Thanks @ryenus! Didn’t include that info in this answer though, since running the `code` tool using `sudo` on macOS doesn’t seem to launch it as root at all, as of this comment – mavenor Jan 12 '23 at 19:31
0

It might be helpful to easily find the default location of the user-data-dir on any OS. It can be found with this command:

Developer: Open User Data Folder
workbench.action.openUserDataFolder

which is in the Insiders Build v1.75 now, Stable soon. Opens your OS file explorer app to the location.

Mark
  • 143,421
  • 24
  • 428
  • 436