@@ -5,6 +5,7 @@ import { CustomModesSettingsSchema } from "./CustomModesSchema"
55import { ModeConfig } from "../../shared/modes"
66import { fileExistsAtPath } from "../../utils/fs"
77import { arePathsEqual } from "../../utils/path"
8+ import { logger } from "../../utils/logging"
89
910const ROOMODES_FILENAME = ".roomodes"
1011
@@ -214,14 +215,26 @@ export class CustomModesManager {
214215 await this . context . globalState . update ( "customModes" , mergedModes )
215216 return mergedModes
216217 }
217-
218218 async updateCustomMode ( slug : string , config : ModeConfig ) : Promise < void > {
219219 try {
220220 const isProjectMode = config . source === "project"
221- const targetPath = isProjectMode ? await this . getWorkspaceRoomodes ( ) : await this . getCustomModesFilePath ( )
221+ let targetPath : string
222222
223- if ( isProjectMode && ! targetPath ) {
224- throw new Error ( "No workspace folder found for project-specific mode" )
223+ if ( isProjectMode ) {
224+ const workspaceFolders = vscode . workspace . workspaceFolders
225+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
226+ logger . error ( "Failed to update project mode: No workspace folder found" , { slug } )
227+ throw new Error ( "No workspace folder found for project-specific mode" )
228+ }
229+ const workspaceRoot = workspaceFolders [ 0 ] . uri . fsPath
230+ targetPath = path . join ( workspaceRoot , ROOMODES_FILENAME )
231+ const exists = await fileExistsAtPath ( targetPath )
232+ logger . info ( `${ exists ? "Updating" : "Creating" } project mode in ${ ROOMODES_FILENAME } ` , {
233+ slug,
234+ workspace : workspaceRoot ,
235+ } )
236+ } else {
237+ targetPath = await this . getCustomModesFilePath ( )
225238 }
226239
227240 await this . queueWrite ( async ( ) => {
@@ -231,7 +244,7 @@ export class CustomModesManager {
231244 source : isProjectMode ? ( "project" as const ) : ( "global" as const ) ,
232245 }
233246
234- await this . updateModesInFile ( targetPath ! , ( modes ) => {
247+ await this . updateModesInFile ( targetPath , ( modes ) => {
235248 const updatedModes = modes . filter ( ( m ) => m . slug !== slug )
236249 updatedModes . push ( modeWithSource )
237250 return updatedModes
@@ -240,9 +253,9 @@ export class CustomModesManager {
240253 await this . refreshMergedState ( )
241254 } )
242255 } catch ( error ) {
243- vscode . window . showErrorMessage (
244- ` Failed to update custom mode: ${ error instanceof Error ? error . message : String ( error ) } ` ,
245- )
256+ const errorMessage = error instanceof Error ? error . message : String ( error )
257+ logger . error ( " Failed to update custom mode" , { slug , error : errorMessage } )
258+ vscode . window . showErrorMessage ( `Failed to update custom mode: ${ errorMessage } ` )
246259 }
247260 }
248261 private async updateModesInFile ( filePath : string , operation : ( modes : ModeConfig [ ] ) => ModeConfig [ ] ) : Promise < void > {
0 commit comments