Skip to content

Commit e9ce823

Browse files
authored
Fix potential race creating JS/TS temp dir (microsoft#203367)
Fixes microsoft#203335
1 parent 4be04d5 commit e9ce823

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

extensions/typescript-language-features/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ export function activate(
8888
}
8989

9090
export function deactivate() {
91-
fs.rmSync(temp.getInstanceTempDir(), { recursive: true, force: true });
91+
fs.rmSync(temp.instanceTempDir.value, { recursive: true, force: true });
9292
}

extensions/typescript-language-features/src/utils/temp.electron.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as fs from 'fs';
77
import * as os from 'os';
88
import * as path from 'path';
9+
import { lazy } from './lazy';
910

1011
function makeRandomHexString(length: number): string {
1112
const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
@@ -17,31 +18,17 @@ function makeRandomHexString(length: number): string {
1718
return result;
1819
}
1920

20-
const getRootTempDir = (() => {
21-
let dir: string | undefined;
22-
return () => {
23-
if (!dir) {
24-
const filename = `vscode-typescript${process.platform !== 'win32' && process.getuid ? process.getuid() : ''}`;
25-
dir = path.join(os.tmpdir(), filename);
26-
}
27-
if (!fs.existsSync(dir)) {
28-
fs.mkdirSync(dir);
29-
}
30-
return dir;
31-
};
32-
})();
21+
const rootTempDir = lazy(() => {
22+
const filename = `vscode-typescript${process.platform !== 'win32' && process.getuid ? process.getuid() : ''}`;
23+
return path.join(os.tmpdir(), filename);
24+
});
3325

34-
export const getInstanceTempDir = (() => {
35-
let dir: string | undefined;
36-
return () => {
37-
dir ??= path.join(getRootTempDir(), makeRandomHexString(20));
38-
if (!fs.existsSync(dir)) {
39-
fs.mkdirSync(dir);
40-
}
41-
return dir;
42-
};
43-
})();
26+
export const instanceTempDir = lazy(() => {
27+
const dir = path.join(rootTempDir.value, makeRandomHexString(20));
28+
fs.mkdirSync(dir, { recursive: true });
29+
return dir;
30+
});
4431

4532
export function getTempFile(prefix: string): string {
46-
return path.join(getInstanceTempDir(), `${prefix}-${makeRandomHexString(20)}.tmp`);
33+
return path.join(instanceTempDir.value, `${prefix}-${makeRandomHexString(20)}.tmp`);
4734
}

0 commit comments

Comments
 (0)