Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove __$__nodeRequire usages #166686

Merged
merged 11 commits into from Nov 21, 2022
3 changes: 2 additions & 1 deletion build/gulpfile.reh.js
Expand Up @@ -378,7 +378,8 @@ function tweakProductForServerWeb(product) {
// TODO: we cannot inline `product.json` because
// it is being changed during build time at a later
// point in time (such as `checksums`)
'../product.json'
'../product.json',
'../package.json'
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion build/gulpfile.vscode.js
Expand Up @@ -119,7 +119,8 @@ const optimizeVSCodeTask = task.define('optimize-vscode', task.series(
// TODO: we cannot inline `product.json` because
// it is being changed during build time at a later
// point in time (such as `checksums`)
'../product.json'
'../product.json',
'../package.json',
]
},
manual: [
Expand Down
7 changes: 7 additions & 0 deletions src/bootstrap-amd.js
Expand Up @@ -11,6 +11,13 @@
// when this file is bundled with other files.
const nodeRequire = require;

// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => nodeRequire(String(mod)) });

// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = require('../product.json');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason product/package.json cannot go through the same proxy above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, _VSCODE_NODE_MODULES is debt and must eventually disappear whereas the _VSCODE_PRODUCT_JSON and _VSCODE_PACKAGE_JSON globals are likely staying (being merged with vscode.context...)

globalThis._VSCODE_PACKAGE_JSON = require('../package.json');

const loader = require('./vs/loader');
const bootstrap = require('./bootstrap');
const performance = require('./vs/base/common/performance');
Expand Down
9 changes: 9 additions & 0 deletions src/bootstrap-window.js
Expand Up @@ -112,6 +112,15 @@

window['MonacoEnvironment'] = {};

// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => (require.__$__nodeRequire ?? require)(String(mod)) });

if (!safeProcess.sandboxed) {
// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = (require.__$__nodeRequire ?? require)(configuration.appRoot + '/product.json');
globalThis._VSCODE_PACKAGE_JSON = (require.__$__nodeRequire ?? require)(configuration.appRoot + '/package.json');
}

const loaderConfig = {
baseUrl: `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out`,
'vs/nls': nlsConfig,
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Expand Up @@ -318,6 +318,7 @@ function getArgvConfigPath() {
dataFolderName = `${dataFolderName}-dev`;
}

// @ts-ignore
jrieken marked this conversation as resolved.
Show resolved Hide resolved
return path.join(os.homedir(), dataFolderName, 'argv.json');
}

Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.monaco.json
Expand Up @@ -18,6 +18,7 @@
"include": [
"typings/require.d.ts",
"typings/thenable.d.ts",
"typings/vscode-globals-product.d.ts",
"vs/loader.d.ts",
"vs/monaco.d.ts",
"vs/editor/*",
Expand Down
9 changes: 8 additions & 1 deletion src/typings/require.d.ts
Expand Up @@ -45,10 +45,17 @@ interface NodeRequire {
* @deprecated use `FileAccess.asFileUri()` for node.js contexts or `FileAccess.asBrowserUri` for browser contexts.
*/
toUrl(path: string): string;

/**
* @deprecated MUST not be used anymore
*
* With the move from AMD to ESM we cannot use this anymore. There will be NO MORE node require like this.
*/
__$__nodeRequire<T>(moduleName: string): T;

(dependencies: string[], callback: (...args: any[]) => any, errorback?: (err: any) => void): any;
config(data: any): any;
onError: Function;
__$__nodeRequire<T>(moduleName: string): T;
getStats?(): ReadonlyArray<LoaderEvent>;
hasDependencyCycle?(): boolean;
define(amdModuleId: string, dependencies: string[], callback: (...args: any[]) => any): any;
Expand Down
30 changes: 30 additions & 0 deletions src/typings/vscode-globals-modules.d.ts
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// AMD2ESM mirgation relevant

declare global {

/**
* @deprecated node modules that are in used in a context that
* shouldn't have access to node_modules (node-free renderer or
* shared process)
*/
var _VSCODE_NODE_MODULES: {
crypto: typeof import('crypto');
zlib: typeof import('zlib');
net: typeof import('net');
os: typeof import('os');
module: typeof import('module');
['native-watchdog']: typeof import('native-watchdog')
perf_hooks: typeof import('perf_hooks');

['vsda']: any
['vscode-encrypt']: any
}
}

// fake export to make global work
export { }
22 changes: 22 additions & 0 deletions src/typings/vscode-globals-product.d.ts
@@ -0,0 +1,22 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// AMD2ESM mirgation relevant

declare global {

/**
* @deprecated You MUST use `IProductService` whenever possible.
*/
var _VSCODE_PRODUCT_JSON: Record<string, any>;
/**
* @deprecated You MUST use `IProductService` whenever possible.
*/
var _VSCODE_PACKAGE_JSON: Record<string, any>;

}

// fake export to make global work
export { }
2 changes: 1 addition & 1 deletion src/vs/base/common/performance.js
Expand Up @@ -78,7 +78,7 @@
} else if (typeof process === 'object') {
// node.js: use the normal polyfill but add the timeOrigin
// from the node perf_hooks API as very first mark
const timeOrigin = Math.round((require.nodeRequire || require)('perf_hooks').performance.timeOrigin);
const timeOrigin = Math.round((require.__$__nodeRequire || require)('perf_hooks').performance.timeOrigin);
return _definePolyfillMarks(timeOrigin);

} else {
Expand Down
8 changes: 4 additions & 4 deletions src/vs/base/parts/ipc/node/ipc.net.ts
Expand Up @@ -20,10 +20,10 @@ import { ChunkStream, Client, ISocket, Protocol, SocketCloseEvent, SocketCloseEv
// TODO@bpasero remove me once electron utility process has landed
function getNodeDependencies() {
return {
crypto: (require.__$__nodeRequire('crypto') as any) as typeof import('crypto'),
zlib: (require.__$__nodeRequire('zlib') as any) as typeof import('zlib'),
net: (require.__$__nodeRequire('net') as any) as typeof import('net'),
os: (require.__$__nodeRequire('os') as any) as typeof import('os')
crypto: globalThis._VSCODE_NODE_MODULES.crypto,
zlib: globalThis._VSCODE_NODE_MODULES.zlib,
net: globalThis._VSCODE_NODE_MODULES.net,
os: globalThis._VSCODE_NODE_MODULES.os,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/vs/platform/environment/test/node/nativeModules.test.ts
Expand Up @@ -58,7 +58,7 @@ flakySuite('Native Modules (all platforms)', () => {

test('vscode-encrypt', async () => {
try {
const vscodeEncrypt: Encryption = require.__$__nodeRequire('vscode-encrypt');
const vscodeEncrypt: Encryption = globalThis._VSCODE_NODE_MODULES['vscode-encrypt'];
const encrypted = await vscodeEncrypt.encrypt('salt', 'value');
const decrypted = await vscodeEncrypt.decrypt('salt', encrypted);

Expand All @@ -73,7 +73,7 @@ flakySuite('Native Modules (all platforms)', () => {

test('vsda', async () => {
try {
const vsda: any = require.__$__nodeRequire('vsda');
const vsda: any = globalThis._VSCODE_NODE_MODULES['vsda'];
const signer = new vsda.signer();
const signed = await signer.sign('value');
assert.ok(typeof signed === 'string', testErrorMessage('vsda'));
Expand Down
16 changes: 5 additions & 11 deletions src/vs/platform/product/common/product.ts
Expand Up @@ -3,11 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { FileAccess } from 'vs/base/common/network';
import { globals } from 'vs/base/common/platform';
import { env } from 'vs/base/common/process';
import { IProductConfiguration } from 'vs/base/common/product';
import { dirname, joinPath } from 'vs/base/common/resources';
import { ISandboxConfiguration } from 'vs/base/parts/sandbox/common/sandboxTypes';

/**
Expand All @@ -24,14 +22,10 @@ if (typeof globals.vscode !== 'undefined' && typeof globals.vscode.context !== '
throw new Error('Sandbox: unable to resolve product configuration from preload script.');
}
}

// Native node.js environment
else if (typeof require?.__$__nodeRequire === 'function') {

// Obtain values from product.json and package.json
const rootPath = dirname(FileAccess.asFileUri(''));

product = require.__$__nodeRequire(joinPath(rootPath, 'product.json').fsPath);
// _VSCODE environment
else if (globalThis._VSCODE_PRODUCT_JSON && globalThis._VSCODE_PACKAGE_JSON) {
// Obtain values from product.json and package.json-data
product = globalThis._VSCODE_PRODUCT_JSON as IProductConfiguration;

// Running out of sources
if (env['VSCODE_DEV']) {
Expand All @@ -47,7 +41,7 @@ else if (typeof require?.__$__nodeRequire === 'function') {
// want to have it running out of sources so we
// read it from package.json only when we need it.
if (!product.version) {
const pkg = require.__$__nodeRequire(joinPath(rootPath, 'package.json').fsPath) as { version: string };
const pkg = globalThis._VSCODE_PACKAGE_JSON as { version: string };
Copy link
Member

@bpasero bpasero Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is lazy to avoid a require(package.json) when running because a built time we add version to the product.json. The only exception is running out of sources for why this is called here. Would be nice for startup perf to avoid the package.json lookup.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the ESM future there will be no more way to synchronously load anything. So, the only way forward will be something like _VSCODE_PRODUCT where the bootstrapper or embedder does these conditional loads. I suggest that we merge this with the vscode.context merging effort


Object.assign(product, {
version: pkg.version
Expand Down
2 changes: 1 addition & 1 deletion src/vs/server/node/remoteExtensionHostAgentServer.ts
Expand Up @@ -727,7 +727,7 @@ export async function createServer(address: string | net.AddressInfo | null, arg
const hasVSDA = fs.existsSync(join(FileAccess.asFileUri('').fsPath, '../node_modules/vsda'));
if (hasVSDA) {
try {
return <typeof vsda>require.__$__nodeRequire('vsda');
return <typeof vsda>globalThis._VSCODE_NODE_MODULES['vsda'];
} catch (err) {
logService.error(err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/node/extHostExtensionService.ts
Expand Up @@ -22,7 +22,7 @@ class NodeModuleRequireInterceptor extends RequireInterceptor {

protected _installInterceptor(): void {
const that = this;
const node_module = <any>require.__$__nodeRequire('module');
const node_module = <any>globalThis._VSCODE_NODE_MODULES.module;
const originalLoad = node_module._load;
node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) {
request = applyAlternatives(request);
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/node/extensionHostProcess.ts
Expand Up @@ -81,7 +81,7 @@ const args = minimist(process.argv.slice(2), {
// happening we essentially blocklist this module from getting loaded in any
// extension by patching the node require() function.
(function () {
const Module = require.__$__nodeRequire('module') as any;
const Module = globalThis._VSCODE_NODE_MODULES.module as any;
const originalLoad = Module._load;

Module._load = function (request: string) {
Expand Down Expand Up @@ -325,7 +325,7 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise<IRenderer
// So also use the native node module to do it from a separate thread
let watchdog: typeof nativeWatchdog;
try {
watchdog = require.__$__nodeRequire('native-watchdog');
watchdog = globalThis._VSCODE_NODE_MODULES['native-watchdog'];
watchdog.start(initData.parentPid);
} catch (err) {
// no problem...
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/node/proxyResolver.ts
Expand Up @@ -97,7 +97,7 @@ const modulesCache = new Map<IExtensionDescription | undefined, { http?: typeof
function configureModuleLoading(extensionService: ExtHostExtensionService, lookup: ReturnType<typeof createPatchedModules>): Promise<void> {
return extensionService.getExtensionPathIndex()
.then(extensionPaths => {
const node_module = <any>require.__$__nodeRequire('module');
const node_module = <any>globalThis._VSCODE_NODE_MODULES.module;
const original = node_module._load;
node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) {
if (request === 'tls') {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/electron/renderer.js
Expand Up @@ -72,6 +72,13 @@ if (util.inspect && util.inspect['defaultOptions']) {
util.inspect['defaultOptions'].customInspect = false;
}

// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => (require.__$__nodeRequire ?? require)(String(mod)) });

// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = (require.__$__nodeRequire ?? require)('../../../product.json');
globalThis._VSCODE_PACKAGE_JSON = (require.__$__nodeRequire ?? require)('../../../package.json');

const _tests_glob = '**/test/**/*.test.js';
let loader;
let _out;
Expand Down
9 changes: 9 additions & 0 deletions test/unit/node/index.js
Expand Up @@ -56,6 +56,15 @@ if (majorRequiredNodeVersion !== currentMajorNodeVersion) {
}

function main() {

// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => require(String(mod)) });

// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = require(`${REPO_ROOT}/product.json`);
globalThis._VSCODE_PACKAGE_JSON = require(`${REPO_ROOT}/package.json`);


process.on('uncaughtException', function (e) {
console.error(e.stack || e);
});
Expand Down