From d60295dc0e58fbb4fe699215654022ea30c4178a Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 3 Sep 2024 15:18:08 +0200 Subject: [PATCH] ESM: revisit need for `_VSCODE_NODE_MODULES` (fix #226453) (#227427) --- src/bootstrap-amd.js | 9 +----- src/typings/vscode-globals-modules.d.ts | 32 ------------------- src/vs/amdX.ts | 7 ++-- .../node/remoteExtensionHostAgentServer.ts | 6 +++- .../api/node/extHostExtensionService.ts | 2 +- .../api/node/extensionHostProcess.ts | 9 ++++-- src/vs/workbench/api/node/proxyResolver.ts | 2 +- test/unit/electron/renderer.amd.js | 3 -- test/unit/electron/renderer.js | 3 -- test/unit/node/index.amd.js | 3 -- test/unit/node/index.mjs | 5 +-- 11 files changed, 18 insertions(+), 63 deletions(-) delete mode 100644 src/typings/vscode-globals-modules.d.ts diff --git a/src/bootstrap-amd.js b/src/bootstrap-amd.js index a59427a5482..9c8daf55756 100644 --- a/src/bootstrap-amd.js +++ b/src/bootstrap-amd.js @@ -20,6 +20,7 @@ import { product, pkg } from './bootstrap-meta.js'; import './bootstrap-node.js'; import * as performance from './vs/base/common/performance.js'; +/** @ts-ignore */ const require = createRequire(import.meta.url); /** @type any */ const module = { exports: {} }; @@ -45,14 +46,6 @@ if (process.env['ELECTRON_RUN_AS_NODE'] || process.versions['electron']) { } // ESM-uncomment-end -// Store the node.js require function in a variable -// before loading our AMD loader to avoid issues -// 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 /** @type Partial */ // ESM-comment-begin diff --git a/src/typings/vscode-globals-modules.d.ts b/src/typings/vscode-globals-modules.d.ts deleted file mode 100644 index cfeeac71845..00000000000 --- a/src/typings/vscode-globals-modules.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -// AMD2ESM migration relevant - -declare global { - - /** - * TODO@esm @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'); - fs: typeof import('fs'), - vm: typeof import('vm'), - ['native-watchdog']: typeof import('native-watchdog') - perf_hooks: typeof import('perf_hooks'); - - ['vsda']: any - ['vscode-encrypt']: any - } -} - -// fake export to make global work -export { } diff --git a/src/vs/amdX.ts b/src/vs/amdX.ts index 9a336316c1d..456b887eb6b 100644 --- a/src/vs/amdX.ts +++ b/src/vs/amdX.ts @@ -185,9 +185,9 @@ class AMDModuleImporter { private async _nodeJSLoadScript(scriptSrc: string): Promise { try { - const fs = (globalThis as any)._VSCODE_NODE_MODULES['fs']; - const vm = (globalThis as any)._VSCODE_NODE_MODULES['vm']; - const module = (globalThis as any)._VSCODE_NODE_MODULES['module']; + const fs = (await import(`${'fs'}`)).default; + const vm = (await import(`${'vm'}`)).default; + const module = (await import(`${'module'}`)).default; const filePath = URI.parse(scriptSrc).fsPath; const content = fs.readFileSync(filePath).toString(); @@ -196,7 +196,6 @@ class AMDModuleImporter { const compileWrapper = script.runInThisContext(); compileWrapper.apply(); return this._defineCalls.pop(); - } catch (error) { throw error; } diff --git a/src/vs/server/node/remoteExtensionHostAgentServer.ts b/src/vs/server/node/remoteExtensionHostAgentServer.ts index 40a82477d8c..c77fbfd3736 100644 --- a/src/vs/server/node/remoteExtensionHostAgentServer.ts +++ b/src/vs/server/node/remoteExtensionHostAgentServer.ts @@ -40,6 +40,10 @@ import { determineServerConnectionToken, requestHasValidConnectionToken as httpR import { IServerEnvironmentService, ServerParsedArgs } from './serverEnvironmentService.js'; import { setupServerServices, SocketServer } from './serverServices.js'; import { CacheControl, serveError, serveFile, WebClientServer } from './webClientServer.js'; +// ESM-uncomment-begin +import { createRequire } from 'node:module'; +const require = createRequire(import.meta.url); +// ESM-uncomment-end const SHUTDOWN_TIMEOUT = 5 * 60 * 1000; @@ -768,7 +772,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 globalThis._VSCODE_NODE_MODULES['vsda']; + return require('vsda'); } catch (err) { logService.error(err); } diff --git a/src/vs/workbench/api/node/extHostExtensionService.ts b/src/vs/workbench/api/node/extHostExtensionService.ts index ee422b09075..98c7296433b 100644 --- a/src/vs/workbench/api/node/extHostExtensionService.ts +++ b/src/vs/workbench/api/node/extHostExtensionService.ts @@ -27,7 +27,7 @@ class NodeModuleRequireInterceptor extends RequireInterceptor { protected _installInterceptor(): void { const that = this; - const node_module = globalThis._VSCODE_NODE_MODULES.module; + const node_module = require('module'); const originalLoad = node_module._load; node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) { request = applyAlternatives(request); diff --git a/src/vs/workbench/api/node/extensionHostProcess.ts b/src/vs/workbench/api/node/extensionHostProcess.ts index 6166d8095b8..694559385c4 100644 --- a/src/vs/workbench/api/node/extensionHostProcess.ts +++ b/src/vs/workbench/api/node/extensionHostProcess.ts @@ -25,10 +25,13 @@ import { IHostUtils } from '../common/extHostExtensionService.js'; import { createURITransformer } from './uriTransformer.js'; import { ExtHostConnectionType, readExtHostConnection } from '../../services/extensions/common/extensionHostEnv.js'; import { ExtensionHostExitCode, IExtHostReadyMessage, IExtHostReduceGraceTimeMessage, IExtHostSocketMessage, IExtensionHostInitData, MessageType, createMessageOfType, isMessageOfType } from '../../services/extensions/common/extensionHostProtocol.js'; - import { IDisposable } from '../../../base/common/lifecycle.js'; import '../common/extHost.common.services.js'; import './extHost.node.services.js'; +// ESM-uncomment-begin +import { createRequire } from 'node:module'; +const require = createRequire(import.meta.url); +// ESM-uncomment-end interface ParsedExtHostArgs { transformURIs?: boolean; @@ -63,7 +66,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 = globalThis._VSCODE_NODE_MODULES.module as any; + const Module = require('module'); const originalLoad = Module._load; Module._load = function (request: string) { @@ -327,7 +330,7 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise): Promise { return extensionService.getExtensionPathIndex() .then(extensionPaths => { - const node_module = globalThis._VSCODE_NODE_MODULES.module; + const node_module = require('module'); const original = node_module._load; node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) { if (request === 'net') { diff --git a/test/unit/electron/renderer.amd.js b/test/unit/electron/renderer.amd.js index b4fd486893a..43afa46a4c5 100644 --- a/test/unit/electron/renderer.amd.js +++ b/test/unit/electron/renderer.amd.js @@ -74,9 +74,6 @@ 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'); diff --git a/test/unit/electron/renderer.js b/test/unit/electron/renderer.js index 1ed91398fb6..2fb26eab8b8 100644 --- a/test/unit/electron/renderer.js +++ b/test/unit/electron/renderer.js @@ -73,9 +73,6 @@ 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(String(mod)) }); - // VSCODE_GLOBALS: package/product.json globalThis._VSCODE_PRODUCT_JSON = require('../../../product.json'); globalThis._VSCODE_PACKAGE_JSON = require('../../../package.json'); diff --git a/test/unit/node/index.amd.js b/test/unit/node/index.amd.js index 8e4a0fa0915..3ad33597820 100644 --- a/test/unit/node/index.amd.js +++ b/test/unit/node/index.amd.js @@ -77,9 +77,6 @@ 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`); diff --git a/test/unit/node/index.mjs b/test/unit/node/index.mjs index 0193be6843b..210423c726f 100644 --- a/test/unit/node/index.mjs +++ b/test/unit/node/index.mjs @@ -79,11 +79,8 @@ if (majorRequiredNodeVersion !== currentMajorNodeVersion) { function main() { - // VSCODE_GLOBALS: node_modules - const _require = module.createRequire(import.meta.url); - globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => _require(String(mod)) }); - // VSCODE_GLOBALS: package/product.json + const _require = module.createRequire(import.meta.url); globalThis._VSCODE_PRODUCT_JSON = _require(`${REPO_ROOT}/product.json`); globalThis._VSCODE_PACKAGE_JSON = _require(`${REPO_ROOT}/package.json`);