{{ message }}
Repository navigation
Expand file tree
/
Copy pathbootstrap-node.ts
More file actions
262 lines (217 loc) · 8.81 KB
/
Copy pathbootstrap-node.ts
File metadata and controls
262 lines (217 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'node:path';
import * as fs from 'node:fs';
import { Buffer } from 'node:buffer';
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';
// Avoid 64 KiB pooled backing stores crossing Mojo's shared-memory threshold.
if (process.platform === 'linux') {
Buffer.poolSize = 8 * 1024;
}
// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
Error.stackTraceLimit = 100;
if (!process.env['VSCODE_HANDLES_SIGPIPE']) {
// Workaround for Electron not installing a handler to ignore SIGPIPE
// (https://github.com/electron/electron/issues/13254)
let didLogAboutSIGPIPE = false;
process.on('SIGPIPE', () => {
// See https://github.com/microsoft/vscode-remote-release/issues/6543
// In certain situations, the console itself can be in a broken pipe state
// so logging SIGPIPE to the console will cause an infinite async loop
if (!didLogAboutSIGPIPE) {
didLogAboutSIGPIPE = true;
console.error(new Error(`Unexpected SIGPIPE`));
}
});
}
// Setup current working directory in all our node & electron processes
// - Windows: call `process.chdir()` to always set application folder as cwd
// - all OS: store the `process.cwd()` inside `VSCODE_CWD` for consistent lookups
function setupCurrentWorkingDirectory(): void {
try {
// Store the `process.cwd()` inside `VSCODE_CWD`
// for consistent lookups, but make sure to only
// do this once unless defined already from e.g.
// a parent process.
if (typeof process.env['VSCODE_CWD'] !== 'string') {
process.env['VSCODE_CWD'] = process.cwd();
}
// Windows: always set application folder as current working dir
if (process.platform === 'win32') {
process.chdir(path.dirname(process.execPath));
}
} catch (err) {
console.error(err);
}
}
setupCurrentWorkingDirectory();
/**
* Add ASAR support to Node's CommonJS module resolution.
*
* Production builds bundle our `node_modules` into a `node_modules.asar`
* archive that sits next to the (now mostly empty) `node_modules` folder.
* Node does not look into `.asar` archives on its own, so we splice the
* archive into the lookup paths right before the real `node_modules` folder.
*
* The archive keeps the same top-level layout as `node_modules`
* (`node_modules.asar/<module>`), so bare `require('<module>')` calls resolve
* exactly like they did before ASAR was introduced. This keeps extensions and
* tooling that reach into `${appRoot}/node_modules.asar/<module>` working.
*
* Note: only applies to the packaged app running on Electron (incl.
* `ELECTRON_RUN_AS_NODE` forks), never when running out of sources.
*/
function enableASARSupport(): void {
if (!process.env['ELECTRON_RUN_AS_NODE'] && !process.versions['electron']) {
return; // only on Electron / Electron-as-node
}
if (process.env['VSCODE_DEV']) {
return; // no ASAR when running out of sources
}
// Normalize the drive letter to lower-case for comparison. On Windows the
// path derived from `import.meta.dirname` (a file URL) can use a different
// drive-letter case than the paths Node computes for a `require` parent, so
// an exact string comparison would miss the insertion point (breaking e.g.
// `require('mkdirp')` from a module inside the archive).
const normalizeDriveLetter = (p: string): string => {
if (isWindows && p.length >= 2 && p.charCodeAt(1) === 58 /* : */) {
const code = p.charCodeAt(0);
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
return p[0].toLowerCase() + p.slice(1);
}
}
return p;
};
const NODE_MODULES_PATH = normalizeDriveLetter(path.join(import.meta.dirname, '../node_modules'));
const Module = require('node:module') as typeof import('node:module') & {
_resolveLookupPaths: (request: string, parent: unknown) => string[] | null;
};
const originalResolveLookupPaths = Module._resolveLookupPaths;
Module._resolveLookupPaths = function (request: string, parent: unknown): string[] | null {
const paths = originalResolveLookupPaths(request, parent);
if (Array.isArray(paths)) {
for (let i = 0, len = paths.length; i < len; i++) {
if (normalizeDriveLetter(paths[i]) === NODE_MODULES_PATH) {
// Derive the archive path from the matched entry so drive-letter
// case and path separators are preserved exactly.
paths.splice(i, 0, `${paths[i]}.asar`);
break;
}
}
}
return paths;
};
}
enableASARSupport();
/**
* Add support for redirecting the loading of node modules
*
* Note: only applies when running out of sources.
*/
export function devInjectNodeModuleLookupPath(injectPath: string): void {
if (!process.env['VSCODE_DEV']) {
return; // only applies running out of sources
}
if (!injectPath) {
throw new Error('Missing injectPath');
}
// register a loader hook
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; // Electron disables global search paths in https://github.com/electron/electron/blob/3186c2f0efa92d275dc3d57b5a14a60ed3846b0e/shell/common/node_bindings.cc#L653
}
const Module = require('module');
const globalPaths = Module.globalPaths;
const originalResolveLookupPaths = Module._resolveLookupPaths;
Module._resolveLookupPaths = function (moduleName: string, parent: unknown): 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;
}
// On Windows, remove drive(s) and users' home directory from search paths,
// UNLESS 'from' is explicitly set to one of those.
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;
// Check if 'from' is the same as 'userDir'
if (!isUsersDir(from)) {
paths = paths.filter(p => !isUsersDir(path.dirname(p)));
}
}
return paths;
};
}
/**
* Helper to enable portable mode.
*/
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)));
}
// appRoot = ..\Microsoft VS Code Insiders\<version>\resources\app
if (process.platform === 'win32' && product.win32VersionedUpdate) {
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
};
}
You can’t perform that action at this time.
