From b523d2d085f074283626851208760ea6f2ce4ef2 Mon Sep 17 00:00:00 2001 From: Mark Sujew Date: Mon, 13 Jan 2025 12:11:39 +0100 Subject: [PATCH 1/2] Provide node-pty package for plugins --- package-lock.json | 1 + packages/plugin-ext/package.json | 1 + .../src/hosted/node/plugin-host-rpc.ts | 7 ++- .../hosted/node/plugin-require-override.ts | 45 +++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 packages/plugin-ext/src/hosted/node/plugin-require-override.ts diff --git a/package-lock.json b/package-lock.json index 45c0ad2a529a8..eeba011bbfef9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30941,6 +30941,7 @@ "lodash.clonedeep": "^4.5.0", "macaddress": "^0.5.3", "mime": "^2.4.4", + "node-pty": "1.1.0-beta27", "ps-tree": "^1.2.0", "semver": "^7.5.4", "tslib": "^2.6.2", diff --git a/packages/plugin-ext/package.json b/packages/plugin-ext/package.json index 73a77bf958aa3..9dcbb78d16027 100644 --- a/packages/plugin-ext/package.json +++ b/packages/plugin-ext/package.json @@ -44,6 +44,7 @@ "lodash.clonedeep": "^4.5.0", "macaddress": "^0.5.3", "mime": "^2.4.4", + "node-pty": "1.1.0-beta27", "ps-tree": "^1.2.0", "semver": "^7.5.4", "tslib": "^2.6.2", diff --git a/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts b/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts index 05c6764a5c3bb..895216040540d 100644 --- a/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts +++ b/packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts @@ -19,7 +19,8 @@ import { dynamicRequire, removeFromCache } from '@theia/core/lib/node/dynamic-require'; import { ContainerModule, inject, injectable, postConstruct, unmanaged } from '@theia/core/shared/inversify'; import { AbstractPluginManagerExtImpl, PluginHost, PluginManagerExtImpl } from '../../plugin/plugin-manager'; -import { MAIN_RPC_CONTEXT, Plugin, PluginAPIFactory, PluginManager, +import { + MAIN_RPC_CONTEXT, Plugin, PluginAPIFactory, PluginManager, LocalizationExt } from '../../common/plugin-api-rpc'; import { PluginMetadata, PluginModel } from '../../common/plugin-protocol'; @@ -41,6 +42,7 @@ import { connectProxyResolver } from './plugin-host-proxy'; import { LocalizationExtImpl } from '../../plugin/localization-ext'; import { RPCProtocol, ProxyIdentifier } from '../../common/rpc-protocol'; import { PluginApiCache } from '../../plugin/node/plugin-container-module'; +import { overridePluginDependencies } from './plugin-require-override'; /** * The full set of all possible `Ext` interfaces that a plugin manager can support. @@ -107,6 +109,7 @@ export abstract class AbstractPluginHostRPC Date: Mon, 27 Jan 2025 14:55:37 +0100 Subject: [PATCH 2/2] Attempt to run require first --- .../src/hosted/node/plugin-require-override.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/plugin-ext/src/hosted/node/plugin-require-override.ts b/packages/plugin-ext/src/hosted/node/plugin-require-override.ts index 877fa7924f269..1da5bc2417789 100644 --- a/packages/plugin-ext/src/hosted/node/plugin-require-override.ts +++ b/packages/plugin-ext/src/hosted/node/plugin-require-override.ts @@ -35,11 +35,19 @@ export function overridePluginDependencies(): void { const node_module = require('module'); const original = node_module._load; node_module._load = function (request: string): unknown { - for (const filter of overrides) { - if (request === filter.package || request.endsWith(`node_modules/${filter.package}`) || request.endsWith(`node_modules\\${filter.package}`)) { - return filter.module; + try { + // Attempt to load the original module + // In some cases VS Code extensions will come with their own `node_modules` folder + return original.apply(this, arguments); + } catch (e) { + // If the `require` call failed, attempt to load the module from the overrides + for (const filter of overrides) { + if (request === filter.package || request.endsWith(`node_modules/${filter.package}`) || request.endsWith(`node_modules\\${filter.package}`)) { + return filter.module; + } } + // If no override was found, rethrow the error + throw e; } - return original.apply(this, arguments); }; }