From 63a5f669db318302a6caaaf3799b0164d2ee7b60 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Sat, 19 Jul 2025 13:34:58 +0000 Subject: [PATCH] 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 --- src/core/tools/__tests__/readFileTool.spec.ts | 15 +++++++++++++++ src/core/tools/readFileTool.ts | 8 ++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/tools/__tests__/readFileTool.spec.ts b/src/core/tools/__tests__/readFileTool.spec.ts index 44be1d3b92..ce91ef3d2a 100644 --- a/src/core/tools/__tests__/readFileTool.spec.ts +++ b/src/core/tools/__tests__/readFileTool.spec.ts @@ -481,6 +481,21 @@ describe("read_file tool XML output structure", () => { `\n${testFilePath}\nFile is empty\n\n`, ) }) + + it("should treat files with only whitespace as empty", async () => { + // Setup - file has lines but only whitespace content + mockedCountFileLines.mockResolvedValue(3) // File has 3 lines + mockedExtractTextFromFile.mockResolvedValue(" \n\t\n ") // Only whitespace + mockProvider.getState.mockResolvedValue({ maxReadFileLine: -1 }) + + // Execute + const result = await executeReadFileTool({}, { totalLines: 3 }) + + // Verify - should show empty file notice even though totalLines > 0 + expect(result).toBe( + `\n${testFilePath}\nFile is empty\n\n`, + ) + }) }) describe("Error Handling Tests", () => { diff --git a/src/core/tools/readFileTool.ts b/src/core/tools/readFileTool.ts index 6de8dd5642..5c83d375ca 100644 --- a/src/core/tools/readFileTool.ts +++ b/src/core/tools/readFileTool.ts @@ -519,9 +519,13 @@ export async function readFileTool( // Handle normal file read const content = await extractTextFromFile(fullPath) const lineRangeAttr = ` lines="1-${totalLines}"` - let xmlInfo = totalLines > 0 ? `\n${content}\n` : `` - if (totalLines === 0) { + // Check if file is effectively empty (no content or only whitespace) + const isEffectivelyEmpty = totalLines === 0 || content.trim() === "" + + let xmlInfo = !isEffectivelyEmpty ? `\n${content}\n` : `` + + if (isEffectivelyEmpty) { xmlInfo += `File is empty\n` }