Skip to content

Commit a76a862

Browse files
committed
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
1 parent 305a5da commit a76a862

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

src/extension.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ export async function activate(context: vscode.ExtensionContext) {
111111
outputChannel.appendLine(
112112
`[CodeIndexManager] Error during background CodeIndexManager configuration/indexing: ${error.message || error}`,
113113
)
114+
// Log additional details for Swift-related errors
115+
if (error instanceof Error && error.message.includes("swift")) {
116+
outputChannel.appendLine(
117+
`[CodeIndexManager] Swift-related error detected. This may be due to Swift parser initialization issues.`,
118+
)
119+
outputChannel.appendLine(`[CodeIndexManager] Stack trace: ${error.stack}`)
120+
}
121+
// Don't let code indexing errors crash the entire extension
122+
console.error("CodeIndexManager initialization error:", error)
114123
}
115124

116125
const provider = new ClineProvider(context, outputChannel, "sidebar", contextProxy, codeIndexManager, mdmService)

src/services/code-index/processors/parser.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,32 @@ export class CodeParser implements ICodeParser {
144144
return []
145145
}
146146

147-
const tree = language.parser.parse(content)
147+
let tree
148+
let captures: any[] = []
149+
150+
try {
151+
tree = language.parser.parse(content)
152+
// We don't need to get the query string from languageQueries since it's already loaded
153+
// in the language object
154+
captures = tree ? language.query.captures(tree.rootNode) : []
155+
} catch (error) {
156+
console.error(`Error parsing ${ext} file ${filePath}:`, error)
157+
TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, {
158+
error: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
159+
stack: error instanceof Error ? sanitizeErrorMessage(error.stack || "") : undefined,
160+
location: "parseContent:parseTree",
161+
fileExtension: ext,
162+
filePath: sanitizeErrorMessage(filePath),
163+
})
164+
165+
// For Swift files specifically, fall back to basic chunking if parsing fails
166+
if (ext === "swift" && content.length >= MIN_BLOCK_CHARS) {
167+
console.warn(`Falling back to basic chunking for Swift file: ${filePath}`)
168+
return this._performFallbackChunking(filePath, content, fileHash, seenSegmentHashes)
169+
}
148170

149-
// We don't need to get the query string from languageQueries since it's already loaded
150-
// in the language object
151-
const captures = tree ? language.query.captures(tree.rootNode) : []
171+
return []
172+
}
152173

153174
// Check if captures are empty
154175
if (captures.length === 0) {

src/services/tree-sitter/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ async function parseFile(
407407
return processCaptures(captures, lines, extLang)
408408
} catch (error) {
409409
console.log(`Error parsing file: ${error}\n`)
410+
// Special handling for Swift files - log more details
411+
if (extLang === "swift") {
412+
console.error(`Swift parsing error for file ${filePath}: ${error instanceof Error ? error.message : error}`)
413+
}
410414
// Return null on parsing error to avoid showing error messages in the output
411415
return null
412416
}

src/services/tree-sitter/languageParser.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,14 @@ export async function loadRequiredLanguageParsers(filesToParse: string[], source
150150
query = new Query(language, phpQuery)
151151
break
152152
case "swift":
153-
language = await loadLanguage("swift", sourceDirectory)
154-
query = new Query(language, swiftQuery)
153+
try {
154+
language = await loadLanguage("swift", sourceDirectory)
155+
query = new Query(language, swiftQuery)
156+
} catch (error) {
157+
console.error(`Failed to load Swift parser: ${error instanceof Error ? error.message : error}`)
158+
// Skip this file if Swift parser fails to load
159+
continue
160+
}
155161
break
156162
case "kt":
157163
case "kts":

0 commit comments

Comments
 (0)