Skip to content

Commit 2532dad

Browse files
committed
feat: improve error message when read_file is used on directory
- Add early directory detection in ReadFileTool before attempting file read - Provide clear, actionable error message suggesting use of list_files tool - Add test case for directory reading error scenario - Fix fsPromises.stat mocking in all test suites to support directory checks Fixes ROO-319
1 parent add06a2 commit 2532dad

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/core/tools/ReadFileTool.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "path"
2+
import * as fs from "fs/promises"
23
import { isBinaryFile } from "isbinaryfile"
34
import type { FileEntry, LineRange } from "@roo-code/types"
45
import { isNativeProtocol, ANTHROPIC_DEFAULT_MAX_TOKENS } from "@roo-code/types"
@@ -350,6 +351,20 @@ export class ReadFileTool extends BaseTool<"read_file"> {
350351
const fullPath = path.resolve(task.cwd, relPath)
351352

352353
try {
354+
// Check if the path is a directory before attempting to read it
355+
const stats = await fs.stat(fullPath)
356+
if (stats.isDirectory()) {
357+
const errorMsg = `Cannot read '${relPath}' because it is a directory. To view the contents of a directory, use the list_files tool instead.`
358+
updateFileResult(relPath, {
359+
status: "error",
360+
error: errorMsg,
361+
xmlContent: `<file><path>${relPath}</path><error>Error reading file: ${errorMsg}</error></file>`,
362+
nativeContent: `File: ${relPath}\nError: Error reading file: ${errorMsg}`,
363+
})
364+
await task.say("error", `Error reading file ${relPath}: ${errorMsg}`)
365+
continue
366+
}
367+
353368
const [totalLines, isBinary] = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)])
354369

355370
if (isBinary) {

src/core/tools/__tests__/readFileTool.spec.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,13 @@ describe("read_file tool with maxReadFileLine setting", () => {
305305
mockedPathResolve.mockReturnValue(absoluteFilePath)
306306
mockedIsBinaryFile.mockResolvedValue(false)
307307

308+
// Mock fsPromises.stat to return a file (not directory) by default
309+
fsPromises.stat.mockResolvedValue({
310+
isDirectory: () => false,
311+
isFile: () => true,
312+
isSymbolicLink: () => false,
313+
} as any)
314+
308315
mockInputContent = fileContent
309316

310317
// Setup the extractTextFromFile mock implementation with the current mockInputContent
@@ -612,7 +619,12 @@ describe("read_file tool output structure", () => {
612619

613620
// CRITICAL: Reset fsPromises mocks to prevent cross-test contamination
614621
fsPromises.stat.mockClear()
615-
fsPromises.stat.mockResolvedValue({ size: 1024 })
622+
fsPromises.stat.mockResolvedValue({
623+
size: 1024,
624+
isDirectory: () => false,
625+
isFile: () => true,
626+
isSymbolicLink: () => false,
627+
} as any)
616628
fsPromises.readFile.mockClear()
617629

618630
// Use shared mock setup function
@@ -1429,6 +1441,37 @@ describe("read_file tool output structure", () => {
14291441
`File: ${testFilePath}\nError: Access to ${testFilePath} is blocked by the .rooignore file settings. You must try to continue in the task without using this file, or ask the user to update the .rooignore file.`,
14301442
)
14311443
})
1444+
1445+
it("should provide helpful error when trying to read a directory", async () => {
1446+
// Setup - mock fsPromises.stat to indicate the path is a directory
1447+
const dirPath = "test/my-directory"
1448+
const absoluteDirPath = "/test/my-directory"
1449+
1450+
mockedPathResolve.mockReturnValue(absoluteDirPath)
1451+
1452+
// Mock fs/promises stat to return directory
1453+
fsPromises.stat.mockResolvedValue({
1454+
isDirectory: () => true,
1455+
isFile: () => false,
1456+
isSymbolicLink: () => false,
1457+
} as any)
1458+
1459+
// Mock isBinaryFile won't be called since we check directory first
1460+
mockedIsBinaryFile.mockResolvedValue(false)
1461+
1462+
// Execute
1463+
const result = await executeReadFileTool({ args: `<file><path>${dirPath}</path></file>` })
1464+
1465+
// Verify - native format for error
1466+
expect(result).toContain(`File: ${dirPath}`)
1467+
expect(result).toContain(`Error: Error reading file: Cannot read '${dirPath}' because it is a directory`)
1468+
expect(result).toContain("use the list_files tool instead")
1469+
1470+
// Verify that task.say was called with the error
1471+
expect(mockCline.say).toHaveBeenCalledWith("error", expect.stringContaining("Cannot read"))
1472+
expect(mockCline.say).toHaveBeenCalledWith("error", expect.stringContaining("is a directory"))
1473+
expect(mockCline.say).toHaveBeenCalledWith("error", expect.stringContaining("list_files tool"))
1474+
})
14321475
})
14331476
})
14341477

@@ -1460,7 +1503,12 @@ describe("read_file tool with image support", () => {
14601503

14611504
// CRITICAL: Reset fsPromises.stat to prevent cross-test contamination
14621505
fsPromises.stat.mockClear()
1463-
fsPromises.stat.mockResolvedValue({ size: 1024 })
1506+
fsPromises.stat.mockResolvedValue({
1507+
size: 1024,
1508+
isDirectory: () => false,
1509+
isFile: () => true,
1510+
isSymbolicLink: () => false,
1511+
} as any)
14641512

14651513
// Use shared mock setup function with local variables
14661514
const mocks = createMockCline()
@@ -1801,6 +1849,13 @@ describe("read_file tool concurrent file reads limit", () => {
18011849
mockedIsBinaryFile.mockResolvedValue(false)
18021850
mockedCountFileLines.mockResolvedValue(10)
18031851

1852+
// Mock fsPromises.stat to return a file (not directory) by default
1853+
fsPromises.stat.mockResolvedValue({
1854+
isDirectory: () => false,
1855+
isFile: () => true,
1856+
isSymbolicLink: () => false,
1857+
} as any)
1858+
18041859
toolResult = undefined
18051860
})
18061861

0 commit comments

Comments
 (0)