fix(process): capture complete host probe output

This commit is contained in:
morluto
2026-09-29 17:36:04 +08:00
parent dfad09e3b5
commit 6c68ff565f
8 changed files with 68 additions and 24 deletions
+6 -8
View File
@@ -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<string | undefined> => {
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<boolean> => {
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;
+3 -1
View File
@@ -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))
+2 -5
View File
@@ -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<readonly ProcessRow[]> => {
const { stdout } = await execFileAsync(
const { stdout } = await execFileOutput(
"ps",
["-axo", "pid=,ppid=,pgid=,sess=,command="],
{ signal },
+2 -7
View File
@@ -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 }),
},
+6 -1
View File
@@ -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<boolean> => {
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);
+16
View File
@@ -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);
});
});
+30
View File
@@ -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 });
},
);
});
+3 -2
View File
@@ -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<string, string> = {};
for (const match of stdout.matchAll(
/(?:^|\s)([A-Za-z_][A-Za-z0-9_]*)=([^\s]*)/gu,