Memory Import Processor
The Memory Import Processor is a feature that allows you to modularize your
GEMINI.md files by importing content from other files using the @file.md
syntax.
Overview
Section titled âOverviewâThis feature enables you to break down large GEMINI.md files into smaller, more manageable components that can be reused across different contexts. The import processor supports both relative and absolute paths, with built-in safety features to prevent circular imports and ensure file access security.
Use the @ symbol followed by the path to the file you want to import:
# Main GEMINI.md file
This is the main content.
@./components/instructions.md
More content here.
@./shared/configuration.mdSupported path formats
Section titled âSupported path formatsâRelative paths
Section titled âRelative pathsâ@./file.md- Import from the same directory@../file.md- Import from parent directory@./components/file.md- Import from subdirectory
Absolute paths
Section titled âAbsolute pathsâ@/absolute/path/to/file.md- Import using absolute path
Examples
Section titled âExamplesâBasic import
Section titled âBasic importâ# My GEMINI.md
Welcome to my project!
@./get-started.md
## Features
@./features/overview.mdNested imports
Section titled âNested importsâThe imported files can themselves contain imports, creating a nested structure:
@./header.md @./content.md @./footer.md# Project Header
@./shared/title.mdSafety features
Section titled âSafety featuresâCircular import detection
Section titled âCircular import detectionâThe processor automatically detects and prevents circular imports:
@./file-b.md@./file-a.md <!-- This will be detected and prevented -->File access security
Section titled âFile access securityâThe validateImportPath function ensures that imports are only allowed from
specified directories, preventing access to sensitive files outside the allowed
scope.
Maximum import depth
Section titled âMaximum import depthâTo prevent infinite recursion, thereâs a configurable maximum import depth (default: 5 levels).
Error handling
Section titled âError handlingâMissing files
Section titled âMissing filesâIf a referenced file doesnât exist, the import will fail gracefully with an error comment in the output.
File access errors
Section titled âFile access errorsâPermission issues or other file system errors are handled gracefully with appropriate error messages.
Code region detection
Section titled âCode region detectionâThe import processor uses the marked library to detect code blocks and inline
code spans, ensuring that @ imports inside these regions are properly ignored.
This provides robust handling of nested code blocks and complex Markdown
structures.
Import tree structure
Section titled âImport tree structureâThe processor returns an import tree that shows the hierarchy of imported files,
similar to Claudeâs /memory feature. This helps users debug problems with
their GEMINI.md files by showing which files were read and their import
relationships.
Example tree structure:
Memory Files L project: GEMINI.md L a.md L b.md L c.md L d.md L e.md L f.md L included.mdThe tree preserves the order that files were imported and shows the complete import chain for debugging purposes.
Comparison to Claude Codeâs /memory (claude.md) approach
Section titled âComparison to Claude Codeâs /memory (claude.md) approachâClaude Codeâs /memory feature (as seen in claude.md) produces a flat, linear
document by concatenating all included files, always marking file boundaries
with clear comments and path names. It does not explicitly present the import
hierarchy, but the LLM receives all file contents and paths, which is sufficient
for reconstructing the hierarchy if needed.
[!NOTE] The import tree is mainly for clarity during development and has limited relevance to LLM consumption.
API reference
Section titled âAPI referenceâprocessImports(content, basePath, debugMode?, importState?)
Section titled âprocessImports(content, basePath, debugMode?, importState?)âProcesses import statements in GEMINI.md content.
Parameters:
content(string): The content to process for importsbasePath(string): The directory path where the current file is locateddebugMode(boolean, optional): Whether to enable debug logging (default: false)importState(ImportState, optional): State tracking for circular import prevention
Returns: Promise<ProcessImportsResult> - Object containing processed content and import tree
ProcessImportsResult
Section titled âProcessImportsResultâinterface ProcessImportsResult { content: string; // The processed content with imports resolved importTree: MemoryFile; // Tree structure showing the import hierarchy}MemoryFile
Section titled âMemoryFileâinterface MemoryFile { path: string; // The file path imports?: MemoryFile[]; // Direct imports, in the order they were imported}validateImportPath(importPath, basePath, allowedDirectories)
Section titled âvalidateImportPath(importPath, basePath, allowedDirectories)âValidates import paths to ensure they are safe and within allowed directories.
Parameters:
importPath(string): The import path to validatebasePath(string): The base directory for resolving relative pathsallowedDirectories(string[]): Array of allowed directory paths
Returns: boolean - Whether the import path is valid
findProjectRoot(startDir)
Section titled âfindProjectRoot(startDir)âFinds the project root by searching for a .git directory upwards from the
given start directory. Implemented as an async function using non-blocking
file system APIs to avoid blocking the Node.js event loop.
Parameters:
startDir(string): The directory to start searching from
Returns: Promise<string> - The project root directory (or the start
directory if no .git is found)
Best Practices
Section titled âBest Practicesâ- Use descriptive file names for imported components
- Keep imports shallow - avoid deeply nested import chains
- Document your structure - maintain a clear hierarchy of imported files
- Test your imports - ensure all referenced files exist and are accessible
- Use relative paths when possible for better portability
Troubleshooting
Section titled âTroubleshootingâCommon issues
Section titled âCommon issuesâ- Import not working: Check that the file exists and the path is correct
- Circular import warnings: Review your import structure for circular references
- Permission errors: Ensure the files are readable and within allowed directories
- Path resolution issues: Use absolute paths if relative paths arenât resolving correctly
Debug mode
Section titled âDebug modeâEnable debug mode to see detailed logging of the import process:
const result = await processImports(content, basePath, true);