api: merge ESM and Node.js module interceptors#313500
Merged
Merged
Conversation
hediet
approved these changes
Apr 30, 2026
Contributor
There was a problem hiding this comment.
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
NodeModuleRequireInterceptorusingnode:module.registerHooks. - Add disposal wiring intended to deregister the registered hooks.
- Add an integration test that validates ESM
importand CJSrequirereturn 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #285297
Summary
Merges the
NodeModuleInterceptorclass (handling ESM imports) into theNodeModuleRequireInterceptorclass (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 thatimportandrequireon 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 forhooks.deregister().extensions/vscode-api-tests/src/singlefolder-tests/module.test.ts: Added validation test for API singleton integrity across ESM and CJS loaders.