Skip to content

Commit 58105ac

Browse files
committed
Tread a failing opendir as an empty dir
1 parent a7b398a commit 58105ac

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

packages/host/src/node/path-utils.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,23 @@ describe("findNodeApiModulePaths", () => {
367367
path.join(tempDir, "node_modules/root.apple.node"),
368368
]);
369369
});
370+
371+
it("returns empty when directory cannot be read due to permissions", async (context) => {
372+
const tempDir = setupTempDirectory(context, {
373+
"addon.apple.node/react-native-node-api-module": "",
374+
});
375+
376+
removeReadPermissions(tempDir);
377+
try {
378+
const result = findNodeApiModulePaths({
379+
fromPath: tempDir,
380+
platform: "apple",
381+
});
382+
assert.deepEqual(await result, []);
383+
} finally {
384+
restoreReadPermissions(tempDir);
385+
}
386+
});
370387
});
371388

372389
describe("determineModuleContext", () => {

packages/host/src/node/path-utils.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,24 @@ 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)) {
331-
if (
332-
dirent.isFile() &&
333-
dirent.name === MAGIC_FILENAME &&
334-
hasPlatformExtension(platform, candidatePath)
335-
) {
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-
);
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+
}
343345
}
346+
} catch {
347+
// Intentionally left empty: if the directory cannot be read, we skip it
344348
}
345349
const childResults = await Promise.all(pendingResults);
346350
result.push(...childResults.flatMap((filePath) => filePath));

0 commit comments

Comments
 (0)