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

Provide node-pty package for plugins #14720

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/plugin-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down Expand Up @@ -107,6 +109,7 @@ export abstract class AbstractPluginHostRPC<PM extends AbstractPluginManagerExtI

@postConstruct()
initialize(): void {
overridePluginDependencies();
this.pluginManager.setPluginHost(this.createPluginHost());

const extInterfaces = this.createExtInterfaces();
Expand Down Expand Up @@ -273,7 +276,7 @@ export abstract class AbstractPluginHostRPC<PM extends AbstractPluginManagerExtI
* @param extApi the extension API to initialize, if appropriate
* @throws if any error occurs in initializing the extension API
*/
protected abstract initExtApi(extApi: ExtPluginApi): void;
protected abstract initExtApi(extApi: ExtPluginApi): void;
}

/**
Expand Down
53 changes: 53 additions & 0 deletions packages/plugin-ext/src/hosted/node/plugin-require-override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/********************************************************************************
* Copyright (C) 2024 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
********************************************************************************/

import * as nodePty from 'node-pty';

const overrides = [
{
package: 'node-pty',
module: nodePty
}
];

/**
* Some plugins attempt to require some packages from VSCode's node_modules.
* Since we don't have node_modules usually, we need to override the require function to return the expected package.
*
* See also:
* https://github.com/eclipse-theia/theia/issues/14714
* https://github.com/eclipse-theia/theia/issues/13779
*/
export function overridePluginDependencies(): void {
const node_module = require('module');
const original = node_module._load;
node_module._load = function (request: string): unknown {
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;
}
};
}
Loading