From c8b645b7ffae7ff265f50144c83a6d09003b2f26 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:53:54 -0500 Subject: [PATCH] test(process): consolidate Darwin compiler diagnostics and module cache checks (#12896) --- .../darwinProcessRunTokenReader.test.ts | 347 ++++++++---------- 1 file changed, 143 insertions(+), 204 deletions(-) diff --git a/tests/boundary/process/darwinProcessRunTokenReader.test.ts b/tests/boundary/process/darwinProcessRunTokenReader.test.ts index 107972a986..a2ff12b1c0 100644 --- a/tests/boundary/process/darwinProcessRunTokenReader.test.ts +++ b/tests/boundary/process/darwinProcessRunTokenReader.test.ts @@ -279,171 +279,138 @@ const preparationFailure = async ( return cause as DarwinProcessOwnershipInspectionError; }; +const scriptedCompilerFailure = async ( + source: string, + compileTimeoutMs?: number, +): Promise => { + const directory = await createTestTempDirectory("rea-compiler-failure-"); + const executable = join(directory, "fixture-xcrun"); + await writeFile( + executable, + `#!/usr/bin/env node +${source} +`, + ); + await chmod(executable, 0o755); + const reader = createDarwinProcessRunTokenReader({ + compiler: { kind: "xcrun", executable }, + ...(compileTimeoutMs === undefined ? {} : { compileTimeoutMs }), + }); + try { + return await preparationFailure(reader); + } finally { + await reader.close(); + } +}; + it.skipIf(process.platform === "win32")( - "reports the compiler exit code with its diagnostics when preparation fails", + "delivers the compiler exit and capped UTF-8 output through capture failure", async () => { - const directory = await createTestTempDirectory( - "rea-process-token-compiler-failure-test-", + const diagnostic = "module cache is not writable\n"; + const retained = diagnostic + "x".repeat(8191 - diagnostic.length); + const emitted = retained + "éafter"; + const failure = await scriptedCompilerFailure( + `process.stderr.write(${JSON.stringify(emitted)}); process.exitCode = 1;`, ); - const executable = join(directory, "failing-xcrun"); - await writeFile( - executable, - '#!/usr/bin/env node\nprocess.stderr.write("module cache is not writable\\n" + "x".repeat(12000)); process.exitCode = 1;\n', - ); - await chmod(executable, 0o755); - const reader = createDarwinProcessRunTokenReader({ - compiler: { kind: "xcrun", executable: executable }, + expect(failure.compilerOutcome).toMatchObject({ + outcome: "exited", + code: 1, + signal: null, + stderr: retained, + stderrBytes: Buffer.byteLength(emitted), + stderrTruncated: true, + killed: false, + }); + expect(failure.message).toMatch( + /exit code 1.*module cache is not writable/iu, + ); + expect(failure.message).toMatch( + /diagnostics truncated at 512 bytes.*no runtime override/iu, + ); + const normalized = normalizeCaptureFailure(failure); + if (!(normalized instanceof ProcessCaptureError)) + throw new Error("Expected a normalized process capture error"); + expect(projectAnalysisError(normalized)).toMatchObject({ + code: "process_capture_failed", + details: { + execution_failure: expect.stringContaining( + "compiler result: exit code 1", + ), + captured_output: { stderr: retained, exit_code: 1, truncated: true }, + }, }); - try { - const failure = await preparationFailure(reader); - expect(failure.compilerOutcome).toMatchObject({ - outcome: "exited", - code: 1, - signal: null, - stderr: expect.stringMatching(/^module cache is not writable\n/u), - stderrTruncated: true, - killed: false, - }); - expect(failure.message).toMatch( - /exit code 1.*module cache is not writable/iu, - ); - expect(failure.message).toMatch( - /diagnostics truncated at 512 bytes.*no runtime override/iu, - ); - const normalized = normalizeCaptureFailure(failure); - if (!(normalized instanceof ProcessCaptureError)) - throw new Error("Expected a normalized process capture error"); - expect(projectAnalysisError(normalized)).toMatchObject({ - code: "process_capture_failed", - details: { - execution_failure: expect.stringContaining( - "compiler result: exit code 1", - ), - captured_output: { - stderr: expect.stringContaining("module cache is not writable"), - exit_code: 1, - truncated: true, - }, - }, - }); - } finally { - await reader.close(); - } }, ); it.skipIf(process.platform === "win32")( "reports compiler diagnostics captured before the compilation deadline", async () => { - const directory = await createTestTempDirectory( - "rea-process-token-compiler-timeout-test-", + const diagnostic = + "error: PCH was compiled with module cache path that differs"; + const failure = await scriptedCompilerFailure( + `process.stderr.write(${JSON.stringify(diagnostic)}, () => { setInterval(() => {}, 1000); });`, + 3_000, ); - const executable = join(directory, "hanging-xcrun"); - await writeFile( - executable, - '#!/usr/bin/env node\nprocess.stderr.write("error: PCH was compiled with module cache path that differs\\n", () => { setInterval(() => {}, 1000); });\n', - ); - await chmod(executable, 0o755); - const reader = createDarwinProcessRunTokenReader({ - compiler: { kind: "xcrun", executable: executable }, - // Headroom for the child interpreter's cold start under suite load. - compileTimeoutMs: 3_000, + expect(failure.compileTimeout?.limit).toEqual({ + control_kind: "environment", + environment: "REA_SWIFTC_COMPILE_TIMEOUT_MS", + effective_value: 3_000, + unit: "ms", + minimum: 1, + maximum: 2_147_483_647, + default_value: 120_000, + applies_at: "next_startup", + }); + expect(failure.message).toMatch( + /ownership helper.*xcrun.*timed out after 3000 ms/iu, + ); + expect(failure.message).toContain(diagnostic); + expect(failure.compilerOutcome).toMatchObject({ + outcome: "timed_out", + code: "ETIMEDOUT", + timeoutMs: 3_000, + stderr: diagnostic, }); - try { - const failure = await preparationFailure(reader); - expect(failure.message).toMatch( - /ownership helper.*xcrun.*timed out after 3000 ms/iu, - ); - expect(failure.compileTimeout?.limit).toEqual({ - control_kind: "environment", - environment: "REA_SWIFTC_COMPILE_TIMEOUT_MS", - effective_value: 3_000, - unit: "ms", - minimum: 1, - maximum: 2_147_483_647, - default_value: 120_000, - applies_at: "next_startup", - }); - expect(failure.message).toContain( - "error: PCH was compiled with module cache path that differs", - ); - expect(failure.compilerOutcome).toMatchObject({ - outcome: "timed_out", - code: "ETIMEDOUT", - timeoutMs: 3_000, - }); - } finally { - await reader.close(); - } }, ); it.skipIf(process.platform === "win32")( "reports the stop signal when the compiler dies without an exit code", async () => { - const directory = await createTestTempDirectory( - "rea-process-token-compiler-signal-test-", + const diagnostic = "compiler stopped during type checking"; + const failure = await scriptedCompilerFailure( + `process.stderr.write(${JSON.stringify(diagnostic)}, () => process.kill(process.pid, "SIGTERM"));`, ); - const executable = join(directory, "segv-xcrun"); - await writeFile( - executable, - '#!/usr/bin/env node\nprocess.stderr.write("fatal: segmentation fault during type checking\\n");\nprocess.kill(process.pid, "SIGSEGV");\n', - ); - await chmod(executable, 0o755); - const reader = createDarwinProcessRunTokenReader({ - compiler: { kind: "xcrun", executable: executable }, + expect(failure).toMatchObject({ + message: expect.stringMatching( + /xcrun.*SIGTERM.*compiler stopped during type checking/u, + ), + compilerOutcome: { + outcome: "signaled", + code: null, + signal: "SIGTERM", + stderr: diagnostic, + }, }); - try { - await expect(preparationFailure(reader)).resolves.toMatchObject({ - message: expect.stringMatching( - /xcrun.*SIGSEGV.*fatal: segmentation fault during type checking/i, - ), - compilerOutcome: { - outcome: "signaled", - code: null, - signal: "SIGSEGV", - }, - }); - } finally { - await reader.close(); - } }, ); it.skipIf(process.platform === "win32")( - "bounds embedded compiler diagnostics and marks the truncation", + "bounds only the diagnostic message when captured output fits", async () => { - const directory = await createTestTempDirectory( - "rea-process-token-compiler-verbose-test-", + const prefix = "x".repeat(511); + const diagnostic = prefix + "é" + "tail".repeat(25) + "\n"; + const failure = await scriptedCompilerFailure( + `process.stderr.write(${JSON.stringify(diagnostic)}); process.exitCode = 1;`, ); - const executable = join(directory, "verbose-xcrun"); - const diagnostic = "x".repeat(600); - await writeFile( - executable, - `#!/usr/bin/env node\nprocess.stderr.write(${JSON.stringify(`${diagnostic}\n`)}); process.exitCode = 1;\n`, - ); - await chmod(executable, 0o755); - const reader = createDarwinProcessRunTokenReader({ - compiler: { kind: "xcrun", executable: executable }, + expect(failure.compilerOutcome).toMatchObject({ + stderr: diagnostic, + stderrBytes: Buffer.byteLength(diagnostic), + stderrTruncated: false, }); - try { - const failure = await preparationFailure(reader); - const outcome = failure.compilerOutcome; - if (outcome === undefined) throw new Error("Expected compiler outcome"); - expect(outcome.stderrBytes).toBe(601); - expect(outcome.stderrTruncated).toBe(false); - expect(failure.message).toContain( - `: ${"x".repeat(512)}… [diagnostics truncated at 512 bytes; this release has no runtime override]`, - ); - const excerpt = failure.message.split("): ")[1]?.split("…")[0]; - expect(excerpt).toBe("x".repeat(512)); - expect(Buffer.byteLength(excerpt ?? "", "utf8")).toBeLessThanOrEqual(512); - expect(Buffer.byteLength(outcome.stderr, "utf8")).toBeLessThanOrEqual( - 8 * 1024, - ); - } finally { - await reader.close(); - } + expect(failure.message).toContain( + `: ${prefix}… [diagnostics truncated at 512 bytes; this release has no runtime override]`, + ); }, ); @@ -483,28 +450,6 @@ it }, ); -it.skipIf(process.platform === "win32")( - "uses the compiler-managed module cache instead of rebuilding system modules per REA process", - async () => { - const directory = await createTestTempDirectory( - "rea-process-token-module-cache-test-", - ); - const fakeCompiler = await writeBlockingCompiler(directory); - const reader = createDarwinProcessRunTokenReader({ - compiler: { kind: "xcrun", executable: fakeCompiler.executable }, - }); - const preparation = reader.prepare(); - try { - const state = await waitForCompileState(fakeCompiler.statePath); - expect(state.arguments).not.toContain("-module-cache-path"); - await writeFile(fakeCompiler.releasePath, "release"); - await preparation; - } finally { - await Promise.all([reader.close(), Promise.allSettled([preparation])]); - } - }, -); - const writeCacheRecordingCompiler = async (directory: string) => { const executable = join(directory, "fake-cache-xcrun"); const statePath = join(directory, "cache-compile-state.json"); @@ -621,58 +566,52 @@ fs.chmodSync(output, 0o755); }, ); -it.skipIf(process.platform === "win32")( - "forwards a caller-owned module cache and keeps it after the helper root is removed", - async () => { +it + .skipIf(process.platform === "win32") + .each(["explicit option", "launch environment"] as const)( + "uses the caller-owned module cache from the %s and preserves it on close", + async (source) => { const directory = await createTestTempDirectory( - "rea-process-token-caller-cache-test-", + "rea-compiler-module-cache-", ); const cachePath = join(directory, "caller", " modules "); const fakeCompiler = await writeCacheRecordingCompiler(directory); - const reader = createDarwinProcessRunTokenReader({ - compiler: { kind: "xcrun", executable: fakeCompiler.executable }, - moduleCachePath: cachePath, - }); - let executable: string; + const compiler = { + kind: "xcrun", + executable: fakeCompiler.executable, + } as const; + const reader = + source === "explicit option" + ? createDarwinProcessRunTokenReader({ + compiler, + moduleCachePath: cachePath, + }) + : createSystemProcessOwnershipHost( + "darwin", + { ...process.env, REA_DARWIN_MODULE_CACHE_PATH: cachePath }, + { darwinCompiler: compiler }, + ); + let executable: string | undefined; try { - executable = await reader.prepare(); - } finally { - await reader.close(); - } - const state = await readCacheCompileState(fakeCompiler.statePath); - const flagIndex = state.arguments.indexOf("-module-cache-path"); - expect(state.arguments[0]).toBe("swiftc"); - expect(flagIndex).toBe(1); - expect(state.arguments[flagIndex + 1]).toBe(cachePath); - expect(state.moduleCacheEnvironment).toBe(cachePath); - expect(await existsPath(join(cachePath, "written-into-cache"))).toBe(true); - expect(await existsPath(executable)).toBe(false); - }, -); - -it.skipIf(process.platform === "win32")( - "selects the caller module cache through REA_DARWIN_MODULE_CACHE_PATH", - async () => { - const directory = await createTestTempDirectory( - "rea-process-token-host-cache-test-", - ); - const cachePath = join(directory, "from-environment", "modules"); - const fakeCompiler = await writeCacheRecordingCompiler(directory); - const host = createSystemProcessOwnershipHost( - "darwin", - { ...process.env, REA_DARWIN_MODULE_CACHE_PATH: cachePath }, - { - darwinCompiler: { kind: "xcrun", executable: fakeCompiler.executable }, - }, - ); - try { - await host.prepare?.(AbortSignal.timeout(15_000)); + await reader.prepare?.(); const state = await readCacheCompileState(fakeCompiler.statePath); - const flagIndex = state.arguments.indexOf("-module-cache-path"); - expect(state.arguments[flagIndex + 1]).toBe(cachePath); + expect(state.arguments.slice(0, 3)).toEqual([ + "swiftc", + "-module-cache-path", + cachePath, + ]); + expect(state.moduleCacheEnvironment).toBe(cachePath); + executable = state.arguments[state.arguments.indexOf("-o") + 1]; + if (executable === undefined) + throw new Error("Expected compiler output path"); + await access(executable); } finally { - await host.close?.(); + await reader.close?.(); } + await expect(access(executable)).rejects.toMatchObject({ code: "ENOENT" }); + expect(await readFile(join(cachePath, "written-into-cache"), "utf8")).toBe( + "kept", + ); }, );