forked from microsoft/vscode
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-node.ts
205 lines (170 loc) · 6.02 KB
/
bootstrap-node.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from "fs";
import { createRequire } from "node:module";
import * as path from "path";
import { fileURLToPath } from "url";
import type { IProductConfiguration } from "./vs/base/common/product.js";
const require = createRequire(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const isWindows = process.platform === "win32";
// 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 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: 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;
}
// 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(__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,
};
}