Skip to content

Commit 3e58a60

Browse files
committed
Load windows-process-tree from appRoot at runtime
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.
1 parent 10a7cc9 commit 3e58a60

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/extension/debugger/attachQuickPick/provider.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
'use strict';
55

6-
import { l10n } from 'vscode';
6+
import * as path from 'path';
7+
import { env, l10n } from 'vscode';
78
import type { IProcessInfo } from '@vscode/windows-process-tree';
89
import { getOSType, OSType } from '../../common/platform';
910
import { PsProcessParser } from './psProcessParser';
@@ -16,6 +17,14 @@ import { logProcess } from '../../common/process/logger';
1617
export class AttachProcessProvider implements IAttachProcessProvider {
1718
constructor() {}
1819

20+
public _loadWindowsProcessTree(): typeof import('@vscode/windows-process-tree') {
21+
const wpcPath = path.join(env.appRoot, 'node_modules', '@vscode', 'windows-process-tree');
22+
// Use eval to bypass webpack's require interception for loading native addon at runtime
23+
// eslint-disable-next-line no-eval
24+
const nodeRequire = eval('require') as NodeJS.Require;
25+
return nodeRequire(wpcPath);
26+
}
27+
1928
public getAttachItems(): Promise<IAttachItem[]> {
2029
return this._getInternalProcessEntries().then((processEntries) => {
2130
processEntries.sort(
@@ -63,7 +72,7 @@ export class AttachProcessProvider implements IAttachProcessProvider {
6372

6473
if (osType === OSType.Windows) {
6574
try {
66-
const wpc = require('@vscode/windows-process-tree');
75+
const wpc = this._loadWindowsProcessTree();
6776
const processList = await new Promise<IProcessInfo[]>((resolve) => {
6877
wpc.getAllProcesses(
6978
(processes: IProcessInfo[]) => resolve(processes),

src/test/unittest/attachQuickPick/provider.unit.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { IAttachItem } from '../../../extension/debugger/attachQuickPick/types';
1313
import { WmicProcessParser } from '../../../extension/debugger/attachQuickPick/wmicProcessParser';
1414
import * as platform from '../../../extension/common/platform';
1515
import * as rawProcessApis from '../../../extension/common/process/rawProcessApis';
16-
import * as wpc from '@vscode/windows-process-tree';
1716

1817
use(chaiAsPromised);
1918

@@ -27,7 +26,12 @@ suite('Attach to process - process provider', () => {
2726
provider = new AttachProcessProvider();
2827
getOSTypeStub = sinon.stub(platform, 'getOSType');
2928
plainExecStub = sinon.stub(rawProcessApis, 'plainExec');
30-
getAllProcessesStub = sinon.stub(wpc, 'getAllProcesses');
29+
getAllProcessesStub = sinon.stub();
30+
sinon.stub(provider, '_loadWindowsProcessTree').returns({
31+
getAllProcesses: getAllProcessesStub,
32+
// eslint-disable-next-line @typescript-eslint/naming-convention
33+
ProcessDataFlag: { None: 0, Memory: 1, CommandLine: 2 },
34+
} as any);
3135
});
3236

3337
teardown(() => {

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const extensionConfig = {
3838
'@opentelemetry/instrumentation': 'commonjs @opentelemetry/instrumentation', // ignored because we don't ship instrumentation
3939
'@azure/opentelemetry-instrumentation-azure-sdk': 'commonjs @azure/opentelemetry-instrumentation-azure-sdk', // ignored because we don't ship instrumentation
4040
'@azure/functions-core': '@azure/functions-core', // ignored because we don't ship instrumentation
41-
'@vscode/windows-process-tree': 'commonjs @vscode/windows-process-tree', // native addon (.node binary); webpack cannot bundle it, resolved at runtime via VS Code's built-in copy
41+
'@vscode/windows-process-tree': 'commonjs @vscode/windows-process-tree', // native addon (.node binary); webpack cannot bundle it. Loaded at runtime via vscode.env.appRoot absolute path using eval('require') to bypass webpack's require interception
4242
},
4343
resolve: {
4444
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader

0 commit comments

Comments
 (0)