Skip to content
Merged
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
45 changes: 24 additions & 21 deletions src/core/config/CustomModesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,29 @@ export class CustomModesManager {

try {
return yaml.parse(cleanedContent)
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
console.error(`[CustomModesManager] Failed to parse YAML from ${filePath}:`, errorMsg)

// Show user-friendly error message for .roomodes files
} catch (yamlError) {
// For .roomodes files, try JSON as fallback
if (filePath.endsWith(ROOMODES_FILENAME)) {
const lineMatch = errorMsg.match(/at line (\d+)/)
const line = lineMatch ? lineMatch[1] : "unknown"
vscode.window.showErrorMessage(t("common:customModes.errors.yamlParseError", { line }))
try {
// Try parsing the original content as JSON (not the cleaned content)
return JSON.parse(content)
} catch (jsonError) {
// JSON also failed, show the original YAML error
const errorMsg = yamlError instanceof Error ? yamlError.message : String(yamlError)
console.error(`[CustomModesManager] Failed to parse YAML from ${filePath}:`, errorMsg)

const lineMatch = errorMsg.match(/at line (\d+)/)
const line = lineMatch ? lineMatch[1] : "unknown"
vscode.window.showErrorMessage(t("common:customModes.errors.yamlParseError", { line }))

// Return empty object to prevent duplicate error handling
return {}
}
}

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

if (!fileExists) {
await this.queueWrite(() =>
fs.writeFile(
filePath,
yaml.stringify({ customModes: [] }, { lineWidth: 0, defaultStringType: "PLAIN" }),
),
)
await this.queueWrite(() => fs.writeFile(filePath, yaml.stringify({ customModes: [] }, { lineWidth: 0 })))
}

return filePath
Expand Down Expand Up @@ -414,7 +420,7 @@ export class CustomModesManager {
content = await fs.readFile(filePath, "utf-8")
} catch (error) {
// File might not exist yet.
content = yaml.stringify({ customModes: [] }, { lineWidth: 0, defaultStringType: "PLAIN" })
content = yaml.stringify({ customModes: [] }, { lineWidth: 0 })
}

let settings
Expand All @@ -427,7 +433,7 @@ export class CustomModesManager {
}

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

private async refreshMergedState(): Promise<void> {
Expand Down Expand Up @@ -485,10 +491,7 @@ export class CustomModesManager {
public async resetCustomModes(): Promise<void> {
try {
const filePath = await this.getCustomModesFilePath()
await fs.writeFile(
filePath,
yaml.stringify({ customModes: [] }, { lineWidth: 0, defaultStringType: "PLAIN" }),
)
await fs.writeFile(filePath, yaml.stringify({ customModes: [] }, { lineWidth: 0 }))
await this.context.globalState.update("customModes", [])
this.clearCache()
await this.onUpdate()
Expand Down
8 changes: 2 additions & 6 deletions src/services/marketplace/SimpleInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class SimpleInstaller {

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

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

// Always write back the file, even if empty
await fs.writeFile(
filePath,
yaml.stringify(existingData, { lineWidth: 0, defaultStringType: "PLAIN" }),
"utf-8",
)
await fs.writeFile(filePath, yaml.stringify(existingData, { lineWidth: 0 }), "utf-8")
}
} catch (error: any) {
if (error.code === "ENOENT") {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/migrateSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function migrateCustomModesToYaml(settingsDir: string, outputChannel: vsco
const customModesData = yaml.parse(jsonContent)

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

// Write YAML file
await fs.writeFile(newYamlPath, yamlContent, "utf-8")
Expand Down
Loading