@@ -304,24 +304,32 @@ function runOxfmtCheck(files, fix) {
304304 * @param {string[] } files - Array of file paths
305305 * @param {boolean } fix - Whether to pass --fix
306306 * @param {boolean } clearCache - Whether to pass --clear-cache
307- * @returns {{ scriptArgs: string[], tempFile: string | null } }
307+ * @returns {{ scriptArgs: string[], cleanup: (() => void) | null } }
308308 */
309309function buildScriptArgs ( files , fix , clearCache ) {
310310 const scriptArgs = [ ]
311- let tempFile = null
311+ let cleanup = null
312312
313313 const totalLength = files . reduce ( ( sum , f ) => sum + f . length + 1 , 0 )
314314 if ( totalLength > MAX_ARG_LENGTH ) {
315- const RADIX = 36
316- const SUFFIX_LENGTH = 8
317- tempFile = path . join (
318- os . tmpdir ( ) ,
319- `lint-files-${ Date . now ( ) } -${ Math . random ( )
320- . toString ( RADIX )
321- . slice ( 2 , 2 + SUFFIX_LENGTH ) } .txt`,
322- )
323- fs . writeFileSync ( tempFile , files . join ( "\n" ) , "utf8" )
324- scriptArgs . push ( `--files-from=${ tempFile } ` )
315+ // Use mkdtempSync (private mode-0700 dir with cryptographic suffix) so the
316+ // inner file cannot be predicted/preempted by another local process.
317+ const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "lint-files-" ) )
318+ const tempFile = path . join ( tempDir , "files.txt" )
319+ cleanup = ( ) => {
320+ try {
321+ fs . rmSync ( tempDir , { recursive : true , force : true } )
322+ } catch {
323+ /* Best-effort cleanup */
324+ }
325+ }
326+ try {
327+ fs . writeFileSync ( tempFile , files . join ( "\n" ) , "utf8" )
328+ scriptArgs . push ( `--files-from=${ tempFile } ` )
329+ } catch ( error ) {
330+ cleanup ( )
331+ throw error
332+ }
325333 } else {
326334 scriptArgs . push ( ...files )
327335 }
@@ -333,7 +341,7 @@ function buildScriptArgs(files, fix, clearCache) {
333341 scriptArgs . push ( "--clear-cache" )
334342 }
335343
336- return { scriptArgs, tempFile }
344+ return { scriptArgs, cleanup }
337345}
338346
339347/**
@@ -352,21 +360,15 @@ function runLinter(script, files, description, fix, clearCache) {
352360 console . log ( `\n🔍 ${ description } (${ files . length } files)` )
353361
354362 const scriptPath = path . join ( __dirname , script )
355- const { scriptArgs, tempFile } = buildScriptArgs ( files , fix , clearCache )
363+ const { scriptArgs, cleanup } = buildScriptArgs ( files , fix , clearCache )
356364
357365 const result = spawnSync ( "node" , [ scriptPath , ...scriptArgs ] , {
358366 stdio : "inherit" ,
359367 cwd : projectRoot ,
360368 env,
361369 } )
362370
363- if ( tempFile ) {
364- try {
365- fs . unlinkSync ( tempFile )
366- } catch {
367- /* Best-effort cleanup */
368- }
369- }
371+ cleanup ?. ( )
370372
371373 if ( result . error ) {
372374 console . error ( `❌ Failed to run ${ script } :` , result . error . message )
@@ -392,7 +394,7 @@ function runLinterAsync(script, files, description, fix, clearCache) {
392394 console . log ( `\n🔍 ${ description } (${ files . length } files)` )
393395
394396 const scriptPath = path . join ( __dirname , script )
395- const { scriptArgs, tempFile } = buildScriptArgs ( files , fix , clearCache )
397+ const { scriptArgs, cleanup } = buildScriptArgs ( files , fix , clearCache )
396398
397399 return new Promise ( ( resolve ) => {
398400 const child = spawn ( "node" , [ scriptPath , ...scriptArgs ] , {
@@ -402,13 +404,7 @@ function runLinterAsync(script, files, description, fix, clearCache) {
402404 } )
403405
404406 child . on ( "close" , ( code , signal ) => {
405- if ( tempFile ) {
406- try {
407- fs . unlinkSync ( tempFile )
408- } catch {
409- /* Best-effort cleanup */
410- }
411- }
407+ cleanup ?. ( )
412408 if ( signal ) {
413409 console . error ( `❌ ${ script } terminated by signal ${ signal } ` )
414410 resolve ( 1 )
@@ -417,13 +413,7 @@ function runLinterAsync(script, files, description, fix, clearCache) {
417413 resolve ( code ?? 1 )
418414 } )
419415 child . on ( "error" , ( err ) => {
420- if ( tempFile ) {
421- try {
422- fs . unlinkSync ( tempFile )
423- } catch {
424- /* Best-effort cleanup */
425- }
426- }
416+ cleanup ?. ( )
427417 console . error ( `❌ Failed to run ${ script } :` , err . message )
428418 resolve ( 1 )
429419 } )
0 commit comments