@@ -2,6 +2,7 @@ import { execFile, spawn, type ChildProcess } from "node:child_process";
22import {
33 access ,
44 chmod ,
5+ mkdir ,
56 mkdtemp ,
67 readFile ,
78 rm ,
@@ -14,6 +15,7 @@ import { fileURLToPath } from "node:url";
1415import { promisify } from "node:util" ;
1516import { _electron as electron } from "playwright-core" ;
1617import { expect , it , vi } from "vitest" ;
18+ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js" ;
1719
1820import {
1921 createSystemProcessOwnershipHost ,
@@ -41,10 +43,10 @@ const state = fs.existsSync(statePath)
4143 ? JSON.parse(fs.readFileSync(statePath, "utf8"))
4244 : { calls: 0 };
4345state.calls += 1;
44- const moduleIndex = process.argv.indexOf("-module-cache-path");
45- state.root = path.dirname(process.argv[moduleIndex + 1]);
4646const outputIndex = process.argv.indexOf("-o");
4747state.output = process.argv[outputIndex + 1];
48+ state.root = path.dirname(state.output);
49+ state.arguments = process.argv.slice(2);
4850state.releasePath = releasePath;
4951fs.writeFileSync(statePath, JSON.stringify(state));
5052if (state.calls === 1) {
@@ -106,6 +108,10 @@ const waitForCompileState = async (statePath: string) => {
106108 root : parsed . root ,
107109 output : parsed . output ,
108110 releasePath : parsed . releasePath ,
111+ arguments :
112+ "arguments" in parsed && Array . isArray ( parsed . arguments )
113+ ? parsed . arguments
114+ : [ ] ,
109115 } ;
110116 } catch {
111117 await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
@@ -161,6 +167,87 @@ it("reports an actionable missing Swift compiler without installing it", async (
161167 }
162168} ) ;
163169
170+ it . skipIf ( process . platform === "win32" ) (
171+ "preserves compiler diagnostics when Swift preparation fails" ,
172+ async ( ) => {
173+ const directory = await createTestTempDirectory (
174+ "rea-process-token-compiler-failure-test-" ,
175+ ) ;
176+ const executable = join ( directory , "failing-xcrun" ) ;
177+ await writeFile (
178+ executable ,
179+ '#!/usr/bin/env node\nprocess.stderr.write("module cache is not writable\\n"); process.exitCode = 1;\n' ,
180+ ) ;
181+ await chmod ( executable , 0o755 ) ;
182+ const reader = createDarwinProcessRunTokenReader ( { xcrun : executable } ) ;
183+ try {
184+ await expect ( reader . prepare ( ) ) . rejects . toThrow (
185+ / c o m p i l a t i o n f a i l e d .* m o d u l e c a c h e i s n o t w r i t a b l e / su,
186+ ) ;
187+ } finally {
188+ await reader . close ( ) ;
189+ }
190+ } ,
191+ ) ;
192+
193+ it
194+ . skipIf ( ! onDarwin || process . getuid ?.( ) === 0 )
195+ . each ( [ "file" , "read-only directory" ] ) (
196+ "ignores unusable inherited Swift module cache: %s" ,
197+ async ( kind ) => {
198+ const directory = await createTestTempDirectory (
199+ "rea-process-token-inherited-cache-test-" ,
200+ ) ;
201+ const cacheParent = join ( directory , "cache-parent" ) ;
202+ if ( kind === "file" ) await writeFile ( cacheParent , "not a directory" ) ;
203+ else {
204+ await mkdir ( cacheParent ) ;
205+ await chmod ( cacheParent , 0o500 ) ;
206+ }
207+ const cachePath = join ( cacheParent , "modules" ) ;
208+ vi . stubEnv ( "CLANG_MODULE_CACHE_PATH" , cachePath ) ;
209+ const reader = createDarwinProcessRunTokenReader ( ) ;
210+ try {
211+ const executable = await reader . prepare ( ) ;
212+ const { stdout } = await execFileOutput (
213+ executable ,
214+ [ "--identities" , String ( process . pid ) ] ,
215+ { timeout : 15_000 } ,
216+ ) ;
217+ expect ( JSON . parse ( stdout ) ) . toMatchObject ( {
218+ results : [ { pid : process . pid , state : "readable" } ] ,
219+ } ) ;
220+ expect ( process . env . CLANG_MODULE_CACHE_PATH ) . toBe ( cachePath ) ;
221+ } finally {
222+ vi . unstubAllEnvs ( ) ;
223+ if ( kind === "read-only directory" ) await chmod ( cacheParent , 0o700 ) ;
224+ await reader . close ( ) ;
225+ }
226+ } ,
227+ ) ;
228+
229+ it . skipIf ( process . platform === "win32" ) (
230+ "uses the compiler-managed module cache instead of rebuilding system modules per REA process" ,
231+ async ( ) => {
232+ const directory = await createTestTempDirectory (
233+ "rea-process-token-module-cache-test-" ,
234+ ) ;
235+ const fakeCompiler = await writeBlockingCompiler ( directory ) ;
236+ const reader = createDarwinProcessRunTokenReader ( {
237+ xcrun : fakeCompiler . executable ,
238+ } ) ;
239+ const preparation = reader . prepare ( ) ;
240+ try {
241+ const state = await waitForCompileState ( fakeCompiler . statePath ) ;
242+ expect ( state . arguments ) . not . toContain ( "-module-cache-path" ) ;
243+ await writeFile ( fakeCompiler . releasePath , "release" ) ;
244+ await preparation ;
245+ } finally {
246+ await Promise . all ( [ reader . close ( ) , Promise . allSettled ( [ preparation ] ) ] ) ;
247+ }
248+ } ,
249+ ) ;
250+
164251it . skipIf ( process . platform === "win32" ) (
165252 "rethrows compile cancellation, removes its temporary root, and permits retry" ,
166253 async ( ) => {
0 commit comments