diff --git a/packages/core/src/ide/process-utils.ts b/packages/core/src/ide/process-utils.ts index 451a3af3..ecb93781 100644 --- a/packages/core/src/ide/process-utils.ts +++ b/packages/core/src/ide/process-utils.ts @@ -24,39 +24,44 @@ async function getProcessInfo(pid: number): Promise<{ name: string; command: string; }> { - const platform = os.platform(); - if (platform === 'win32') { - const powershellCommand = [ - '$p = Get-CimInstance Win32_Process', - `-Filter 'ProcessId=${pid}'`, - '-ErrorAction SilentlyContinue;', - 'if ($p) {', - '@{Name=$p.Name;ParentProcessId=$p.ParentProcessId;CommandLine=$p.CommandLine}', - '| ConvertTo-Json', - '}', - ].join(' '); - const { stdout } = await execAsync(`powershell "${powershellCommand}"`); - const output = stdout.trim(); - if (!output) return { parentPid: 0, name: '', command: '' }; - const { - Name = '', - ParentProcessId = 0, - CommandLine = '', - } = JSON.parse(output); - return { parentPid: ParentProcessId, name: Name, command: CommandLine }; - } else { - const command = `ps -o ppid=,command= -p ${pid}`; - const { stdout } = await execAsync(command); - const trimmedStdout = stdout.trim(); - const ppidString = trimmedStdout.split(/\s+/)[0]; - const parentPid = parseInt(ppidString, 10); - const fullCommand = trimmedStdout.substring(ppidString.length).trim(); - const processName = path.basename(fullCommand.split(' ')[0]); - return { - parentPid: isNaN(parentPid) ? 1 : parentPid, - name: processName, - command: fullCommand, - }; + try { + const platform = os.platform(); + if (platform === 'win32') { + const powershellCommand = [ + '$p = Get-CimInstance Win32_Process', + `-Filter 'ProcessId=${pid}'`, + '-ErrorAction SilentlyContinue;', + 'if ($p) {', + '@{Name=$p.Name;ParentProcessId=$p.ParentProcessId;CommandLine=$p.CommandLine}', + '| ConvertTo-Json', + '}', + ].join(' '); + const { stdout } = await execAsync(`powershell "${powershellCommand}"`); + const output = stdout.trim(); + if (!output) return { parentPid: 0, name: '', command: '' }; + const { + Name = '', + ParentProcessId = 0, + CommandLine = '', + } = JSON.parse(output); + return { parentPid: ParentProcessId, name: Name, command: CommandLine }; + } else { + const command = `ps -o ppid=,command= -p ${pid}`; + const { stdout } = await execAsync(command); + const trimmedStdout = stdout.trim(); + const ppidString = trimmedStdout.split(/\s+/)[0]; + const parentPid = parseInt(ppidString, 10); + const fullCommand = trimmedStdout.substring(ppidString.length).trim(); + const processName = path.basename(fullCommand.split(' ')[0]); + return { + parentPid: isNaN(parentPid) ? 1 : parentPid, + name: processName, + command: fullCommand, + }; + } + } catch (_e) { + console.debug(`Failed to get process info for pid ${pid}:`, _e); + return { parentPid: 0, name: '', command: '' }; } } @@ -169,7 +174,6 @@ async function getIdeProcessInfoForWindows(): Promise<{ * top-level ancestor process ID and command as a fallback. * * @returns A promise that resolves to the PID and command of the IDE process. - * @throws Will throw an error if the underlying shell commands fail. */ export async function getIdeProcessInfo(): Promise<{ pid: number;