Skip to content

api: merge ESM and Node.js module interceptors#313500

Merged
jrieken merged 3 commits into
mainfrom
joh/module-interceptor-merge
Apr 30, 2026
Merged

api: merge ESM and Node.js module interceptors#313500
jrieken merged 3 commits into
mainfrom
joh/module-interceptor-merge

Conversation

@jrieken
Copy link
Copy Markdown
Contributor

@jrieken jrieken commented Apr 30, 2026

fixes #285297

Summary

Merges the NodeModuleInterceptor class (handling ESM imports) into the NodeModuleRequireInterceptor class (handling CJS requires) so that module interception for both ESM and CJS are handled together in a single place. Additionally, an integration test is included to verify that import and require on the VS Code API module consistently yield identical object instances.

Session Context Key decisions from the development session: - **Interceptor Merging**: We explicitly chose to merge `NodeModuleInterceptor` into `NodeModuleRequireInterceptor` rather than having two separate classes. This keeps both ESM and CJS interception rules centralized and simpler to maintain. - **Hook Cleanup**: Since the Node `registerHooks` method returns an object with a `deregister` method, we tied this hook to the interceptor's internal `DisposableStore`. It will be safely cleaned up on disposal. - **ESM Integration Test Strategy**: We wrote the integration test using a `new Function('url', 'return import(url)')` wrapper to bypass TypeScript transpiling dynamic `import()` to `require()` in the `extensions/vscode-api-tests` CommonJS build constraints. - **Test File Location**: We specifically wrote the test file adjacent to the suite rather than using the OS temp directory, because VS Code identifies calling packages and permissions using file path containment. Writing in temp would cause `import` interceptions to fail. A fallback to skip the test was added if file system writing fails, and a bold disclaimer is placed inside the code block so it can be safely removed by developers if a crash prevents automatic cleanup.

Changes

  • src/vs/workbench/api/node/extHostExtensionService.ts: Merged intercepts, added cleanup disposable for hooks.deregister().
  • extensions/vscode-api-tests/src/singlefolder-tests/module.test.ts: Added validation test for API singleton integrity across ESM and CJS loaders.

Copilot AI review requested due to automatic review settings April 30, 2026 13:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR consolidates VS Code API module interception for both CommonJS (require) and ESM (import) into a single interceptor implementation in the extension host, and adds an integration test to ensure import 'vscode' and require('vscode') expose the same underlying API instances.

Changes:

  • Merge the ESM interception logic into NodeModuleRequireInterceptor using node:module.registerHooks.
  • Add disposal wiring intended to deregister the registered hooks.
  • Add an integration test that validates ESM import and CJS require return consistent API instances.
Show a summary per file
File Description
src/vs/workbench/api/node/extHostExtensionService.ts Merges ESM+CJS interception paths into one interceptor and attempts to deregister hooks on disposal.
extensions/vscode-api-tests/src/singlefolder-tests/module.test.ts Adds an integration test that dynamically imports an .mjs file to compare ESM vs CJS vscode API instances.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment thread src/vs/workbench/api/node/extHostExtensionService.ts
@jrieken jrieken self-assigned this Apr 30, 2026
@jrieken jrieken added this to the 1.119.0 milestone Apr 30, 2026
@jrieken jrieken enabled auto-merge April 30, 2026 13:31
@jrieken jrieken merged commit a5bfd2d into main Apr 30, 2026
31 checks passed
@jrieken jrieken deleted the joh/module-interceptor-merge branch April 30, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using require(esm) breaks the mechanism for intercepting the vscode module

4 participants