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

Allow Extensions to use native modules shipped with vscode #84439

Closed
go2sh opened this issue Nov 11, 2019 · 1 comment
Closed

Allow Extensions to use native modules shipped with vscode #84439

go2sh opened this issue Nov 11, 2019 · 1 comment
Labels
*dev-question VS Code Extension Development Question

Comments

@go2sh
Copy link
Contributor

go2sh commented Nov 11, 2019

It would be nice to use the node-pty module shipped with vscode in an extension. Currently, I'am doing the following:

//@ts-ignore
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
const moduleName = path.join(vscode.env.appRoot, "node_modules.asar", "node-pty");
const spawn: typeof import('node-pty').spawn = requireFunc(moduleName).spawn;

It should be possible to create a require interceptor injecting the parrents node-pty object into the extension.

This is especially nice as there is no general way of loading platform dependent code.

@alexdima
Copy link
Member

@go2sh Well, keep doing what you're doing... But keep an eye out, as your extension might get broken by a future VS Code update... Platform specific extensions is something we have on our radar and we track that at #23251

Here is the code pattern that you can use:

/**
 * Returns a node module installed with VSCode, or null if it fails.
 */
function getCoreNodeModule(moduleName: string) {
    try {
        return require(`${vscode.env.appRoot}/node_modules.asar/${moduleName}`);
    } catch (err) { }

    try {
        return require(`${vscode.env.appRoot}/node_modules/${moduleName}`);
    } catch (err) { }

    return null;
}

i.e. please always handle the failure case and inform users...

@alexdima alexdima added the *dev-question VS Code Extension Development Question label Nov 11, 2019
@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*dev-question VS Code Extension Development Question
Projects
None yet
Development

No branches or pull requests

2 participants