fix: separate countFileLines and isBinaryFile calls for accurate error handling

- Split Promise.all() into sequential calls to isolate error sources
- countFileLines is called first and should not fail for valid files
- Only isBinaryFile errors are caught and handled by treating file as binary
- This ensures RangeError from isBinaryFile doesn't incorrectly attribute to countFileLines
- Fixes issue where binary file detection errors were masking actual file reading problems
This commit is contained in:
Daniel Riccio 2025-07-31 11:32:07 -05:00
parent 6796e1a285
commit 7718bef258
No known key found for this signature in database
GPG key ID: FFD5FD825F8E8209

View file

@ -436,14 +436,15 @@ export async function readFileTool(
let totalLines: number
let isBinary: boolean
// First, count the file lines (this should not fail for valid files)
totalLines = await countFileLines(fullPath)
// Then check if it's binary, with error handling specific to isBinaryFile
try {
const results = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)])
totalLines = results[0]
isBinary = results[1]
isBinary = await 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
}