-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix bug not to respect symbolic linked rules, if target is a directory or another symbolic link #2513
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
Fix bug not to respect symbolic linked rules, if target is a directory or another symbolic link #2513
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "roo-cline": patch | ||
| --- | ||
|
|
||
| Fix bug not to respect symbolic linked rules, if target is a directory or another symbolic link |
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ import fs from "fs/promises" | |||||
| import path from "path" | ||||||
|
|
||||||
| import { LANGUAGES, isLanguage } from "../../../shared/language" | ||||||
| import { Dirent } from "fs" | ||||||
|
|
||||||
| /** | ||||||
| * Safely read a file and return its trimmed content | ||||||
|
|
@@ -31,6 +32,68 @@ async function directoryExists(dirPath: string): Promise<boolean> { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| const MAX_DEPTH = 5 | ||||||
|
|
||||||
| /** | ||||||
| * Recursively resolve directory entries and collect file paths | ||||||
| */ | ||||||
| async function resolveDirectoryEntry( | ||||||
| entry: Dirent, | ||||||
| dirPath: string, | ||||||
| filePaths: string[], | ||||||
| depth: number, | ||||||
| ): Promise<void> { | ||||||
| // Avoid cyclic symlinks | ||||||
| if (depth > MAX_DEPTH) { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const fullPath = path.resolve(entry.parentPath || dirPath, entry.name) | ||||||
| if (entry.isFile()) { | ||||||
| // Regular file | ||||||
| filePaths.push(fullPath) | ||||||
| } else if (entry.isSymbolicLink()) { | ||||||
| // Await the resolution of the symbolic link | ||||||
| await resolveSymLink(fullPath, filePaths, depth + 1) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Recursively resolve a symbolic link and collect file paths | ||||||
| */ | ||||||
| async function resolveSymLink(fullPath: string, filePaths: string[], depth: number): Promise<void> { | ||||||
| // Avoid cyclic symlinks | ||||||
| if (depth > MAX_DEPTH) { | ||||||
| return | ||||||
| } | ||||||
| try { | ||||||
| // Get the symlink target | ||||||
| const linkTarget = await fs.readlink(fullPath) | ||||||
| // Resolve the target path (relative to the symlink location) | ||||||
| const resolvedTarget = path.resolve(path.dirname(fullPath), linkTarget) | ||||||
|
|
||||||
| // Check if the target is a file | ||||||
| const stats = await fs.stat(resolvedTarget) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| if (stats.isFile()) { | ||||||
| filePaths.push(resolvedTarget) | ||||||
| } else if (stats.isDirectory()) { | ||||||
| const anotherEntries = await fs.readdir(resolvedTarget, { withFileTypes: true, recursive: true }) | ||||||
| // Collect promises for recursive calls within the directory | ||||||
| const directoryPromises: Promise<void>[] = [] | ||||||
| for (const anotherEntry of anotherEntries) { | ||||||
| directoryPromises.push(resolveDirectoryEntry(anotherEntry, resolvedTarget, filePaths, depth + 1)) | ||||||
| } | ||||||
| // Wait for all entries in the resolved directory to be processed | ||||||
| await Promise.all(directoryPromises) | ||||||
| } else if (stats.isSymbolicLink()) { | ||||||
| // Handle nested symlinks by awaiting the recursive call | ||||||
| await resolveSymLink(resolvedTarget, filePaths, depth + 1) | ||||||
| } | ||||||
| } catch (err) { | ||||||
| // Skip invalid symlinks | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Read all text files from a directory in alphabetical order | ||||||
| */ | ||||||
|
|
@@ -40,30 +103,16 @@ async function readTextFilesFromDirectory(dirPath: string): Promise<Array<{ file | |||||
|
|
||||||
| // Process all entries - regular files and symlinks that might point to files | ||||||
| const filePaths: string[] = [] | ||||||
| // Collect promises for the initial resolution calls | ||||||
| const initialPromises: Promise<void>[] = [] | ||||||
|
|
||||||
| for (const entry of entries) { | ||||||
| const fullPath = path.resolve(entry.parentPath || dirPath, entry.name) | ||||||
| if (entry.isFile()) { | ||||||
| // Regular file | ||||||
| filePaths.push(fullPath) | ||||||
| } else if (entry.isSymbolicLink()) { | ||||||
| try { | ||||||
| // Get the symlink target | ||||||
| const linkTarget = await fs.readlink(fullPath) | ||||||
| // Resolve the target path (relative to the symlink location) | ||||||
| const resolvedTarget = path.resolve(path.dirname(fullPath), linkTarget) | ||||||
|
|
||||||
| // Check if the target is a file | ||||||
| const stats = await fs.stat(resolvedTarget) | ||||||
| if (stats.isFile()) { | ||||||
| filePaths.push(resolvedTarget) | ||||||
| } | ||||||
| } catch (err) { | ||||||
| // Skip invalid symlinks | ||||||
| } | ||||||
| } | ||||||
| initialPromises.push(resolveDirectoryEntry(entry, dirPath, filePaths, 0)) | ||||||
| } | ||||||
|
|
||||||
| // Wait for all asynchronous operations (including recursive ones) to complete | ||||||
| await Promise.all(initialPromises) | ||||||
|
|
||||||
| const fileContents = await Promise.all( | ||||||
| filePaths.map(async (file) => { | ||||||
| try { | ||||||
|
|
||||||
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 code relies on a non-standard
parentPathproperty on Dirent objects.parentPathisn’t part of the standard Dirent interface, which could lead to inconsistencies outside of your tests. Consider passing the directory path explicitly instead.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.
parentPath should be used. ref: #2405 (comment)