Fix potential race creating JS/TS temp dir (#203367)

Fixes #203335
This commit is contained in:
Matt Bierner 2024-01-29 17:24:41 +00:00 committed by GitHub
parent 4be04d5d8c
commit e9ce823bf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 25 deletions

View File

@ -88,5 +88,5 @@ export function activate(
}
export function deactivate() {
fs.rmSync(temp.getInstanceTempDir(), { recursive: true, force: true });
fs.rmSync(temp.instanceTempDir.value, { recursive: true, force: true });
}

View File

@ -6,6 +6,7 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { lazy } from './lazy';
function makeRandomHexString(length: number): string {
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 {
return result;
}
const getRootTempDir = (() => {
let dir: string | undefined;
return () => {
if (!dir) {
const filename = `vscode-typescript${process.platform !== 'win32' && process.getuid ? process.getuid() : ''}`;
dir = path.join(os.tmpdir(), filename);
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
};
})();
const rootTempDir = lazy(() => {
const filename = `vscode-typescript${process.platform !== 'win32' && process.getuid ? process.getuid() : ''}`;
return path.join(os.tmpdir(), filename);
});
export const getInstanceTempDir = (() => {
let dir: string | undefined;
return () => {
dir ??= path.join(getRootTempDir(), makeRandomHexString(20));
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
};
})();
export const instanceTempDir = lazy(() => {
const dir = path.join(rootTempDir.value, makeRandomHexString(20));
fs.mkdirSync(dir, { recursive: true });
return dir;
});
export function getTempFile(prefix: string): string {
return path.join(getInstanceTempDir(), `${prefix}-${makeRandomHexString(20)}.tmp`);
return path.join(instanceTempDir.value, `${prefix}-${makeRandomHexString(20)}.tmp`);
}