environment - consistently handle VSCODE_CWD in all our node.js processes

This commit is contained in:
Benjamin Pasero
2021-03-15 11:22:42 +01:00
parent 56ee90e32e
commit 1441ba91f0
15 changed files with 92 additions and 71 deletions

35
src/bootstrap-node.js vendored
View File

@@ -6,6 +6,41 @@
//@ts-check
'use strict';
// Setup current working directory in all our node & electron processes
// - Windows: call `process.chdir()` to always set application folder as cwd
// - Posix: allow to change the current working dir via `VSCODE_CWD` if defined
// - all OS: store the `process.cwd()` inside `VSCODE_CWD` for consistent lookups
function setupCurrentWorkingDirectory() {
const path = require('path');
try {
let cwd = process.env['VSCODE_CWD'];
// remember current working directory in environment
// unless it was given to us already from outside
if (typeof cwd !== 'string') {
cwd = process.cwd();
process.env['VSCODE_CWD'] = cwd;
}
// Windows: always set application folder as current working dir
if (process.platform === 'win32') {
process.chdir(path.dirname(process.execPath));
}
// Linux/macOS: allow to change current working dir based on env
else {
if (cwd !== process.cwd()) {
process.chdir(cwd);
}
}
} catch (err) {
console.error(err);
}
}
setupCurrentWorkingDirectory();
/**
* Add support for redirecting the loading of node modules
*