Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/metal-papayas-think.md
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
69 changes: 58 additions & 11 deletions src/core/prompts/sections/__tests__/custom-instructions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,30 +615,64 @@ describe("Rules directory reading", () => {
} as any)

// Simulate listing files including a symlink
readdirMock.mockResolvedValueOnce([
{
name: "regular.txt",
isFile: () => true,
isSymbolicLink: () => false,
parentPath: "/fake/path/.roo/rules",
},
{ name: "link.txt", isFile: () => false, isSymbolicLink: () => true, parentPath: "/fake/path/.roo/rules" },
] as any)
readdirMock
.mockResolvedValueOnce([
{
name: "regular.txt",
isFile: () => true,
isSymbolicLink: () => false,
parentPath: "/fake/path/.roo/rules",
},
{
name: "link.txt",
isFile: () => false,
isSymbolicLink: () => true,
parentPath: "/fake/path/.roo/rules",
},
{
name: "link_dir",
isFile: () => false,
isSymbolicLink: () => true,
parentPath: "/fake/path/.roo/rules",
},
{
name: "nested_link.txt",
isFile: () => false,
isSymbolicLink: () => true,
parentPath: "/fake/path/.roo/rules",
},
] as any)
.mockResolvedValueOnce([
{ name: "subdir_link.txt", isFile: () => true, parentPath: "/fake/path/.roo/rules/symlink-target-dir" },
] as any)

// Simulate readlink response
readlinkMock.mockResolvedValueOnce("../symlink-target.txt")
readlinkMock
.mockResolvedValueOnce("../symlink-target.txt")
.mockResolvedValueOnce("../symlink-target-dir")
.mockResolvedValueOnce("../nested-symlink")
.mockResolvedValueOnce("nested-symlink-target.txt")

// Reset and set up the stat mock with more granular control
statMock.mockReset()
statMock.mockImplementation((path: string) => {
// For directory check
if (path === "/fake/path/.roo/rules") {
if (path === "/fake/path/.roo/rules" || path.endsWith("dir")) {
return Promise.resolve({
isDirectory: jest.fn().mockReturnValue(true),
isFile: jest.fn().mockReturnValue(false),
} as any)
}

// For symlink check
if (path.endsWith("symlink")) {
return Promise.resolve({
isDirectory: jest.fn().mockReturnValue(false),
isFile: jest.fn().mockReturnValue(false),
isSymbolicLink: jest.fn().mockReturnValue(true),
} as any)
}

// For all files
return Promise.resolve({
isFile: jest.fn().mockReturnValue(true),
Expand All @@ -654,6 +688,12 @@ describe("Rules directory reading", () => {
if (filePath.toString() === "/fake/path/.roo/rules/../symlink-target.txt") {
return Promise.resolve("symlink target content")
}
if (filePath.toString() === "/fake/path/.roo/rules/symlink-target-dir/subdir_link.txt") {
return Promise.resolve("regular file content under symlink target dir")
}
if (filePath.toString() === "/fake/path/.roo/rules/../nested-symlink-target.txt") {
return Promise.resolve("nested symlink target content")
}
return Promise.reject({ code: "ENOENT" })
})

Expand All @@ -664,13 +704,20 @@ describe("Rules directory reading", () => {
expect(result).toContain("regular file content")
expect(result).toContain("# Rules from /fake/path/.roo/rules/../symlink-target.txt:")
expect(result).toContain("symlink target content")
expect(result).toContain("# Rules from /fake/path/.roo/rules/symlink-target-dir/subdir_link.txt:")
expect(result).toContain("regular file content under symlink target dir")
expect(result).toContain("# Rules from /fake/path/.roo/rules/../nested-symlink-target.txt:")
expect(result).toContain("nested symlink target content")

// Verify readlink was called with the symlink path
expect(readlinkMock).toHaveBeenCalledWith("/fake/path/.roo/rules/link.txt")
expect(readlinkMock).toHaveBeenCalledWith("/fake/path/.roo/rules/link_dir")

// Verify both files were read
expect(readFileMock).toHaveBeenCalledWith("/fake/path/.roo/rules/regular.txt", "utf-8")
expect(readFileMock).toHaveBeenCalledWith("/fake/path/.roo/rules/../symlink-target.txt", "utf-8")
expect(readFileMock).toHaveBeenCalledWith("/fake/path/.roo/rules/symlink-target-dir/subdir_link.txt", "utf-8")
expect(readFileMock).toHaveBeenCalledWith("/fake/path/.roo/rules/../nested-symlink-target.txt", "utf-8")
})
beforeEach(() => {
jest.clearAllMocks()
Expand Down
89 changes: 69 additions & 20 deletions src/core/prompts/sections/custom-instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Contributor

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 parentPath property on Dirent objects. parentPath isn’t part of the standard Dirent interface, which could lead to inconsistencies outside of your tests. Consider passing the directory path explicitly instead.

Suggested change
const fullPath = path.resolve(entry.parentPath || dirPath, entry.name)
const fullPath = path.resolve(dirPath, entry.name)

Copy link
Contributor Author

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)

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fs.stat to check if the target is a symlink may be ineffective since fs.stat follows symlinks. To accurately detect a nested symlink, consider using fs.lstat instead.

Suggested change
const stats = await fs.stat(resolvedTarget)
const stats = await fs.lstat(resolvedTarget)

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
*/
Expand All @@ -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 {
Expand Down
Loading