Skip to content

Commit aeaae53

Browse files
committed
Move VS Code test isolates to user cache
1 parent b02a2f8 commit aeaae53

4 files changed

Lines changed: 119 additions & 8 deletions

File tree

Extension/.scripts/vscode.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
* ------------------------------------------------------------------------------------------ */
55

66
import { downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } from '@vscode/test-electron';
7-
import { createHash } from 'crypto';
8-
import { tmpdir } from 'os';
97
import { resolve } from 'path';
108
import { verbose } from '../src/Utility/Text/streams';
119
import { mkdir, readJson, rimraf, write } from './common';
10+
import { getVSCodeTestIsolate } from './vscodeTestPath';
1211

13-
export const isolated = resolve(tmpdir(), '.vscode-test', createHash('sha256').update(__dirname).digest('hex').substring(0, 6));
12+
export const isolated = getVSCodeTestIsolate(__dirname);
1413
export const extensionsDir = resolve(isolated, 'extensions');
1514
export const userDir = resolve(isolated, 'user-data');
1615
export const settings = resolve(userDir, "User", 'settings.json');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All Rights Reserved.
3+
* See 'LICENSE' in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
import { createHash } from 'crypto';
7+
import { homedir } from 'os';
8+
import { posix, win32 } from 'path';
9+
10+
export function getVSCodeTestIsolate(
11+
scriptDirectory: string,
12+
platform: NodeJS.Platform = process.platform,
13+
environment: NodeJS.ProcessEnv = process.env,
14+
homeDirectory: string = homedir()): string {
15+
const path = platform === 'win32' ? win32 : posix;
16+
const override = environment.CPPTOOLS_VSCODE_TEST_ROOT;
17+
let root: string;
18+
19+
if (override) {
20+
root = override;
21+
} else {
22+
switch (platform) {
23+
case 'win32':
24+
root = path.resolve(environment.LOCALAPPDATA || path.resolve(homeDirectory, 'AppData', 'Local'), 'Microsoft', 'vscode-cpptools', 'vscode-test');
25+
break;
26+
case 'darwin':
27+
root = path.resolve(homeDirectory, 'Library', 'Caches', 'vscode-cpptools', 'vscode-test');
28+
break;
29+
default:
30+
root = path.resolve(environment.XDG_CACHE_HOME || path.resolve(homeDirectory, '.cache'), 'vscode-cpptools', 'vscode-test');
31+
break;
32+
}
33+
}
34+
35+
const worktreeHash = createHash('sha256').update(scriptDirectory).digest('hex').substring(0, 6);
36+
return path.resolve(root, worktreeHash);
37+
}

Extension/readme.developer.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,30 @@ The scripts for this repository now support running VS Code and the extension in
8181
completely isolated environment (separate install of VS Code, private extensions and
8282
user folders, etc).
8383

84-
The scripts that install VS Code place it in a `$ENV:TMP/.vscode-test/<UID>` folder where
85-
`<UID>` is a has calculated from the extension folder (this permits multiple checkouts of
86-
the source repository and each gets it's own isolated environment).
84+
The scripts that install VS Code retain it in a per-user cache directory:
85+
86+
* Windows: `%LOCALAPPDATA%/Microsoft/vscode-cpptools/vscode-test/<UID>` (or
87+
`~/AppData/Local/Microsoft/vscode-cpptools/vscode-test/<UID>` if `%LOCALAPPDATA%` is unavailable)
88+
* macOS: `~/Library/Caches/vscode-cpptools/vscode-test/<UID>`
89+
* Linux: `${XDG_CACHE_HOME:-~/.cache}/vscode-cpptools/vscode-test/<UID>`
90+
91+
`<UID>` is a six-character hash calculated from the extension folder. This permits multiple
92+
checkouts of the source repository, with each checkout retaining its own isolated `cache`,
93+
`extensions`, and `user-data` folders across runs. Set `CPPTOOLS_VSCODE_TEST_ROOT` to an absolute
94+
directory to override the platform-specific `vscode-test` root; the checkout-specific `<UID>` is
95+
still appended to the override.
8796

8897
The [`test scripts`](#yarn-test) will automatically install and use this isolated environment.
8998

9099
You can invoke VS Code from the command line using the [`yarn code`](#yarn-code) script.
91100

92101
If you want to remove the isolate environment use the `yarn code reset` or `yarn test reset` scripts
93-
to delete the folders and remove all of the configuration files. Next time you use the `yarn test` or
94-
`yarn code` commands, it will reinstall a fresh isolated environment.
102+
to delete only the current checkout's hashed folder and remove all of its configuration files. Next
103+
time you use the `yarn test` or `yarn code` commands, it will reinstall a fresh isolated environment.
104+
105+
Isolates created by earlier versions under the system temporary directory are not migrated or
106+
removed automatically. After ensuring that no test runs are using them, you can remove the old
107+
`.vscode-test` folder from the system temporary directory once to reclaim that space.
95108

96109
The Isolated environment has the theme automatically set to blue so that it is visually distinct from
97110
your normal VS Code environment.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All Rights Reserved.
3+
* See 'LICENSE' in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
import * as assert from 'assert';
7+
import { createHash } from 'crypto';
8+
import { describe, it } from 'mocha';
9+
import { posix, win32 } from 'path';
10+
import { getVSCodeTestIsolate } from '../../.scripts/vscodeTestPath';
11+
12+
const posixScriptDirectory = '/worktrees/agent/Extension/.scripts';
13+
const windowsScriptDirectory = 'C:\\worktrees\\agent\\Extension\\.scripts';
14+
15+
function getWorktreeHash(scriptDirectory: string): string {
16+
return createHash('sha256').update(scriptDirectory).digest('hex').substring(0, 6);
17+
}
18+
19+
describe('VS Code test isolate path', () => {
20+
it('uses XDG_CACHE_HOME on Linux', () => {
21+
assert.strictEqual(
22+
getVSCodeTestIsolate(posixScriptDirectory, 'linux', { XDG_CACHE_HOME: '/cache' }, '/home/developer'),
23+
posix.resolve('/cache', 'vscode-cpptools', 'vscode-test', getWorktreeHash(posixScriptDirectory)));
24+
});
25+
26+
it('falls back to the user cache directory on Linux', () => {
27+
assert.strictEqual(
28+
getVSCodeTestIsolate(posixScriptDirectory, 'linux', {}, '/home/developer'),
29+
posix.resolve('/home/developer', '.cache', 'vscode-cpptools', 'vscode-test', getWorktreeHash(posixScriptDirectory)));
30+
});
31+
32+
it('uses the user cache directory on macOS', () => {
33+
assert.strictEqual(
34+
getVSCodeTestIsolate(posixScriptDirectory, 'darwin', {}, '/Users/developer'),
35+
posix.resolve('/Users/developer', 'Library', 'Caches', 'vscode-cpptools', 'vscode-test', getWorktreeHash(posixScriptDirectory)));
36+
});
37+
38+
it('uses LOCALAPPDATA on Windows', () => {
39+
assert.strictEqual(
40+
getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { LOCALAPPDATA: 'D:\\LocalAppData' }, 'C:\\Users\\developer'),
41+
win32.resolve('D:\\LocalAppData', 'Microsoft', 'vscode-cpptools', 'vscode-test', getWorktreeHash(windowsScriptDirectory)));
42+
});
43+
44+
it('falls back to the user profile on Windows', () => {
45+
assert.strictEqual(
46+
getVSCodeTestIsolate(windowsScriptDirectory, 'win32', {}, 'C:\\Users\\developer'),
47+
win32.resolve('C:\\Users\\developer', 'AppData', 'Local', 'Microsoft', 'vscode-cpptools', 'vscode-test', getWorktreeHash(windowsScriptDirectory)));
48+
});
49+
50+
it('honors CPPTOOLS_VSCODE_TEST_ROOT without sharing worktree isolates', () => {
51+
const environment = { CPPTOOLS_VSCODE_TEST_ROOT: '/test-root', XDG_CACHE_HOME: '/cache' };
52+
const first = getVSCodeTestIsolate('/worktrees/first/Extension/.scripts', 'linux', environment, '/home/developer');
53+
const firstAgain = getVSCodeTestIsolate('/worktrees/first/Extension/.scripts', 'linux', environment, '/home/developer');
54+
const second = getVSCodeTestIsolate('/worktrees/second/Extension/.scripts', 'linux', environment, '/home/developer');
55+
56+
assert.strictEqual(first, firstAgain);
57+
assert.match(posix.basename(first), /^[0-9a-f]{6}$/);
58+
assert.notStrictEqual(first, second);
59+
assert.strictEqual(posix.dirname(first), '/test-root');
60+
assert.strictEqual(posix.dirname(second), '/test-root');
61+
});
62+
});

0 commit comments

Comments
 (0)