From 05397b4c774862bf73106dc009c3f375746143dd Mon Sep 17 00:00:00 2001 From: "Hana (Hyang-Ah) Kim" Date: Wed, 17 Jun 2020 18:39:04 -0400 Subject: [PATCH] prefer /usr/bin/pgrep to other pgrep if available Use the default pgrep, available since os x mountain lion. proctools' pgrep does not implement `-P` correctly, returns unrelated processes, breaks tree-kill's assumption, and may cause a large number of pgrep processes. Reported in https://github.com/pkrumins/node-tree-kill/issues/17#issuecomment-484206992 Update https://github.com/golang/vscode-go/issues/90#issuecomment-634430428 --- index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8df6a0f..9499a89 100755 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; var childProcess = require('child_process'); +const { existsSync } = require('fs'); var spawn = childProcess.spawn; var exec = childProcess.exec; @@ -30,7 +31,7 @@ module.exports = function (pid, signal, callback) { break; case 'darwin': buildProcessTree(pid, tree, pidsToProcess, function (parentPid) { - return spawn('pgrep', ['-P', parentPid]); + return spawn(pathToPgrep(), ['-P', parentPid]); }, function () { killAll(tree, signal, callback); }); @@ -116,3 +117,20 @@ function buildProcessTree (parentPid, tree, pidsToProcess, spawnChildProcessesLi ps.on('close', onClose); } + +var pgrep = ''; +function pathToPgrep () { + if (pgrep) { + return pgrep; + } + // Use the default pgrep, available since os x mountain lion. + // proctools' pgrep does not implement `-P` correctly and returns + // unrelated processes. + // https://github.com/golang/vscode-go/issues/90#issuecomment-634430428 + try { + pgrep = existsSync('/usr/bin/pgrep') ? '/usr/bin/pgrep' : 'pgrep'; + } catch (e) { + pgrep = 'pgrep'; + } + return pgrep; +} \ No newline at end of file