Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/services/tree-sitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,41 @@ async function parseFile(

try {
// Parse the file content into an Abstract Syntax Tree (AST)
const tree = parser.parse(fileContent)
// Add timeout to prevent hanging on large or complex files
const parseTimeout = new Promise<any>((resolve) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using any type here reduces type safety. Could we define proper types for the tree structure to maintain type safety throughout the Promise chain?

setTimeout(() => {
console.warn(`Parsing timeout for ${filePath} - file may be too large or complex`)
resolve(null)
}, 10000) // 10 second timeout
})

const parsePromise = new Promise<any>((resolve) => {
try {
const tree = parser.parse(fileContent)
resolve(tree)
} catch (err) {
console.error(`Parser error for ${filePath}: ${err instanceof Error ? err.message : err}`)
resolve(null)
}
})

const tree = await Promise.race([parsePromise, parseTimeout])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this approach intentional? Using Promise.race here means the parser might continue running in the background even after the timeout resolves. This could lead to resource consumption.

Consider using AbortController or a similar mechanism to actually cancel the parsing operation when the timeout occurs.


if (!tree || !tree.rootNode) {
console.warn(`Failed to parse ${filePath} - skipping`)
return null
}

// Apply the query to the AST and get the captures
const captures = tree ? query.captures(tree.rootNode) : []
const captures = query.captures(tree.rootNode) || []

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

// Process the captures
return processCaptures(captures, lines, extLang)
} catch (error) {
console.log(`Error parsing file: ${error}\n`)
console.error(`Error parsing file ${filePath}: ${error instanceof Error ? error.message : error}`)
// Return null on parsing error to avoid showing error messages in the output
return null
}
Expand Down
Loading
Loading