Path: blob/main/src/vs/platform/environment/node/userDataPath.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { homedir } from 'os';6import { NativeParsedArgs } from '../common/argv.js';78// This file used to be a pure JS file and was always9// importing `path` from node.js even though we ship10// our own version of the library and prefer to use11// that.12// However, resolution of user-data-path is critical13// and while our version of `path` is a copy of node.js14// one, you never know. As such, preserve the use of15// the built-in `path` lib for the time being.16// eslint-disable-next-line local/code-import-patterns17import { resolve, isAbsolute, join } from 'path';1819const cwd = process.env['VSCODE_CWD'] || process.cwd();2021/**22* Returns the user data path to use with some rules:23* - respect portable mode24* - respect VSCODE_APPDATA environment variable25* - respect --user-data-dir CLI argument26*/27export function getUserDataPath(cliArgs: NativeParsedArgs, productName: string): string {28const userDataPath = doGetUserDataPath(cliArgs, productName);29const pathsToResolve = [userDataPath];3031// If the user-data-path is not absolute, make32// sure to resolve it against the passed in33// current working directory. We cannot use the34// node.js `path.resolve()` logic because it will35// not pick up our `VSCODE_CWD` environment variable36// (https://github.com/microsoft/vscode/issues/120269)37if (!isAbsolute(userDataPath)) {38pathsToResolve.unshift(cwd);39}4041return resolve(...pathsToResolve);42}4344function doGetUserDataPath(cliArgs: NativeParsedArgs, productName: string): string {4546// 0. Running out of sources has a fixed productName47if (process.env['VSCODE_DEV']) {48productName = 'code-oss-dev';49}5051// 1. Support portable mode52const portablePath = process.env['VSCODE_PORTABLE'];53if (portablePath) {54return join(portablePath, 'user-data');55}5657// 2. Support global VSCODE_APPDATA environment variable58let appDataPath = process.env['VSCODE_APPDATA'];59if (appDataPath) {60return join(appDataPath, productName);61}6263// With Electron>=13 --user-data-dir switch will be propagated to64// all processes https://github.com/electron/electron/blob/1897b14af36a02e9aa7e4d814159303441548251/shell/browser/electron_browser_client.cc#L546-L55365// Check VSCODE_PORTABLE and VSCODE_APPDATA before this case to get correct values.66// 3. Support explicit --user-data-dir67const cliPath = cliArgs['user-data-dir'];68if (cliPath) {69return cliPath;70}7172// 4. Otherwise check per platform73switch (process.platform) {74case 'win32':75appDataPath = process.env['APPDATA'];76if (!appDataPath) {77const userProfile = process.env['USERPROFILE'];78if (typeof userProfile !== 'string') {79throw new Error('Windows: Unexpected undefined %USERPROFILE% environment variable');80}8182appDataPath = join(userProfile, 'AppData', 'Roaming');83}84break;85case 'darwin':86appDataPath = join(homedir(), 'Library', 'Application Support');87break;88case 'linux':89appDataPath = process.env['XDG_CONFIG_HOME'] || join(homedir(), '.config');90break;91default:92throw new Error('Platform not supported');93}9495return join(appDataPath, productName);96}979899