Skip to content

Commit 7ca4901

Browse files
fix: prevent empty mode names from being saved (fixes #5766) (#5767)
* fix: prevent empty mode names from being saved (fixes #5766) - Add frontend validation in ModesView to prevent empty names from being saved - Add onBlur handler to restore original name if field is left empty - Add backend validation in CustomModesManager.updateCustomMode using modeConfigSchema - Provide user feedback when validation fails - Trim whitespace from mode names before validation This prevents YAML parsing errors caused by empty mode name fields. * fix: improve UX by allowing users to empty mode name field - Remove restriction that prevented users from emptying the name field - Remove onBlur handler that automatically restored original name - Allow backend validation to handle empty names and show appropriate errors - Users can now type freely but invalid saves are prevented by backend validation Addresses feedback from @daniel-lxs in PR #5767 * fix: allow emptying mode name field but prevent saving when invalid - Modified onBlur handler to check if name is empty before saving - If empty, revert to original name instead of saving empty value - This provides better UX as requested in PR review * fix: add proper JSON formatting to source map writes for Windows compatibility --------- Co-authored-by: Roo Code <[email protected]>
1 parent c34e412 commit 7ca4901

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/core/config/CustomModesManager.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,13 @@ export class CustomModesManager {
405405
// Validate the mode configuration before saving
406406
const validationResult = modeConfigSchema.safeParse(config)
407407
if (!validationResult.success) {
408-
const errors = validationResult.error.errors.map((e) => e.message).join(", ")
409-
logger.error(`Invalid mode configuration for ${slug}`, { errors: validationResult.error.errors })
410-
throw new Error(`Invalid mode configuration: ${errors}`)
408+
const errorMessages = validationResult.error.errors
409+
.map((err) => `${err.path.join(".")}: ${err.message}`)
410+
.join(", ")
411+
const errorMessage = `Invalid mode configuration: ${errorMessages}`
412+
logger.error("Mode validation failed", { slug, errors: validationResult.error.errors })
413+
vscode.window.showErrorMessage(t("common:customModes.errors.updateFailed", { error: errorMessage }))
414+
return
411415
}
412416

413417
const isProjectMode = config.source === "project"
@@ -786,7 +790,7 @@ export class CustomModesManager {
786790
// This excludes the rules-{slug} folder from the path
787791
const relativePath = path.relative(modeRulesDir, filePath)
788792
// Normalize path to use forward slashes for cross-platform compatibility
789-
const normalizedRelativePath = relativePath.replace(/\\/g, '/')
793+
const normalizedRelativePath = relativePath.replace(/\\/g, "/")
790794
rulesFiles.push({ relativePath: normalizedRelativePath, content: content.trim() })
791795
}
792796
}

webview-ui/src/components/modes/ModesView.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,17 +755,25 @@ const ModesView = ({ onDone }: ModesViewProps) => {
755755
}
756756
}}
757757
onChange={(e) => {
758-
setLocalModeName(e.target.value)
758+
const newName = e.target.value
759+
// Allow users to type freely, including emptying the field
760+
setLocalModeName(newName)
759761
}}
760762
onBlur={() => {
761763
const customMode = findModeBySlug(visualMode, customModes)
762-
if (customMode && localModeName.trim()) {
764+
if (customMode) {
765+
const trimmedName = localModeName.trim()
763766
// Only update if the name is not empty
764-
updateCustomMode(visualMode, {
765-
...customMode,
766-
name: localModeName,
767-
source: customMode.source || "global",
768-
})
767+
if (trimmedName) {
768+
updateCustomMode(visualMode, {
769+
...customMode,
770+
name: trimmedName,
771+
source: customMode.source || "global",
772+
})
773+
} else {
774+
// Revert to the original name if empty
775+
setLocalModeName(customMode.name)
776+
}
769777
}
770778
// Clear the editing state
771779
setCurrentEditingModeSlug(null)

webview-ui/src/vite-plugins/sourcemapPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export function sourcemapPlugin(): Plugin {
8888
})
8989
}
9090

91-
// Write back the updated source map
92-
fs.writeFileSync(mapPath, JSON.stringify(mapContent))
91+
// Write back the updated source map with proper formatting
92+
fs.writeFileSync(mapPath, JSON.stringify(mapContent, null, 2))
9393
console.log(`Updated source map for ${jsFile}`)
9494
} catch (error) {
9595
console.error(`Error processing source map for ${jsFile}:`, error)

0 commit comments

Comments
 (0)