Skip to content

Commit 835893e

Browse files
committed
Fix relative user data dirs on Windows
Fixes microsoft#11735
1 parent 788caba commit 835893e

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ function getNLSConfiguration() {
111111
return resolvedLocale ? resolvedLocale : { locale: initialLocale, availableLanguages: {} };
112112
}
113113

114+
// Set userData path before app 'ready' event and call to process.chdir
115+
var userData = path.resolve(args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
116+
app.setPath('userData', userData);
117+
114118
// Update cwd based on environment and platform
115119
try {
116120
if (process.platform === 'win32') {
@@ -123,10 +127,6 @@ try {
123127
console.error(err);
124128
}
125129

126-
// Set userData path before app 'ready' event
127-
var userData = path.resolve(args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
128-
app.setPath('userData', userData);
129-
130130
// Mac: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
131131
// the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
132132
global.macOpenFiles = [];

src/vs/platform/environment/node/environmentService.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class EnvironmentService implements IEnvironmentService {
6565
get userHome(): string { return path.join(os.homedir(), product.dataFolderName); }
6666

6767
@memoize
68-
get userDataPath(): string { return path.resolve(this._args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform)); }
68+
get userDataPath(): string { return parseUserDataDir(this._args, process); }
6969

7070
@memoize
7171
get appSettingsHome(): string { return path.join(this.userDataPath, 'User'); }
@@ -110,4 +110,19 @@ export function parseExtensionHostPort(args: ParsedArgs, isBuild: boolean): { po
110110
const port = Number(portStr) || (!isBuild ? 5870 : null);
111111
const brk = port ? Boolean(!!args.debugBrkPluginHost) : false;
112112
return { port, break: brk };
113+
}
114+
115+
export function parseUserDataDir(args: ParsedArgs, process: NodeJS.Process) {
116+
const arg = args['user-data-dir'];
117+
if (arg) {
118+
// Determine if the arg is relative or absolute, if relative use the original CWD
119+
// (VSCODE_CWD), not the potentially overridden one (process.cwd()).
120+
const resolved = path.resolve(arg);
121+
if (path.normalize(arg) === resolved) {
122+
return resolved;
123+
} else {
124+
return path.resolve(process.env['VSCODE_CWD'] || process.cwd(), arg);
125+
}
126+
}
127+
return path.resolve(paths.getDefaultUserDataPath(process.platform));
113128
}

src/vs/platform/environment/test/node/environmentService.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
'use strict';
66

77
import * as assert from 'assert';
8+
import * as path from 'path';
89
import { parseArgs } from 'vs/platform/environment/node/argv';
9-
import { parseExtensionHostPort } from 'vs/platform/environment/node/environmentService';
10+
import { parseExtensionHostPort, parseUserDataDir } from 'vs/platform/environment/node/environmentService';
1011

1112
suite('EnvironmentService', () => {
1213

@@ -31,4 +32,13 @@ suite('EnvironmentService', () => {
3132
assert.deepEqual(parse(['--debugBrkPluginHost=5678']), { port: 5678, break: true });
3233
assert.deepEqual(parse(['--debugPluginHost=1234', '--debugBrkPluginHost=5678']), { port: 5678, break: true });
3334
});
35+
36+
test('userDataPath', () => {
37+
const parse = (a, b: { cwd: () => string, env: { [key: string]: string }}) => parseUserDataDir(parseArgs(a), <any>b);
38+
39+
assert.equal(parse(['--user-data-dir', './dir'], { cwd: () => '/foo', env: {} }), path.resolve('/foo/dir'),
40+
'should use cwd when --user-data-dir is specified');
41+
assert.equal(parse(['--user-data-dir', './dir'], { cwd: () => '/foo', env: {'VSCODE_CWD': '/bar'} }), path.resolve('/bar/dir'),
42+
'should use VSCODE_CWD as the cwd when --user-data-dir is specified');
43+
});
3444
});

0 commit comments

Comments
 (0)