Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/vs/base/node/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import { FileAccess } from '../common/network.js';
import { ProcessItem } from '../common/processes.js';
import { isWindows } from '../common/platform.js';

export function listProcesses(rootPid: number): Promise<ProcessItem> {
export const JS_FILENAME_PATTERN = /[a-zA-Z-]+\.js\b/g;

export function listProcesses(rootPid: number): Promise<ProcessItem> {
return new Promise((resolve, reject) => {

let rootItem: ProcessItem | undefined;
const map = new Map<number, ProcessItem>();
const totalMemory = totalmem();

function addToTree(pid: number, ppid: number, cmd: string, load: number, mem: number) {

const parent = map.get(ppid);
if (pid === rootPid || parent) {

const item: ProcessItem = {
name: findName(cmd),
cmd,
Expand Down Expand Up @@ -49,7 +47,6 @@ export function listProcesses(rootPid: number): Promise<ProcessItem> {
}

function findName(cmd: string): string {

const UTILITY_NETWORK_HINT = /--utility-sub-type=network/i;
const WINDOWS_CRASH_REPORTER = /--crashes-directory/i;
const WINPTY = /\\pipe\\winpty-control/i;
Expand Down Expand Up @@ -88,27 +85,24 @@ export function listProcesses(rootPid: number): Promise<ProcessItem> {
return matches[1];
}

// find all xxxx.js
const JS = /[a-zA-Z-]+\.js/g;
let result = '';
do {
matches = JS.exec(cmd);
if (matches) {
result += matches + ' ';
}
} while (matches);
if (cmd.indexOf('node ') < 0 && cmd.indexOf('node.exe') < 0) {
let result = ''; // find all xyz.js
do {
matches = JS_FILENAME_PATTERN.exec(cmd);
if (matches) {
result += matches + ' ';
}
} while (matches);

if (result) {
if (cmd.indexOf('node ') < 0 && cmd.indexOf('node.exe') < 0) {
return `electron-nodejs (${result})`;
if (result) {
return `electron-nodejs (${result.trim()})`;
}
}

return cmd;
}

if (process.platform === 'win32') {

const cleanUNCPrefix = (value: string): string => {
if (value.indexOf('\\\\?\\') === 0) {
return value.substring(4);
Expand Down Expand Up @@ -167,8 +161,12 @@ export function listProcesses(rootPid: number): Promise<ProcessItem> {
});
}, windowsProcessTree.ProcessDataFlag.CommandLine | windowsProcessTree.ProcessDataFlag.Memory);
});
} else { // OS X & Linux
}

// OS X & Linux
else {
function calculateLinuxCpuUsage() {

// Flatten rootItem to get a list of all VSCode processes
let processes = [rootItem];
const pids: number[] = [];
Expand Down
64 changes: 64 additions & 0 deletions src/vs/base/test/node/ps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { deepStrictEqual } from 'assert';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';
import { JS_FILENAME_PATTERN } from '../../node/ps.js';

suite('Process Utils', () => {

ensureNoDisposablesAreLeakedInTestSuite();

suite('JS file regex', () => {

function findJsFiles(cmd: string): string[] {
const matches: string[] = [];
let match;
while ((match = JS_FILENAME_PATTERN.exec(cmd)) !== null) {
matches.push(match[0]);
}
return matches;
}

test('should match simple .js files', () => {
deepStrictEqual(findJsFiles('node bootstrap.js'), ['bootstrap.js']);
});

test('should match multiple .js files', () => {
deepStrictEqual(findJsFiles('node server.js --require helper.js'), ['server.js', 'helper.js']);
});

test('should match .js files with hyphens', () => {
deepStrictEqual(findJsFiles('node my-script.js'), ['my-script.js']);
});

test('should not match .json files', () => {
deepStrictEqual(findJsFiles('cat package.json'), []);
});

test('should not match .js prefix in .json extension (regression test for \\b fix)', () => {
// Without the \b word boundary, the regex would incorrectly match "package.js" from "package.json"
deepStrictEqual(findJsFiles('node --config tsconfig.json'), []);
deepStrictEqual(findJsFiles('eslint.json'), []);
});

test('should not match .jsx files', () => {
deepStrictEqual(findJsFiles('node component.jsx'), []);
});

test('should match .js but not .json in same command', () => {
deepStrictEqual(findJsFiles('node app.js --config settings.json'), ['app.js']);
});

test('should not match partial matches inside other extensions', () => {
deepStrictEqual(findJsFiles('file.jsmith'), []);
});

test('should match .js at end of command', () => {
deepStrictEqual(findJsFiles('/path/to/script.js'), ['script.js']);
});
});
});

Loading