fix: prevent activation crash with Swift files during code indexing

- Add try-catch block around Swift parser loading to handle failures gracefully
- Add error handling in code parser for Swift file parsing with fallback to basic chunking
- Add defensive checks in extension initialization to prevent code indexing errors from crashing the extension
- Add specific logging for Swift-related errors to help with debugging

Fixes #6555
This commit is contained in:
Roo Code 2025-08-01 15:57:46 +00:00
parent 305a5da369
commit a76a862d82
4 changed files with 46 additions and 6 deletions

View file

@ -111,6 +111,15 @@ export async function activate(context: vscode.ExtensionContext) {
outputChannel.appendLine(
`[CodeIndexManager] Error during background CodeIndexManager configuration/indexing: ${error.message || error}`,
)
// Log additional details for Swift-related errors
if (error instanceof Error && error.message.includes("swift")) {
outputChannel.appendLine(
`[CodeIndexManager] Swift-related error detected. This may be due to Swift parser initialization issues.`,
)
outputChannel.appendLine(`[CodeIndexManager] Stack trace: ${error.stack}`)
}
// Don't let code indexing errors crash the entire extension
console.error("CodeIndexManager initialization error:", error)
}
const provider = new ClineProvider(context, outputChannel, "sidebar", contextProxy, codeIndexManager, mdmService)

View file

@ -144,11 +144,32 @@ export class CodeParser implements ICodeParser {
return []
}
const tree = language.parser.parse(content)
let tree
let captures: any[] = []
// We don't need to get the query string from languageQueries since it's already loaded
// in the language object
const captures = tree ? language.query.captures(tree.rootNode) : []
try {
tree = language.parser.parse(content)
// We don't need to get the query string from languageQueries since it's already loaded
// in the language object
captures = tree ? language.query.captures(tree.rootNode) : []
} catch (error) {
console.error(`Error parsing ${ext} file ${filePath}:`, error)
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
error: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
stack: error instanceof Error ? sanitizeErrorMessage(error.stack || "") : undefined,
location: "parseContent:parseTree",
fileExtension: ext,
filePath: sanitizeErrorMessage(filePath),
})
// For Swift files specifically, fall back to basic chunking if parsing fails
if (ext === "swift" && content.length >= MIN_BLOCK_CHARS) {
console.warn(`Falling back to basic chunking for Swift file: ${filePath}`)
return this._performFallbackChunking(filePath, content, fileHash, seenSegmentHashes)
}
return []
}
// Check if captures are empty
if (captures.length === 0) {

View file

@ -407,6 +407,10 @@ async function parseFile(
return processCaptures(captures, lines, extLang)
} catch (error) {
console.log(`Error parsing file: ${error}\n`)
// Special handling for Swift files - log more details
if (extLang === "swift") {
console.error(`Swift parsing error for file ${filePath}: ${error instanceof Error ? error.message : error}`)
}
// Return null on parsing error to avoid showing error messages in the output
return null
}

View file

@ -150,8 +150,14 @@ export async function loadRequiredLanguageParsers(filesToParse: string[], source
query = new Query(language, phpQuery)
break
case "swift":
language = await loadLanguage("swift", sourceDirectory)
query = new Query(language, swiftQuery)
try {
language = await loadLanguage("swift", sourceDirectory)
query = new Query(language, swiftQuery)
} catch (error) {
console.error(`Failed to load Swift parser: ${error instanceof Error ? error.message : error}`)
// Skip this file if Swift parser fails to load
continue
}
break
case "kt":
case "kts":