diff --git a/src/core/tools/readFileTool.ts b/src/core/tools/readFileTool.ts index 5d878c5f81..04b91a2189 100644 --- a/src/core/tools/readFileTool.ts +++ b/src/core/tools/readFileTool.ts @@ -437,7 +437,9 @@ export async function readFileTool( let isBinary: boolean try { - ;[totalLines, isBinary] = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)]) + const results = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)]) + totalLines = results[0] + isBinary = results[1] } catch (error) { // If isBinaryFile throws an error (e.g., RangeError), treat the file as binary console.warn(`Error checking if file is binary for ${relPath}:`, error) diff --git a/src/integrations/misc/__tests__/extract-text-large-files.spec.ts b/src/integrations/misc/__tests__/extract-text-large-files.spec.ts index fc2f7f54b6..e164c070ab 100644 --- a/src/integrations/misc/__tests__/extract-text-large-files.spec.ts +++ b/src/integrations/misc/__tests__/extract-text-large-files.spec.ts @@ -25,6 +25,8 @@ describe("extractTextFromFile - Large File Handling", () => { // Set default mock behavior mockedFs.access.mockResolvedValue(undefined) mockedIsBinaryFile.mockResolvedValue(false) + // Mock console.warn + vi.spyOn(console, "warn").mockImplementation(() => {}) }) it("should truncate files that exceed maxReadFileLine limit", async () => { @@ -218,4 +220,20 @@ describe("extractTextFromFile - Large File Handling", () => { "File not found: /test/nonexistent.ts", ) }) + + it("should handle RangeError from isBinaryFile and treat file as binary", async () => { + // Setup - mock isBinaryFile to throw RangeError + mockedIsBinaryFile.mockRejectedValue(new RangeError("Invalid array length")) + + // Execute and expect it to throw since file is treated as binary + await expect(extractTextFromFile("/test/problematic-file.bin", 100)).rejects.toThrow( + "Cannot read text for file type: .bin", + ) + + // Verify that the warning was logged + expect(console.warn).toHaveBeenCalledWith( + "Error checking if file is binary for /test/problematic-file.bin:", + expect.any(RangeError), + ) + }) })