Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/bootstrap-node.ts
3285 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as path from 'node:path';
7
import * as fs from 'node:fs';
8
import { createRequire } from 'node:module';
9
import type { IProductConfiguration } from './vs/base/common/product.js';
10
11
const require = createRequire(import.meta.url);
12
const isWindows = process.platform === 'win32';
13
14
// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
15
Error.stackTraceLimit = 100;
16
17
if (!process.env['VSCODE_HANDLES_SIGPIPE']) {
18
// Workaround for Electron not installing a handler to ignore SIGPIPE
19
// (https://github.com/electron/electron/issues/13254)
20
let didLogAboutSIGPIPE = false;
21
process.on('SIGPIPE', () => {
22
// See https://github.com/microsoft/vscode-remote-release/issues/6543
23
// In certain situations, the console itself can be in a broken pipe state
24
// so logging SIGPIPE to the console will cause an infinite async loop
25
if (!didLogAboutSIGPIPE) {
26
didLogAboutSIGPIPE = true;
27
console.error(new Error(`Unexpected SIGPIPE`));
28
}
29
});
30
}
31
32
// Setup current working directory in all our node & electron processes
33
// - Windows: call `process.chdir()` to always set application folder as cwd
34
// - all OS: store the `process.cwd()` inside `VSCODE_CWD` for consistent lookups
35
function setupCurrentWorkingDirectory(): void {
36
try {
37
38
// Store the `process.cwd()` inside `VSCODE_CWD`
39
// for consistent lookups, but make sure to only
40
// do this once unless defined already from e.g.
41
// a parent process.
42
if (typeof process.env['VSCODE_CWD'] !== 'string') {
43
process.env['VSCODE_CWD'] = process.cwd();
44
}
45
46
// Windows: always set application folder as current working dir
47
if (process.platform === 'win32') {
48
process.chdir(path.dirname(process.execPath));
49
}
50
} catch (err) {
51
console.error(err);
52
}
53
}
54
55
setupCurrentWorkingDirectory();
56
57
/**
58
* Add support for redirecting the loading of node modules
59
*
60
* Note: only applies when running out of sources.
61
*/
62
export function devInjectNodeModuleLookupPath(injectPath: string): void {
63
if (!process.env['VSCODE_DEV']) {
64
return; // only applies running out of sources
65
}
66
67
if (!injectPath) {
68
throw new Error('Missing injectPath');
69
}
70
71
// register a loader hook
72
const Module = require('node:module');
73
Module.register('./bootstrap-import.js', { parentURL: import.meta.url, data: injectPath });
74
}
75
76
export function removeGlobalNodeJsModuleLookupPaths(): void {
77
if (typeof process?.versions?.electron === 'string') {
78
return; // Electron disables global search paths in https://github.com/electron/electron/blob/3186c2f0efa92d275dc3d57b5a14a60ed3846b0e/shell/common/node_bindings.cc#L653
79
}
80
81
const Module = require('module');
82
const globalPaths = Module.globalPaths;
83
84
const originalResolveLookupPaths = Module._resolveLookupPaths;
85
86
Module._resolveLookupPaths = function (moduleName: string, parent: any): string[] {
87
const paths = originalResolveLookupPaths(moduleName, parent);
88
if (Array.isArray(paths)) {
89
let commonSuffixLength = 0;
90
while (commonSuffixLength < paths.length && paths[paths.length - 1 - commonSuffixLength] === globalPaths[globalPaths.length - 1 - commonSuffixLength]) {
91
commonSuffixLength++;
92
}
93
94
return paths.slice(0, paths.length - commonSuffixLength);
95
}
96
97
return paths;
98
};
99
100
const originalNodeModulePaths = Module._nodeModulePaths;
101
Module._nodeModulePaths = function (from: string): string[] {
102
let paths: string[] = originalNodeModulePaths(from);
103
if (!isWindows) {
104
return paths;
105
}
106
107
// On Windows, remove drive(s) and users' home directory from search paths,
108
// UNLESS 'from' is explicitly set to one of those.
109
const isDrive = (p: string) => p.length >= 3 && p.endsWith(':\\');
110
111
if (!isDrive(from)) {
112
paths = paths.filter(p => !isDrive(path.dirname(p)));
113
}
114
115
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
116
const userDir = path.dirname(path.join(process.env.HOMEDRIVE, process.env.HOMEPATH));
117
118
const isUsersDir = (p: string) => path.relative(p, userDir).length === 0;
119
120
// Check if 'from' is the same as 'userDir'
121
if (!isUsersDir(from)) {
122
paths = paths.filter(p => !isUsersDir(path.dirname(p)));
123
}
124
}
125
126
return paths;
127
};
128
}
129
130
/**
131
* Helper to enable portable mode.
132
*/
133
export function configurePortable(product: Partial<IProductConfiguration>): { portableDataPath: string; isPortable: boolean } {
134
const appRoot = path.dirname(import.meta.dirname);
135
136
function getApplicationPath(): string {
137
if (process.env['VSCODE_DEV']) {
138
return appRoot;
139
}
140
141
if (process.platform === 'darwin') {
142
return path.dirname(path.dirname(path.dirname(appRoot)));
143
}
144
145
return path.dirname(path.dirname(appRoot));
146
}
147
148
function getPortableDataPath(): string {
149
if (process.env['VSCODE_PORTABLE']) {
150
return process.env['VSCODE_PORTABLE'];
151
}
152
153
if (process.platform === 'win32' || process.platform === 'linux') {
154
return path.join(getApplicationPath(), 'data');
155
}
156
157
const portableDataName = product.portable || `${product.applicationName}-portable-data`;
158
return path.join(path.dirname(getApplicationPath()), portableDataName);
159
}
160
161
const portableDataPath = getPortableDataPath();
162
const isPortable = !('target' in product) && fs.existsSync(portableDataPath);
163
const portableTempPath = path.join(portableDataPath, 'tmp');
164
const isTempPortable = isPortable && fs.existsSync(portableTempPath);
165
166
if (isPortable) {
167
process.env['VSCODE_PORTABLE'] = portableDataPath;
168
} else {
169
delete process.env['VSCODE_PORTABLE'];
170
}
171
172
if (isTempPortable) {
173
if (process.platform === 'win32') {
174
process.env['TMP'] = portableTempPath;
175
process.env['TEMP'] = portableTempPath;
176
} else {
177
process.env['TMPDIR'] = portableTempPath;
178
}
179
}
180
181
return {
182
portableDataPath,
183
isPortable
184
};
185
}
186
187