Skip to content

Commit fdc3e6a

Browse files
committed
fix: prevent crashes when parsing Rust/Swift files by adding robust error handling
- Add timeout protection for WASM loading to prevent hanging - Return null instead of throwing when WASM fails to load - Add try-catch blocks around parser operations - Implement memory management to clean up old parsers - Add timeout for parsing operations to handle complex files This fixes issue #6532 where Roo Code crashes repeatedly, especially with Rust and Swift code on macOS.
1 parent 5c05762 commit fdc3e6a

File tree

2 files changed

+282
-132
lines changed

2 files changed

+282
-132
lines changed

src/services/tree-sitter/index.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,41 @@ async function parseFile(
395395

396396
try {
397397
// Parse the file content into an Abstract Syntax Tree (AST)
398-
const tree = parser.parse(fileContent)
398+
// Add timeout to prevent hanging on large or complex files
399+
const parseTimeout = new Promise<any>((resolve) => {
400+
setTimeout(() => {
401+
console.warn(`Parsing timeout for ${filePath} - file may be too large or complex`)
402+
resolve(null)
403+
}, 10000) // 10 second timeout
404+
})
405+
406+
const parsePromise = new Promise<any>((resolve) => {
407+
try {
408+
const tree = parser.parse(fileContent)
409+
resolve(tree)
410+
} catch (err) {
411+
console.error(`Parser error for ${filePath}: ${err instanceof Error ? err.message : err}`)
412+
resolve(null)
413+
}
414+
})
415+
416+
const tree = await Promise.race([parsePromise, parseTimeout])
417+
418+
if (!tree || !tree.rootNode) {
419+
console.warn(`Failed to parse ${filePath} - skipping`)
420+
return null
421+
}
399422

400423
// Apply the query to the AST and get the captures
401-
const captures = tree ? query.captures(tree.rootNode) : []
424+
const captures = query.captures(tree.rootNode) || []
402425

403426
// Split the file content into individual lines
404427
const lines = fileContent.split("\n")
405428

406429
// Process the captures
407430
return processCaptures(captures, lines, extLang)
408431
} catch (error) {
409-
console.log(`Error parsing file: ${error}\n`)
432+
console.error(`Error parsing file ${filePath}: ${error instanceof Error ? error.message : error}`)
410433
// Return null on parsing error to avoid showing error messages in the output
411434
return null
412435
}

0 commit comments

Comments
 (0)