From 17b08730dc0e294de09d742bf0c2065f9c831102 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Sat, 26 Jul 2025 10:17:13 +0000 Subject: [PATCH] fix: handle RangeError in isbinaryfile library - Add try-catch blocks around all isBinaryFile calls - Treat files as binary when isbinaryfile throws RangeError - Prevents extension from freezing when processing certain files - Fixes #6242 --- src/core/mentions/index.ts | 9 ++++++++- src/core/tools/readFileTool.ts | 12 +++++++++++- src/integrations/misc/extract-text.ts | 9 ++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/core/mentions/index.ts b/src/core/mentions/index.ts index d0d305d096..4f9607c4a7 100644 --- a/src/core/mentions/index.ts +++ b/src/core/mentions/index.ts @@ -268,7 +268,14 @@ async function getFileOrFolderContent( fileContentPromises.push( (async () => { try { - const isBinary = await isBinaryFile(absoluteFilePath).catch(() => false) + let isBinary = false + try { + isBinary = await isBinaryFile(absoluteFilePath) + } catch (error) { + // If isBinaryFile throws an error (e.g., RangeError), treat as binary + console.warn(`Error checking if file is binary for ${absoluteFilePath}:`, error) + isBinary = true + } if (isBinary) { return undefined } diff --git a/src/core/tools/readFileTool.ts b/src/core/tools/readFileTool.ts index 6de8dd5642..5d878c5f81 100644 --- a/src/core/tools/readFileTool.ts +++ b/src/core/tools/readFileTool.ts @@ -433,7 +433,17 @@ export async function readFileTool( // Process approved files try { - const [totalLines, isBinary] = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)]) + let totalLines: number + let isBinary: boolean + + try { + ;[totalLines, isBinary] = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)]) + } 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) + totalLines = await countFileLines(fullPath) + isBinary = true + } // Handle binary files (but allow specific file types that extractTextFromFile can handle) if (isBinary) { diff --git a/src/integrations/misc/extract-text.ts b/src/integrations/misc/extract-text.ts index 8231c609be..a2a9c722ee 100644 --- a/src/integrations/misc/extract-text.ts +++ b/src/integrations/misc/extract-text.ts @@ -86,7 +86,14 @@ export async function extractTextFromFile(filePath: string, maxReadFileLine?: nu } // Handle other files - const isBinary = await isBinaryFile(filePath).catch(() => false) + let isBinary = false + try { + isBinary = await isBinaryFile(filePath) + } catch (error) { + // If isBinaryFile throws an error (e.g., RangeError), treat as binary + console.warn(`Error checking if file is binary for ${filePath}:`, error) + isBinary = true + } if (!isBinary) { // Check if we need to apply line limit