From 79ec92f11ce4a9ee9e4c863d61c5dd606f090062 Mon Sep 17 00:00:00 2001 From: Jagjeevan Kashid Date: Thu, 19 Feb 2026 23:35:20 +0530 Subject: [PATCH 1/2] fix(memport): strip HTML comments --- docs/core/memport.md | 6 ++++ .../src/utils/memoryImportProcessor.test.ts | 26 ++++++++++++++ .../core/src/utils/memoryImportProcessor.ts | 36 +++++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/docs/core/memport.md b/docs/core/memport.md index 1460404792..9f5bed81b8 100644 --- a/docs/core/memport.md +++ b/docs/core/memport.md @@ -121,6 +121,12 @@ code spans, ensuring that `@` imports inside these regions are properly ignored. This provides robust handling of nested code blocks and complex Markdown structures. +## HTML comment handling + +The import processor removes HTML comments from memory content before resolving +imports. Any `@` imports inside HTML comments are ignored, and the comments do +not appear in the final prompt. + ## Import tree structure The processor returns an import tree that shows the hierarchy of imported files, diff --git a/packages/core/src/utils/memoryImportProcessor.test.ts b/packages/core/src/utils/memoryImportProcessor.test.ts index 3c9a74b604..57cf2feb7a 100644 --- a/packages/core/src/utils/memoryImportProcessor.test.ts +++ b/packages/core/src/utils/memoryImportProcessor.test.ts @@ -378,6 +378,32 @@ describe('memoryImportProcessor', () => { ); }); + it('should strip HTML comments and ignore imports inside them', async () => { + const content = [ + 'Header', + '', + 'Real import @./real.md', + 'Inline comment after text', + ].join('\n'); + const basePath = testPath('test', 'path'); + const importedContent = 'Real imported content'; + + mockedFs.access.mockResolvedValue(undefined); + mockedFs.readFile.mockResolvedValue(importedContent); + + const result = await processImports(content, basePath, true); + + expect(result.content).toContain(importedContent); + expect(result.content).not.toContain('user comment that should be removed'); + expect(result.content).not.toContain('fake.md'); + + expect(mockedFs.readFile).toHaveBeenCalledTimes(1); + expect(mockedFs.readFile).toHaveBeenCalledWith( + path.resolve(basePath, './real.md'), + 'utf-8', + ); + }); + it('should handle nested tokens and non-unique content correctly', async () => { // This test verifies the robust findCodeRegions implementation // that recursively walks the token tree and handles non-unique content diff --git a/packages/core/src/utils/memoryImportProcessor.ts b/packages/core/src/utils/memoryImportProcessor.ts index bf20bd6c13..bf25b079ef 100644 --- a/packages/core/src/utils/memoryImportProcessor.ts +++ b/packages/core/src/utils/memoryImportProcessor.ts @@ -163,6 +163,33 @@ function findCodeRegions(content: string): Array<[number, number]> { return regions; } +function stripHtmlComments(content: string): string { + if (!content.includes('', start + 4); + if (end === -1) { + break; + } + + cursor = end + 3; + } + + return result; +} + /** * Processes import statements in GEMINI.md content * Supports @path/to/file syntax for importing content from other files @@ -186,6 +213,8 @@ export async function processImports( projectRoot?: string, importFormat: 'flat' | 'tree' = 'tree', ): Promise { + content = stripHtmlComments(content); + if (!projectRoot) { projectRoot = await findProjectRoot(basePath); } @@ -216,6 +245,7 @@ export async function processImports( filePath: string, depth: number, ) { + const sanitizedContent = stripHtmlComments(fileContent); // Normalize the file path to ensure consistent comparison const normalizedPath = path.normalize(filePath); @@ -226,11 +256,11 @@ export async function processImports( processedFiles.add(normalizedPath); // Add this file to the flat list - flatFiles.push({ path: normalizedPath, content: fileContent }); + flatFiles.push({ path: normalizedPath, content: sanitizedContent }); // Find imports in this file - const codeRegions = findCodeRegions(fileContent); - const imports = findImports(fileContent); + const codeRegions = findCodeRegions(sanitizedContent); + const imports = findImports(sanitizedContent); // Process imports in reverse order to handle indices correctly for (let i = imports.length - 1; i >= 0; i--) { From 06c52bcafc78c32e0abca54bca47b1c49788c935 Mon Sep 17 00:00:00 2001 From: Jagjeevan Kashid Date: Thu, 19 Feb 2026 23:56:36 +0530 Subject: [PATCH 2/2] fix(memport): optimize comment stripping --- .../core/src/utils/memoryImportProcessor.ts | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/core/src/utils/memoryImportProcessor.ts b/packages/core/src/utils/memoryImportProcessor.ts index bf25b079ef..b33b43541c 100644 --- a/packages/core/src/utils/memoryImportProcessor.ts +++ b/packages/core/src/utils/memoryImportProcessor.ts @@ -164,30 +164,28 @@ function findCodeRegions(content: string): Array<[number, number]> { } function stripHtmlComments(content: string): string { - if (!content.includes(''; + let start = content.indexOf(startToken); + if (start === -1) return content; - let result = ''; + const parts: string[] = []; let cursor = 0; - const len = content.length; - while (cursor < len) { - const start = content.indexOf('', start + 4); + const end = content.indexOf(endToken, start + startToken.length); if (end === -1) { - break; + return parts.join(''); } - cursor = end + 3; + cursor = end + endToken.length; + start = content.indexOf(startToken, cursor); } - return result; + parts.push(content.slice(cursor)); + return parts.join(''); } /**