fix(scripts): stop the snippet sync walking node_modules and cloned f… (#2664)

This commit is contained in:
Konstantin Konstantinov 2026-08-14 17:03:06 +03:00 • committed by GitHub
commit 27a94b5d1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -504,31 +504,54 @@ function findMarkdownFiles(dir: string): string[] {
return files;
}
/**
* Directory names that are never descended into when looking for package sources.
*
* `node_modules` is the critical one: pnpm fills it with symlinks pointing back
* into the store, whose packages link onward in turn. Entering it means walking
* that graph along every distinct link path, re-visiting the same directories
* over and over.
*
* `batch-test` holds the codemod's cloned fixture repositories — whole external
* monorepos that `pnpm-workspace.yaml` already excludes from the workspace, and
* whose sources this script must never rewrite.
*/
const SKIPPED_DIRS = new Set(['node_modules', 'dist', 'batch-test']);
/**
* Find all package src directories under the packages directory.
*
* Descends explicitly rather than using `readdirSync`'s `recursive` option. That
* option follows symlinks and collects every entry it visits into a single array
* before returning, so filtering unwanted directories out of the result is too
* late to keep the walk bounded — the traversal has already happened.
*
* @param packagesDir The packages directory
* @returns Array of absolute paths to src directories
*/
function findPackageSrcDirs(packagesDir: string): string[] {
const srcDirs: string[] = [];
const entries = readdirSync(packagesDir, {
withFileTypes: true,
recursive: true,
});
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (entry.name !== 'src') continue;
const descend = (dir: string): void => {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
// isDirectory() is false for a symlink, so pnpm's links are never entered.
if (!entry.isDirectory()) continue;
if (SKIPPED_DIRS.has(entry.name)) continue;
const fullPath = join(entry.parentPath, entry.name);
const fullPath = join(dir, entry.name);
// Only include src dirs that are direct children of a package
// (e.g., packages/core-internal/src, packages/middleware/express/src)
// Skip nested src dirs like node_modules/*/src
if (fullPath.includes('node_modules')) continue;
// A package owns a single src dir; everything below it is that package's
// own tree, which findSourceFiles walks.
if (entry.name === 'src') {
srcDirs.push(fullPath);
continue;
}
srcDirs.push(fullPath);
}
descend(fullPath);
}
};
descend(packagesDir);
return srcDirs;
}