Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,9 @@ describe('findFilesWithExtension', () => {
return [];
},
existsSync: () => true,
statSync: () => ({
lstatSync: () => ({
isDirectory: () => false,
isSymbolicLink: () => false,
}),
readFileSync: () => packageJson,
}));
Expand Down Expand Up @@ -586,11 +587,12 @@ describe('findFilesWithExtension', () => {
return [];
},
existsSync: () => true,
statSync: filePath => ({
lstatSync: filePath => ({
isDirectory: () =>
filePath === pnpmFolder ||
filePath === packageFolder ||
filePath === path.join(targetFolder, '.hidden'),
isSymbolicLink: () => false,
}),
readFileSync: () => packageJson,
}));
Expand Down Expand Up @@ -619,8 +621,9 @@ describe('findFilesWithExtension', () => {
return [];
},
existsSync: () => true,
statSync: filePath => ({
lstatSync: filePath => ({
isDirectory: () => filePath === path.join(targetFolder, 'Components'),
isSymbolicLink: () => false,
}),
readFileSync: () => packageJson,
}));
Expand All @@ -635,6 +638,67 @@ describe('findFilesWithExtension', () => {
path.join(targetFolder, 'Components', 'MyComponent.mm'),
]);
});

it('skips nested node_modules folders', () => {
const targetFolder = '/project/my-library';
const nodeModules = path.join(targetFolder, 'node_modules');

jest.mock('node:fs', () => ({
readdirSync: dirPath => {
if (dirPath === targetFolder) {
return ['node_modules', 'Component.mm'];
}
if (dirPath === nodeModules) {
return ['Dependency.mm'];
}
return [];
},
existsSync: () => true,
lstatSync: filePath => ({
isDirectory: () => filePath === nodeModules,
isSymbolicLink: () => false,
}),
readFileSync: () => packageJson,
}));

const {
findFilesWithExtension: findFiles,
} = require('../generate-artifacts-executor/generateRCTThirdPartyComponents');

const result = findFiles(targetFolder, '.mm');
expect(result).toEqual([path.join(targetFolder, 'Component.mm')]);
});

it('does not follow symlinked folders', () => {
const targetFolder = '/project/my-library';
const symlinkedFolder = path.join(targetFolder, 'linked');

jest.mock('node:fs', () => ({
readdirSync: dirPath => {
if (dirPath === targetFolder) {
return ['linked', 'Component.mm'];
}
// A symlink pointing back at its parent: following it never terminates.
if (dirPath === symlinkedFolder) {
return ['linked', 'Component.mm'];
}
return [];
},
existsSync: () => true,
lstatSync: filePath => ({
isDirectory: () => filePath.endsWith('linked'),
isSymbolicLink: () => filePath.endsWith('linked'),
}),
readFileSync: () => packageJson,
}));

const {
findFilesWithExtension: findFiles,
} = require('../generate-artifacts-executor/generateRCTThirdPartyComponents');

const result = findFiles(targetFolder, '.mm');
expect(result).toEqual([path.join(targetFolder, 'Component.mm')]);
});
});

describe('generateSchemaInfos', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,24 @@ function findFilesWithExtension(
return null;
}

if (
fs.existsSync(absolutePath) &&
fs.statSync(absolutePath).isDirectory()
) {
// A library's own sources never live in its dependencies, and crawling them
// is what makes this walk explode on large projects.
if (file === 'node_modules') {
return null;
}

if (!fs.existsSync(absolutePath)) {
return null;
}

// `lstatSync` does not resolve symlinks: following them can loop forever,
// e.g. workspace packages that link into each other under pnpm.
const stats = fs.lstatSync(absolutePath);
if (stats.isSymbolicLink()) {
return null;
}

if (stats.isDirectory()) {
files.push(...findFilesWithExtension(absolutePath, extension));
} else if (file.endsWith(extension)) {
files.push(absolutePath);
Expand Down