From bf28864c1fdab1a55f9dadd79f63f2235b43f83a Mon Sep 17 00:00:00 2001 From: "Hana (Hyang-Ah) Kim" Date: Wed, 17 Jun 2020 22:22:34 +0000 Subject: [PATCH] third_party/tree-kill: always use the default pgrep on mac The proctools' pgrep is problematic when running with -P. Fixes golang/vscode-go#90 Change-Id: If3e306cd725a7c88873ca40d74c1581e2d695ea4 Change-Id: If3e306cd725a7c88873ca40d74c1581e2d695ea4 GitHub-Last-Rev: 2933f719f16735de1e841d31113d1f8759a7b050 GitHub-Pull-Request: golang/vscode-go#174 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/236538 Reviewed-by: Rebecca Stambler --- third_party/tree-kill/index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/third_party/tree-kill/index.js b/third_party/tree-kill/index.js index 8df6a0fb2f..9499a8973c 100755 --- a/third_party/tree-kill/index.js +++ b/third_party/tree-kill/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