import * as path from 'node:path';
import * as fs from 'node:fs';
import { createRequire } from 'node:module';
import type { IProductConfiguration } from './vs/base/common/product.js';
const require = createRequire(import.meta.url);
const isWindows = process.platform === 'win32';
Error.stackTraceLimit = 100;
if (!process.env['VSCODE_HANDLES_SIGPIPE']) {
let didLogAboutSIGPIPE = false;
process.on('SIGPIPE', () => {
if (!didLogAboutSIGPIPE) {
didLogAboutSIGPIPE = true;
console.error(new Error(`Unexpected SIGPIPE`));
}
});
}
function setupCurrentWorkingDirectory(): void {
try {
if (typeof process.env['VSCODE_CWD'] !== 'string') {
process.env['VSCODE_CWD'] = process.cwd();
}
if (process.platform === 'win32') {
process.chdir(path.dirname(process.execPath));
}
} catch (err) {
console.error(err);
}
}
setupCurrentWorkingDirectory();
export function devInjectNodeModuleLookupPath(injectPath: string): void {
if (!process.env['VSCODE_DEV']) {
return;
}
if (!injectPath) {
throw new Error('Missing injectPath');
}
const Module = require('node:module');
Module.register('./bootstrap-import.js', { parentURL: import.meta.url, data: injectPath });
}
export function removeGlobalNodeJsModuleLookupPaths(): void {
if (typeof process?.versions?.electron === 'string') {
return;
}
const Module = require('module');
const globalPaths = Module.globalPaths;
const originalResolveLookupPaths = Module._resolveLookupPaths;
Module._resolveLookupPaths = function (moduleName: string, parent: any): string[] {
const paths = originalResolveLookupPaths(moduleName, parent);
if (Array.isArray(paths)) {
let commonSuffixLength = 0;
while (commonSuffixLength < paths.length && paths[paths.length - 1 - commonSuffixLength] === globalPaths[globalPaths.length - 1 - commonSuffixLength]) {
commonSuffixLength++;
}
return paths.slice(0, paths.length - commonSuffixLength);
}
return paths;
};
const originalNodeModulePaths = Module._nodeModulePaths;
Module._nodeModulePaths = function (from: string): string[] {
let paths: string[] = originalNodeModulePaths(from);
if (!isWindows) {
return paths;
}
const isDrive = (p: string) => p.length >= 3 && p.endsWith(':\\');
if (!isDrive(from)) {
paths = paths.filter(p => !isDrive(path.dirname(p)));
}
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
const userDir = path.dirname(path.join(process.env.HOMEDRIVE, process.env.HOMEPATH));
const isUsersDir = (p: string) => path.relative(p, userDir).length === 0;
if (!isUsersDir(from)) {
paths = paths.filter(p => !isUsersDir(path.dirname(p)));
}
}
return paths;
};
}
export function configurePortable(product: Partial<IProductConfiguration>): { portableDataPath: string; isPortable: boolean } {
const appRoot = path.dirname(import.meta.dirname);
function getApplicationPath(): string {
if (process.env['VSCODE_DEV']) {
return appRoot;
}
if (process.platform === 'darwin') {
return path.dirname(path.dirname(path.dirname(appRoot)));
}
return path.dirname(path.dirname(appRoot));
}
function getPortableDataPath(): string {
if (process.env['VSCODE_PORTABLE']) {
return process.env['VSCODE_PORTABLE'];
}
if (process.platform === 'win32' || process.platform === 'linux') {
return path.join(getApplicationPath(), 'data');
}
const portableDataName = product.portable || `${product.applicationName}-portable-data`;
return path.join(path.dirname(getApplicationPath()), portableDataName);
}
const portableDataPath = getPortableDataPath();
const isPortable = !('target' in product) && fs.existsSync(portableDataPath);
const portableTempPath = path.join(portableDataPath, 'tmp');
const isTempPortable = isPortable && fs.existsSync(portableTempPath);
if (isPortable) {
process.env['VSCODE_PORTABLE'] = portableDataPath;
} else {
delete process.env['VSCODE_PORTABLE'];
}
if (isTempPortable) {
if (process.platform === 'win32') {
process.env['TMP'] = portableTempPath;
process.env['TEMP'] = portableTempPath;
} else {
process.env['TMPDIR'] = portableTempPath;
}
}
return {
portableDataPath,
isPortable
};
}