From 6c68ff565fe1196fd8c7a63598e8bd94098abc3c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 29 Sep 2026 17:36:04 +0800 Subject: [PATCH] fix(process): capture complete host probe output --- src/application/Doctor.ts | 14 +++++------ src/application/LinuxHopper.ts | 4 +++- src/application/ProcessSampling.ts | 7 ++---- src/artifacts/NativeDmgArtifactReader.ts | 9 ++----- src/hopper/BridgeLauncher.ts | 7 +++++- src/process/ExecFileOutput.test.ts | 16 +++++++++++++ src/process/ExecFileOutput.ts | 30 ++++++++++++++++++++++++ src/process/ProcessOwnership.ts | 5 ++-- 8 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 src/process/ExecFileOutput.test.ts create mode 100644 src/process/ExecFileOutput.ts diff --git a/src/application/Doctor.ts b/src/application/Doctor.ts index 13927d3ef..190711c42 100644 --- a/src/application/Doctor.ts +++ b/src/application/Doctor.ts @@ -2,10 +2,9 @@ import { constants } from "node:fs"; import { access, readFile, readdir } from "node:fs/promises"; import { homedir } from "node:os"; import { delimiter, join } from "node:path"; -import { execFile } from "node:child_process"; -import { promisify } from "node:util"; import { parseBinaryTarget } from "./BinaryTargetResolver.js"; +import { execFileOutput } from "../process/ExecFileOutput.js"; import type { JsonValue } from "../domain/jsonValue.js"; import { probeHomebrew } from "./homebrew.js"; import { @@ -35,7 +34,6 @@ import { type DoctorScopeReport, } from "./DoctorScope.js"; -const execFileAsync = promisify(execFile); /** Deep provider IDs whose readiness can be selected explicitly by doctor. */ export const DOCTOR_PROVIDER_IDS = ["hopper", "ghidra"] as const; /** One actionable environment diagnostic. */ @@ -324,7 +322,7 @@ export const systemDoctorHost = ( return probeHomebrew(async (command) => { try { const prefix = ( - await execFileAsync(command, [ + await execFileOutput(command, [ "--prefix", "--cask", "hopper-disassembler", @@ -347,7 +345,7 @@ export const systemDoctorHost = ( try { await access(path, constants.X_OK); return firstIlspyVersionLine( - (await execFileAsync(path, ["--version"], { timeout: 10_000 })).stdout, + (await execFileOutput(path, ["--version"], { timeout: 10_000 })).stdout, ); } catch { return undefined; @@ -366,7 +364,7 @@ export const systemDoctorHost = ( try { const command = process.platform === "win32" ? "where" : "which"; const arguments_ = process.platform === "win32" ? ["rea"] : ["-a", "rea"]; - return uniqueLines((await execFileAsync(command, arguments_)).stdout); + return uniqueLines((await execFileOutput(command, arguments_)).stdout); } catch { return []; } @@ -377,7 +375,7 @@ export const systemDoctorHost = ( const readMacosVersion = async (): Promise => { try { - return (await execFileAsync("sw_vers", ["-productVersion"])).stdout.trim(); + return (await execFileOutput("sw_vers", ["-productVersion"])).stdout.trim(); } catch { return undefined; } @@ -387,7 +385,7 @@ const executableAvailable = async (path: string): Promise => { try { await access(path, constants.X_OK); if (process.platform !== "linux") return true; - const linked = await execFileAsync("ldd", [path]); + const linked = await execFileOutput("ldd", [path]); return linuxSharedLibrariesAvailable(`${linked.stdout}\n${linked.stderr}`); } catch { return false; diff --git a/src/application/LinuxHopper.ts b/src/application/LinuxHopper.ts index f4d0cac25..3f1beeb88 100644 --- a/src/application/LinuxHopper.ts +++ b/src/application/LinuxHopper.ts @@ -6,6 +6,8 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { promisify } from "node:util"; +import { execFileOutput } from "../process/ExecFileOutput.js"; + const execFileAsync = promisify(execFile); const DOWNLOAD_PREFIX = "https://www.hopperapp.com:443/downloader/public/"; const MAX_PACKAGE_BYTES = 100_000_000; @@ -236,7 +238,7 @@ const systemLinuxHopperInstallHost = (): LinuxHopperInstallHost => ({ async launcherStatus(path) { try { await access(path); - const linked = await execFileAsync("ldd", [path]); + const linked = await execFileOutput("ldd", [path]); if (!linuxSharedLibrariesAvailable(`${linked.stdout}\n${linked.stderr}`)) return "runtime_dependencies"; return (await linuxHopperBinarySupported(path)) diff --git a/src/application/ProcessSampling.ts b/src/application/ProcessSampling.ts index 933ba60f5..6aa000c4d 100644 --- a/src/application/ProcessSampling.ts +++ b/src/application/ProcessSampling.ts @@ -1,13 +1,10 @@ -import { execFile } from "node:child_process"; import { readFile, readdir } from "node:fs/promises"; -import { promisify } from "node:util"; import type { ProcessSample, RecordProcessCaptureEvent, } from "../domain/processCapture.js"; +import { execFileOutput } from "../process/ExecFileOutput.js"; import { readProcessRunId } from "../process/ProcessOwnership.js"; - -const execFileAsync = promisify(execFile); const PROC_READ_CONCURRENCY = 64; interface ProcessRow { @@ -311,7 +308,7 @@ const sampleLinux = async ( const readPsRows = async ( signal: AbortSignal, ): Promise => { - const { stdout } = await execFileAsync( + const { stdout } = await execFileOutput( "ps", ["-axo", "pid=,ppid=,pgid=,sess=,command="], { signal }, diff --git a/src/artifacts/NativeDmgArtifactReader.ts b/src/artifacts/NativeDmgArtifactReader.ts index 7c6bb9fd8..32dcea7ca 100644 --- a/src/artifacts/NativeDmgArtifactReader.ts +++ b/src/artifacts/NativeDmgArtifactReader.ts @@ -1,9 +1,7 @@ -import { execFile } from "node:child_process"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { basename, join } from "node:path"; import type { Readable } from "node:stream"; -import { promisify } from "node:util"; import { parse } from "plist"; import { z } from "zod"; @@ -15,8 +13,7 @@ import { type ArtifactReader, } from "./ArtifactReader.js"; import { DirectoryArtifactReader } from "./DirectoryArtifactReader.js"; - -const execFileAsync = promisify(execFile); +import { execFileOutput } from "../process/ExecFileOutput.js"; const DETACH_TIMEOUT_MS = 120_000; const attachOutputSchema = z.object({ "system-entities": z.array( @@ -39,12 +36,10 @@ export interface NativeDmgHost { const systemHost: NativeDmgHost = { async run(arguments_, signal, options) { try { - const { stdout } = await execFileAsync( + const { stdout } = await execFileOutput( "/usr/bin/hdiutil", [...arguments_], { - encoding: "utf8", - maxBuffer: Number.POSITIVE_INFINITY, ...(options === undefined ? {} : { timeout: options.timeoutMs }), ...(signal === undefined ? {} : { signal }), }, diff --git a/src/hopper/BridgeLauncher.ts b/src/hopper/BridgeLauncher.ts index 4597e587b..fbd75265f 100644 --- a/src/hopper/BridgeLauncher.ts +++ b/src/hopper/BridgeLauncher.ts @@ -10,6 +10,7 @@ import { } from "../domain/errors.js"; import { err, ok, type Result } from "../domain/result.js"; import { cleanupOwnedProcessGroup } from "../process/ProcessOwnership.js"; +import { execFileOutput } from "../process/ExecFileOutput.js"; import { type ProviderProcessLaunch, spawnOwnedProviderProcess, @@ -440,7 +441,11 @@ const hopperApplicationBundle = (launcherPath: string): string | undefined => { const processIsRunning = async (executablePath: string): Promise => { try { - const processes = await execFileAsync("/bin/ps", ["-ax", "-o", "command="]); + const processes = await execFileOutput("/bin/ps", [ + "-ax", + "-o", + "command=", + ]); return processes.stdout .split("\n") .some((command) => command.trim() === executablePath); diff --git a/src/process/ExecFileOutput.test.ts b/src/process/ExecFileOutput.test.ts new file mode 100644 index 000000000..bb5e50e14 --- /dev/null +++ b/src/process/ExecFileOutput.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; + +import { execFileOutput } from "./ExecFileOutput.js"; + +describe("execFileOutput", () => { + it("captures output larger than Node's default execFile limit", async () => { + const marker = "tail-marker"; + const result = await execFileOutput(process.execPath, [ + "-e", + `process.stdout.write("x".repeat(2 * 1024 * 1024)); process.stdout.write(${JSON.stringify(marker)});`, + ]); + + expect(result.stdout).toHaveLength(2 * 1024 * 1024 + marker.length); + expect(result.stdout.endsWith(marker)).toBe(true); + }); +}); diff --git a/src/process/ExecFileOutput.ts b/src/process/ExecFileOutput.ts new file mode 100644 index 000000000..dd25c4429 --- /dev/null +++ b/src/process/ExecFileOutput.ts @@ -0,0 +1,30 @@ +import { + execFile, + type ExecFileOptionsWithStringEncoding, +} from "node:child_process"; + +type ExecFileOutputOptions = Omit< + ExecFileOptionsWithStringEncoding, + "encoding" | "maxBuffer" +>; + +/** Run a shell-free command while capturing its complete UTF-8 output. */ +export const execFileOutput = ( + command: string, + arguments_: readonly string[], + options: ExecFileOutputOptions = {}, +): Promise<{ readonly stdout: string; readonly stderr: string }> => + new Promise((resolve, reject) => { + execFile( + command, + [...arguments_], + { ...options, encoding: "utf8", maxBuffer: Number.POSITIVE_INFINITY }, + (error, stdout, stderr) => { + if (error !== null) { + reject(error); + return; + } + resolve({ stdout, stderr }); + }, + ); + }); diff --git a/src/process/ProcessOwnership.ts b/src/process/ProcessOwnership.ts index 79618c89e..9c53b86c6 100644 --- a/src/process/ProcessOwnership.ts +++ b/src/process/ProcessOwnership.ts @@ -8,6 +8,7 @@ import { } from "./ProcessOwnershipObservation.js"; import { launcherIdentityFailure } from "./ProcessOwnershipIdentity.js"; import { descendantsOf, liveProcesses } from "./ProcessOwnershipProcessTree.js"; +import { execFileOutput } from "./ExecFileOutput.js"; const execFileAsync = promisify(execFile); @@ -159,7 +160,7 @@ export const parseProcessEnvironment = ( const systemHost: ProcessOwnershipHost = { async listProcesses() { if (process.platform === "win32") return []; - const { stdout } = await execFileAsync("ps", [ + const { stdout } = await execFileOutput("ps", [ "-axo", "pid=,ppid=,pgid=,stat=,command=", ]); @@ -181,7 +182,7 @@ const systemHost: ProcessOwnershipHost = { await readFile(`/proc/${pid}/environ`, "utf8"), ); } - const { stdout } = await execFileAsync("ps", ["eww", "-p", String(pid)]); + const { stdout } = await execFileOutput("ps", ["eww", "-p", String(pid)]); const environment: Record = {}; for (const match of stdout.matchAll( /(?:^|\s)([A-Za-z_][A-Za-z0-9_]*)=([^\s]*)/gu,