Skip to content

Commit d397d13

Browse files
authored
fix: restore JSON backwards compatibility for .roomodes files (#5199)
1 parent f1feccc commit d397d13

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

src/core/config/CustomModesManager.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,29 @@ export class CustomModesManager {
123123

124124
try {
125125
return yaml.parse(cleanedContent)
126-
} catch (error) {
127-
const errorMsg = error instanceof Error ? error.message : String(error)
128-
console.error(`[CustomModesManager] Failed to parse YAML from ${filePath}:`, errorMsg)
129-
130-
// Show user-friendly error message for .roomodes files
126+
} catch (yamlError) {
127+
// For .roomodes files, try JSON as fallback
131128
if (filePath.endsWith(ROOMODES_FILENAME)) {
132-
const lineMatch = errorMsg.match(/at line (\d+)/)
133-
const line = lineMatch ? lineMatch[1] : "unknown"
134-
vscode.window.showErrorMessage(t("common:customModes.errors.yamlParseError", { line }))
129+
try {
130+
// Try parsing the original content as JSON (not the cleaned content)
131+
return JSON.parse(content)
132+
} catch (jsonError) {
133+
// JSON also failed, show the original YAML error
134+
const errorMsg = yamlError instanceof Error ? yamlError.message : String(yamlError)
135+
console.error(`[CustomModesManager] Failed to parse YAML from ${filePath}:`, errorMsg)
136+
137+
const lineMatch = errorMsg.match(/at line (\d+)/)
138+
const line = lineMatch ? lineMatch[1] : "unknown"
139+
vscode.window.showErrorMessage(t("common:customModes.errors.yamlParseError", { line }))
140+
141+
// Return empty object to prevent duplicate error handling
142+
return {}
143+
}
135144
}
136145

137-
// Return empty object to prevent duplicate error handling
146+
// For non-.roomodes files, just log and return empty object
147+
const errorMsg = yamlError instanceof Error ? yamlError.message : String(yamlError)
148+
console.error(`[CustomModesManager] Failed to parse YAML from ${filePath}:`, errorMsg)
138149
return {}
139150
}
140151
}
@@ -205,12 +216,7 @@ export class CustomModesManager {
205216
const fileExists = await fileExistsAtPath(filePath)
206217

207218
if (!fileExists) {
208-
await this.queueWrite(() =>
209-
fs.writeFile(
210-
filePath,
211-
yaml.stringify({ customModes: [] }, { lineWidth: 0, defaultStringType: "PLAIN" }),
212-
),
213-
)
219+
await this.queueWrite(() => fs.writeFile(filePath, yaml.stringify({ customModes: [] }, { lineWidth: 0 })))
214220
}
215221

216222
return filePath
@@ -414,7 +420,7 @@ export class CustomModesManager {
414420
content = await fs.readFile(filePath, "utf-8")
415421
} catch (error) {
416422
// File might not exist yet.
417-
content = yaml.stringify({ customModes: [] }, { lineWidth: 0, defaultStringType: "PLAIN" })
423+
content = yaml.stringify({ customModes: [] }, { lineWidth: 0 })
418424
}
419425

420426
let settings
@@ -427,7 +433,7 @@ export class CustomModesManager {
427433
}
428434

429435
settings.customModes = operation(settings.customModes || [])
430-
await fs.writeFile(filePath, yaml.stringify(settings, { lineWidth: 0, defaultStringType: "PLAIN" }), "utf-8")
436+
await fs.writeFile(filePath, yaml.stringify(settings, { lineWidth: 0 }), "utf-8")
431437
}
432438

433439
private async refreshMergedState(): Promise<void> {
@@ -485,10 +491,7 @@ export class CustomModesManager {
485491
public async resetCustomModes(): Promise<void> {
486492
try {
487493
const filePath = await this.getCustomModesFilePath()
488-
await fs.writeFile(
489-
filePath,
490-
yaml.stringify({ customModes: [] }, { lineWidth: 0, defaultStringType: "PLAIN" }),
491-
)
494+
await fs.writeFile(filePath, yaml.stringify({ customModes: [] }, { lineWidth: 0 }))
492495
await this.context.globalState.update("customModes", [])
493496
this.clearCache()
494497
await this.onUpdate()

src/services/marketplace/SimpleInstaller.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class SimpleInstaller {
8484

8585
// Write back to file
8686
await fs.mkdir(path.dirname(filePath), { recursive: true })
87-
const yamlContent = yaml.stringify(existingData, { lineWidth: 0, defaultStringType: "PLAIN" })
87+
const yamlContent = yaml.stringify(existingData, { lineWidth: 0 })
8888
await fs.writeFile(filePath, yamlContent, "utf-8")
8989

9090
// Calculate approximate line number where the new mode was added
@@ -282,11 +282,7 @@ export class SimpleInstaller {
282282
existingData.customModes = existingData.customModes.filter((mode: any) => mode.slug !== modeData.slug)
283283

284284
// Always write back the file, even if empty
285-
await fs.writeFile(
286-
filePath,
287-
yaml.stringify(existingData, { lineWidth: 0, defaultStringType: "PLAIN" }),
288-
"utf-8",
289-
)
285+
await fs.writeFile(filePath, yaml.stringify(existingData, { lineWidth: 0 }), "utf-8")
290286
}
291287
} catch (error: any) {
292288
if (error.code === "ENOENT") {

src/utils/migrateSettings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async function migrateCustomModesToYaml(settingsDir: string, outputChannel: vsco
9393
const customModesData = yaml.parse(jsonContent)
9494

9595
// Convert to YAML with no line width limit to prevent line breaks
96-
const yamlContent = yaml.stringify(customModesData, { lineWidth: 0, defaultStringType: "PLAIN" })
96+
const yamlContent = yaml.stringify(customModesData, { lineWidth: 0 })
9797

9898
// Write YAML file
9999
await fs.writeFile(newYamlPath, yamlContent, "utf-8")

0 commit comments

Comments
 (0)