fix(process): add an opt-in Swift module cache (#2252)

* fix(process): add an opt-in Swift module cache

Signed-off-by: Charan Rathore <180254320+charan-rathore@users.noreply.github.com>

* fix(process): preserve caller-owned Swift module caches

Fold the complementary implementation from #2260 into the earlier #2252 proposal, retaining the original contribution history.

Co-authored-by: NIU-123370 <191000457+NIU-123370@users.noreply.github.com>

* fix(process): bypass xcrun lookup for explicit Swift toolchains

---------

Signed-off-by: Charan Rathore <180254320+charan-rathore@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: NIU-123370 <191000457+NIU-123370@users.noreply.github.com>
This commit is contained in:
Charan Rathore
2026-10-13 02:26:16 +08:00
committed by GitHub
co-authored by morluto NIU-123370
parent f941968eb1
commit 47a51b08f4
4 changed files with 392 additions and 38 deletions
+58 -18
View File
@@ -1,7 +1,7 @@
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { isAbsolute, join } from "node:path";
import { fileURLToPath } from "node:url";
import { z } from "zod";
@@ -163,14 +163,19 @@ const compilerOutcomeDescription = (
const compilerInspectionError = (
cause: unknown,
timeoutMs: number,
compiler: DarwinProcessReaderCompiler,
): DarwinProcessOwnershipInspectionError => {
const outcome = compilerOutcome(cause, timeoutMs);
const code = outcome?.code ?? "unknown";
const description = compilerOutcomeDescription(outcome, code);
const launcher =
compiler.kind === "xcrun"
? "xcrun"
: "the selected Swift compiler (REA_DARWIN_SWIFTC_PATH)";
const prefix =
code === "ENOENT"
? "macOS process ownership inspection requires the Apple Swift compiler via xcrun"
: "macOS process ownership helper compilation failed via xcrun";
? `macOS process ownership inspection requires the Apple Swift compiler via ${launcher}`
: `macOS process ownership helper compilation failed via ${launcher}`;
const stderr = boundedUtf8Text(outcome?.stderr ?? "", STDERR_EXCERPT_LIMIT);
const diagnostics = stderr.text.trim();
const truncated =
@@ -235,15 +240,35 @@ const waitForCompilation = async (
});
};
/** Explicit tool selection; direct Swift compilation also needs its matching SDK. */
export type DarwinProcessReaderCompiler =
| { readonly kind: "xcrun"; readonly executable: string }
| {
readonly kind: "swiftc";
readonly executable: string;
readonly sdkRoot: string;
};
/** Compile and retain the narrow Darwin sysctl reader for this REA process. */
export const createDarwinProcessRunTokenReader = (
options: {
readonly xcrun?: string;
readonly compiler?: DarwinProcessReaderCompiler;
/** Deadline for the helper compilation; the default keeps verification fast. */
readonly compileTimeoutMs?: number;
/**
* Caller-owned directory holding the compiler's module cache. Unset keeps
* Swift's reusable default cache; a supplied directory outlives this
* reader because cleanup only removes the per-run helper root.
*/
readonly moduleCachePath?: string;
} = {},
) => {
const compiler = options.compiler ?? {
kind: "xcrun",
executable: "/usr/bin/xcrun",
};
const compilerTimeoutMs = options.compileTimeoutMs ?? COMPILER_TIMEOUT_MS;
const moduleCachePath = options.moduleCachePath ?? "";
let root: string | undefined;
let executable: string | undefined;
let compilation: ProcessReaderCompilation | undefined;
@@ -266,6 +291,14 @@ export const createDarwinProcessRunTokenReader = (
const runCompilation = async (signal: AbortSignal): Promise<string> => {
let operationRoot: string | undefined;
try {
if (
compiler.kind === "swiftc" &&
(!isAbsolute(compiler.executable) || !isAbsolute(compiler.sdkRoot))
) {
throw new DarwinProcessOwnershipInspectionError(
"Direct Darwin helper compilation requires absolute REA_DARWIN_SWIFTC_PATH and SDKROOT paths selecting a compatible installed Swift compiler and macOS SDK.",
);
}
operationRoot = await mkdtemp(join(tmpdir(), "rea-process-token-"));
root = operationRoot;
process.once("exit", cleanupAtExit);
@@ -280,23 +313,30 @@ export const createDarwinProcessRunTokenReader = (
...process.env,
TMPDIR: compilerTemporary,
};
// Use Swift's reusable default cache for REA's internal helper compilation.
delete compilerEnvironment.CLANG_MODULE_CACHE_PATH;
const compilerArguments =
compiler.kind === "xcrun" ? ["swiftc"] : ["-sdk", compiler.sdkRoot];
if (compiler.kind === "swiftc")
compilerEnvironment.SDKROOT = compiler.sdkRoot;
if (moduleCachePath === "") {
// Use Swift's reusable default cache for REA's internal helper compilation.
delete compilerEnvironment.CLANG_MODULE_CACHE_PATH;
} else {
await mkdir(moduleCachePath, { recursive: true });
compilerArguments.push("-module-cache-path", moduleCachePath);
compilerEnvironment.CLANG_MODULE_CACHE_PATH = moduleCachePath;
}
compilerArguments.push(...sourceFiles, "-o", output);
try {
await execFileOutput(
options.xcrun ?? "/usr/bin/xcrun",
["swiftc", ...sourceFiles, "-o", output],
{
timeout: compilerTimeoutMs,
maxBuffer: 1024 * 1024,
signal,
stopSignal: SWIFTC_INTERRUPT,
env: compilerEnvironment,
},
);
await execFileOutput(compiler.executable, compilerArguments, {
timeout: compilerTimeoutMs,
maxBuffer: 1024 * 1024,
signal,
stopSignal: SWIFTC_INTERRUPT,
env: compilerEnvironment,
});
} catch (cause: unknown) {
if (isExpectedAbort(cause, signal)) throw cause;
throw compilerInspectionError(cause, compilerTimeoutMs);
throw compilerInspectionError(cause, compilerTimeoutMs, compiler);
}
if (signal.aborted || closed) throw abortReason(signal);
executable = output;
+25 -6
View File
@@ -4,7 +4,10 @@ import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore.
import { launcherIdentityFailure } from "./ProcessOwnershipIdentity.js";
import { descendantsOf, liveProcesses } from "./ProcessOwnershipProcessTree.js";
import { execFileOutput } from "./ExecFileOutput.js";
import { createDarwinProcessRunTokenReader } from "./DarwinProcessRunTokenReader.js";
import {
createDarwinProcessRunTokenReader,
type DarwinProcessReaderCompiler,
} from "./DarwinProcessRunTokenReader.js";
import { readProcessRunTokens } from "./ProcessRunTokenObservations.js";
import type {
OwnedProcessGroup,
@@ -219,16 +222,32 @@ export const createSystemProcessOwnershipHost = (
hostEnvironment: NodeJS.ProcessEnv = process.env,
options: {
/** Override the compiler executable used by the Darwin native reader. */
readonly darwinXcrun?: string;
readonly darwinCompiler?: DarwinProcessReaderCompiler;
/** Override the caller-owned module cache used compiling that helper. */
readonly darwinModuleCachePath?: string;
} = {},
): ProcessOwnershipHost => {
const darwinModuleCachePath =
options.darwinModuleCachePath ??
hostEnvironment.REA_DARWIN_MODULE_CACHE_PATH;
const swiftcPath = hostEnvironment.REA_DARWIN_SWIFTC_PATH;
const darwinCompiler =
options.darwinCompiler ??
(swiftcPath === undefined || swiftcPath === ""
? undefined
: {
kind: "swiftc" as const,
executable: swiftcPath,
sdkRoot: hostEnvironment.SDKROOT ?? "",
});
const darwinTokens =
platform === "darwin"
? createDarwinProcessRunTokenReader(
options.darwinXcrun === undefined
? createDarwinProcessRunTokenReader({
...(darwinCompiler === undefined ? {} : { compiler: darwinCompiler }),
...(darwinModuleCachePath === undefined
? {}
: { xcrun: options.darwinXcrun },
)
: { moduleCachePath: darwinModuleCachePath }),
})
: undefined;
const listProcesses = async (signal?: AbortSignal) => {
if (platform === "win32") return [];