Skip to content

Commit 63a5f66

Browse files
committed
fix: treat files with only whitespace as empty in read_file tool
- Added trim() check to detect files containing only whitespace characters - Files with only spaces, tabs, or newlines now show "File is empty" notice - Original file content is preserved without modification - Added test case to verify whitespace-only files are treated as empty Fixes #5789
1 parent e28fad1 commit 63a5f66

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,21 @@ describe("read_file tool XML output structure", () => {
481481
`<files>\n<file><path>${testFilePath}</path>\n<content/><notice>File is empty</notice>\n</file>\n</files>`,
482482
)
483483
})
484+
485+
it("should treat files with only whitespace as empty", async () => {
486+
// Setup - file has lines but only whitespace content
487+
mockedCountFileLines.mockResolvedValue(3) // File has 3 lines
488+
mockedExtractTextFromFile.mockResolvedValue(" \n\t\n ") // Only whitespace
489+
mockProvider.getState.mockResolvedValue({ maxReadFileLine: -1 })
490+
491+
// Execute
492+
const result = await executeReadFileTool({}, { totalLines: 3 })
493+
494+
// Verify - should show empty file notice even though totalLines > 0
495+
expect(result).toBe(
496+
`<files>\n<file><path>${testFilePath}</path>\n<content/><notice>File is empty</notice>\n</file>\n</files>`,
497+
)
498+
})
484499
})
485500

486501
describe("Error Handling Tests", () => {

src/core/tools/readFileTool.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,13 @@ export async function readFileTool(
519519
// Handle normal file read
520520
const content = await extractTextFromFile(fullPath)
521521
const lineRangeAttr = ` lines="1-${totalLines}"`
522-
let xmlInfo = totalLines > 0 ? `<content${lineRangeAttr}>\n${content}</content>\n` : `<content/>`
523522

524-
if (totalLines === 0) {
523+
// Check if file is effectively empty (no content or only whitespace)
524+
const isEffectivelyEmpty = totalLines === 0 || content.trim() === ""
525+
526+
let xmlInfo = !isEffectivelyEmpty ? `<content${lineRangeAttr}>\n${content}</content>\n` : `<content/>`
527+
528+
if (isEffectivelyEmpty) {
525529
xmlInfo += `<notice>File is empty</notice>\n`
526530
}
527531

0 commit comments

Comments
 (0)