@@ -346,10 +346,19 @@ export async function generateMappingCodeCore(mappingRequest: ProcessMappingPara
346346
347347 let customFunctionsTargetPath : string ;
348348 let customFunctionsFileName : string ;
349-
349+
350350 if ( allMappingsRequest . customFunctionsFilePath ) {
351- customFunctionsTargetPath = determineCustomFunctionsPath ( projectRoot , currentActiveFile ) ;
352- customFunctionsFileName = path . basename ( customFunctionsTargetPath ) ;
351+ const absoluteCustomFunctionsPath = determineCustomFunctionsPath ( projectRoot , currentActiveFile ) ;
352+ customFunctionsFileName = path . basename ( absoluteCustomFunctionsPath ) ;
353+
354+ // For workspace projects, make path relative to workspace root
355+ const workspacePath = context . workspacePath ;
356+ if ( workspacePath ) {
357+ customFunctionsTargetPath = path . relative ( workspacePath , absoluteCustomFunctionsPath ) ;
358+ } else {
359+ // Normal project: use relative path from project root
360+ customFunctionsTargetPath = path . relative ( projectRoot , absoluteCustomFunctionsPath ) ;
361+ }
353362 }
354363
355364 // Check if mappings file and custom functions file are the same
@@ -395,7 +404,15 @@ export async function generateMappingCodeCore(mappingRequest: ProcessMappingPara
395404 ) ;
396405 await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
397406
398- let targetFilePath = path . join ( projectRoot , mappingContext . filePath ) ;
407+ // For workspace projects, compute relative file path from workspace root
408+ const workspacePath = context . workspacePath ;
409+ let targetFilePath = mappingContext . filePath ;
410+
411+ if ( workspacePath ) {
412+ // Workspace project: need to include package path prefix (e.g., "foo/mappings.bal")
413+ const absoluteFilePath = path . join ( projectRoot , mappingContext . filePath ) ;
414+ targetFilePath = path . relative ( workspacePath , absoluteFilePath ) ;
415+ }
399416
400417 const generatedSourceFiles = buildMappingFileArray (
401418 targetFilePath ,
@@ -416,12 +433,12 @@ export async function generateMappingCodeCore(mappingRequest: ProcessMappingPara
416433
417434 if ( isSameFile ) {
418435 const mergedContent = `${ generatedFunctionDefinition . source } \n${ customContent } ` ;
419- assistantResponse += `<code filename="${ mappingContext . filePath } " type="ai_map">\n\`\`\`ballerina\n${ mergedContent } \n\`\`\`\n</code>` ;
436+ assistantResponse += `<code filename="${ targetFilePath } " type="ai_map">\n\`\`\`ballerina\n${ mergedContent } \n\`\`\`\n</code>` ;
420437 } else {
421- assistantResponse += `<code filename="${ mappingContext . filePath } " type="ai_map">\n\`\`\`ballerina\n${ generatedFunctionDefinition . source } \n\`\`\`\n</code>` ;
438+ assistantResponse += `<code filename="${ targetFilePath } " type="ai_map">\n\`\`\`ballerina\n${ generatedFunctionDefinition . source } \n\`\`\`\n</code>` ;
422439
423440 if ( codeRepairResult . customFunctionsContent ) {
424- assistantResponse += `<code filename="${ customFunctionsFileName } " type="ai_map">\n\`\`\`ballerina\n${ codeRepairResult . customFunctionsContent } \n\`\`\`\n</code>` ;
441+ assistantResponse += `<code filename="${ customFunctionsTargetPath || customFunctionsFileName } " type="ai_map">\n\`\`\`ballerina\n${ codeRepairResult . customFunctionsContent } \n\`\`\`\n</code>` ;
425442 }
426443 }
427444
@@ -650,10 +667,19 @@ export async function generateInlineMappingCodeCore(inlineMappingRequest: Metada
650667
651668 let customFunctionsTargetPath : string | undefined ;
652669 let customFunctionsFileName : string | undefined ;
653-
670+
654671 if ( inlineMappingsResult . allMappingsRequest . customFunctionsFilePath ) {
655- customFunctionsTargetPath = determineCustomFunctionsPath ( projectRoot , targetFileName ) ;
656- customFunctionsFileName = path . basename ( customFunctionsTargetPath ) ;
672+ const absoluteCustomFunctionsPath = determineCustomFunctionsPath ( projectRoot , targetFileName ) ;
673+ customFunctionsFileName = path . basename ( absoluteCustomFunctionsPath ) ;
674+
675+ // For workspace projects, make path relative to workspace root
676+ const workspacePath = context . workspacePath ;
677+ if ( workspacePath ) {
678+ customFunctionsTargetPath = path . relative ( workspacePath , absoluteCustomFunctionsPath ) ;
679+ } else {
680+ // Normal project: use relative path from project root
681+ customFunctionsTargetPath = path . relative ( projectRoot , absoluteCustomFunctionsPath ) ;
682+ }
657683 }
658684
659685 // Check if mappings file and custom functions file are the same
@@ -691,8 +717,17 @@ export async function generateInlineMappingCodeCore(inlineMappingRequest: Metada
691717 } , langClient , projectRoot ) ;
692718 }
693719
720+ // For workspace projects, compute relative file path from workspace root
721+ let targetFilePath = path . relative ( projectRoot , context . documentUri ) ;
722+ const workspacePath = context . workspacePath ;
723+
724+ if ( workspacePath ) {
725+ // Workspace project: make path relative to workspace root (e.g., "foo/mappings.bal")
726+ targetFilePath = path . relative ( workspacePath , context . documentUri ) ;
727+ }
728+
694729 const generatedSourceFiles = buildMappingFileArray (
695- context . documentUri ,
730+ targetFilePath ,
696731 codeRepairResult . finalContent ,
697732 customFunctionsTargetPath ,
698733 codeRepairResult . customFunctionsContent ,
@@ -718,12 +753,12 @@ export async function generateInlineMappingCodeCore(inlineMappingRequest: Metada
718753
719754 if ( isSameFile ) {
720755 const mergedCodeDisplay = customContent ? `${ codeToDisplay } \n${ customContent } ` : codeToDisplay ;
721- assistantResponse += `<code filename="${ targetFileName } " type="ai_map">\n\`\`\`ballerina\n${ mergedCodeDisplay } \n\`\`\`\n</code>` ;
756+ assistantResponse += `<code filename="${ targetFilePath } " type="ai_map">\n\`\`\`ballerina\n${ mergedCodeDisplay } \n\`\`\`\n</code>` ;
722757 } else {
723- assistantResponse += `<code filename="${ targetFileName } " type="ai_map">\n\`\`\`ballerina\n${ codeToDisplay } \n\`\`\`\n</code>` ;
758+ assistantResponse += `<code filename="${ targetFilePath } " type="ai_map">\n\`\`\`ballerina\n${ codeToDisplay } \n\`\`\`\n</code>` ;
724759
725760 if ( codeRepairResult . customFunctionsContent ) {
726- assistantResponse += `<code filename="${ customFunctionsFileName } " type="ai_map">\n\`\`\`ballerina\n${ codeRepairResult . customFunctionsContent } \n\`\`\`\n</code>` ;
761+ assistantResponse += `<code filename="${ customFunctionsTargetPath || customFunctionsFileName } " type="ai_map">\n\`\`\`ballerina\n${ codeRepairResult . customFunctionsContent } \n\`\`\`\n</code>` ;
727762 }
728763 }
729764
0 commit comments