@@ -327,20 +327,32 @@ export async function findNodeApiModulePaths(
327327 const result : string [ ] = [ ] ;
328328 const pendingResults : Promise < string [ ] > [ ] = [ ] ;
329329
330- for await ( const dirent of await fs . promises . opendir ( candidatePath ) ) {
330+ try {
331+ for await ( const dirent of await fs . promises . opendir ( candidatePath ) ) {
332+ if (
333+ dirent . isFile ( ) &&
334+ dirent . name === MAGIC_FILENAME &&
335+ hasPlatformExtension ( platform , candidatePath )
336+ ) {
337+ result . push ( candidatePath ) ;
338+ } else if ( dirent . isDirectory ( ) ) {
339+ // Traverse into the child directory
340+ // Pushing result into a list instead of awaiting immediately to parallelize the search
341+ pendingResults . push (
342+ findNodeApiModulePaths ( options , path . join ( suffix , dirent . name ) )
343+ ) ;
344+ }
345+ }
346+ } catch ( error ) {
331347 if (
332- dirent . isFile ( ) &&
333- dirent . name === MAGIC_FILENAME &&
334- hasPlatformExtension ( platform , candidatePath )
348+ error instanceof Error &&
349+ "code" in error &&
350+ ( error . code === "ENOENT" || error . code === "EACCES" )
335351 ) {
336- result . push ( candidatePath ) ;
337- } else if ( dirent . isDirectory ( ) ) {
338- // Traverse into the child directory
339- // Pushing result into a list instead of awaiting immediately to parallelize the search
340- pendingResults . push (
341- findNodeApiModulePaths ( options , path . join ( suffix , dirent . name ) )
342- ) ;
352+ // Gracefully handling issues with reading directories
353+ return [ ] ;
343354 }
355+ throw error ;
344356 }
345357 const childResults = await Promise . all ( pendingResults ) ;
346358 result . push ( ...childResults . flatMap ( ( filePath ) => filePath ) ) ;
0 commit comments