Load native windows-process-tree via appRoot (Follow-up to #991)#1030
Conversation
Add _loadWindowsProcessTree helper to resolve @vscode/windows-process-tree via env.appRoot and eval('require') so the native .node addon is loaded at runtime (bypassing webpack's require interception). Update AttachProcessProvider to use this loader instead of a direct require. Update unit tests to remove the direct import of the windows-process-tree module and stub the new _loadWindowsProcessTree method. Clarify webpack comment to document the runtime loading approach. Also add path and env imports used by the new loader.
|
|
||
| if (osType === OSType.Windows) { | ||
| try { | ||
| const wpc = require('@vscode/windows-process-tree'); |
There was a problem hiding this comment.
I believe the way this is handled for other stuff is that VS code would intercept the require and return their implementation. Perhaps they don't do that for windows-process-tree though. I recall this happening for things like the 'vscode' package itself.
There was a problem hiding this comment.
I believe the way this is handled for other stuff is that VS code would intercept the require and return their implementation. Perhaps they don't do that for windows-process-tree though. I recall this happening for things like the 'vscode' package itself.
Yep, I found out other modules like @vscode/extension-telemetry, VS Code's loader does exactly that and intercepts the require call.
Maybe, @vscode/windows-process-tree is a bit of a special case because it’s a native addon containing a .node binary? Since it involves loading machine code that is highly sensitive to the specific OS architecture and Node ABI, it doesn't seem to be part of that standard interception layer. Loading it via appRoot ensures we're hitting the actual binary that matches the running VS Code instance.

This is a follow-up to PR #991.
🎯 Summary
Fixes an issue where the
@vscode/windows-process-treenative module failed to load in production. This PR leveragesvscode.env.appRootto locate VS Code's built-in module and uses aneval('require')bypass to circumvent Webpack’s dependency interception.🔍 Why the previous
require()approach failedIn published builds,
require('@vscode/windows-process-tree')consistently threwMODULE_NOT_FOUND, causing a silent fallback to WMIC. This was caused by two overlapping technical constraints:Since the package is marked as an
externalin Webpack and excluded from the bundle via.vscodeignore, the installed extension directory contains nonode_modules/@vscode/windows-process-tree. Node’s standard resolution algorithm does not look inside VS Code’s ownresources/app/node_modulesfor bare specifiers.Even if an absolute path is computed via
appRoot, Webpack rewrites the standardrequire()call to__webpack_require__. This internal function is unable to resolve arbitrary absolute filesystem paths outside of the Webpack dependency graph.As a result, the optimized native code path was never actually exercised in production.
💡 New Design
We now load the module using Node’s native loader by bypassing Webpack's static analysis:
env.appRootto point directly to the VS Code installation root, where a compatible version of@vscode/windows-process-treeis guaranteed to exist.eval('require')returns the true Node.jsrequirefunction. Since Webpack does not analyze the contents ofeval, it leaves the call untouched, allowing runtime resolution of the absolute path.try/catchblock. If the requirement fails (due to ABI mismatch, missing binaries, or older VS Code versions), the existing WMIC fallback remains functional._loadWindowsProcessTree()to allow unit tests to stub the loader without needing a real VS Code environment orevalexecution.import typeis retained for compile-time checks. These are erased during compilation and do not generate any runtimeimportstatements.// This PR description was generated by AI
// No programmers were harmed in the making of this description