-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(scripts): stop the snippet sync walking node_modules and cloned f… #2664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 The new doc comment on findPackageSrcDirs explains that recursive readdirSync follows symlinks and that filtering results post-traversal is too late — yet the same pattern survives at two sibling sites in this file: findSourceFiles (line 450) and findMarkdownFiles (line 485). Since findSourceFiles runs on every src dir the new walker returns, a stray symlink inside a package src tree (or under docs/) would re-create the unbounded walk this PR fixes; consider extracting the explicit descend/SKIPPED_DIRS walk into a shared helper used by all three walkers.
Extended reasoning...
What the finding is. This PR rewrites
findPackageSrcDirsto descend explicitly and skipnode_modules/dist/batch-testbefore entering them, and its new doc comment documents why:readdirSync'srecursiveoption "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." But that exact pattern survives at two sibling sites in the same file:findSourceFiles(scripts/sync-snippets.ts:450) still callsreaddirSync(dir, { withFileTypes: true, recursive: true })and filters/generatedpaths out of the collected results afterward, andfindMarkdownFiles(scripts/sync-snippets.ts:485) does the same recursive readdir overdocs/with a post-hocstartsWith(generatedApiDir)filter.\n\nThe code path.main()callsfindPackageSrcDirs(PACKAGES_DIR)and then invokesfindSourceFiles(srcDir)on every src dir the new walker returns, plusfindMarkdownFiles(DOCS_DIR)once. So while the outerpackages/scan is now bounded bySKIPPED_DIRSand the symlink-safeentry.isDirectory()check, every inner walk immediately re-enters the unbounded recursive-readdir mode.\n\nWhy the new guard doesn't cover it.SKIPPED_DIRSand the explicitdescendclosure are local tofindPackageSrcDirs— nothing prevents the inner walkers from following a symlink. Verifiers empirically confirmed that Node's recursivereaddirSyncdoes follow directory symlinks (a self-referencing link is re-entered until the symloop limit), so one stray symlink inside a package'ssrc/tree or underdocs/would re-create the very unbounded symlink-graph walk (and heap exhaustion in the pre-push hook) this PR exists to fix.\n\nStep-by-step example. (1) A developer (or a tool) drops a symlink atpackages/client/src/some-linkpointing at a large tree — or even back up the tree. (2)findPackageSrcDirscorrectly returnspackages/client/src(the link itself is irrelevant to the outer walk). (3)main()callsfindSourceFiles('packages/client/src'), which runsreaddirSync(dir, { recursive: true })— this traversal follows the symlink and collects everything reachable through it into one array before any filter runs. (4) The/generatedfilter executes only after the full traversal, exactly the "too late" failure mode the PR's own comment describes.\n\nImpact and why it's a nit. Todaysrc/trees anddocs/contain no symlinks and nonode_modules, so nothing breaks on merge — this is a consistency/robustness cleanup, not a live bug. The concrete cost is that the fix lands at one of three call sites of the same pattern in the same file, and future readers get contradictory guidance: a comment declaring the option unsafe sitting next to two live uses of it. This matches the repository's Completeness review convention (partial migrations leaving sibling code paths with the pattern the PR replaces).\n\nHow to fix. Extract the explicit-descend walk into a shared helper — e.g. awalk(dir, { skip: SKIPPED_DIRS, onFile })that useswithFileTypes: trueper level and never enters symlinks — and have all three walkers (findPackageSrcDirs,findSourceFiles,findMarkdownFiles) use it, moving the.examples.ts/.test.ts/generatedanddocs/apiexclusions into the descend/file callbacks so they run before traversal rather than after.