mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-22 03:51:22 -07:00
fix(core): fix three JIT context bugs in read_file, read_many_files, and memoryDiscovery (#22679)
This commit is contained in:
@@ -1155,6 +1155,60 @@ included directory memory
|
||||
// Ensure outer memory is NOT loaded
|
||||
expect(result.files.find((f) => f.path === outerMemory)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should resolve file target to its parent directory for traversal', async () => {
|
||||
const rootDir = await createEmptyDir(
|
||||
path.join(testRootDir, 'jit_file_resolve'),
|
||||
);
|
||||
const subDir = await createEmptyDir(path.join(rootDir, 'src'));
|
||||
|
||||
// Create the target file so fs.stat can identify it as a file
|
||||
const targetFile = await createTestFile(
|
||||
path.join(subDir, 'app.ts'),
|
||||
'const x = 1;',
|
||||
);
|
||||
|
||||
const subDirMemory = await createTestFile(
|
||||
path.join(subDir, DEFAULT_CONTEXT_FILENAME),
|
||||
'Src context rules',
|
||||
);
|
||||
|
||||
const result = await loadJitSubdirectoryMemory(
|
||||
targetFile,
|
||||
[rootDir],
|
||||
new Set(),
|
||||
);
|
||||
|
||||
// Should find the GEMINI.md in the same directory as the file
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].path).toBe(subDirMemory);
|
||||
expect(result.files[0].content).toBe('Src context rules');
|
||||
});
|
||||
|
||||
it('should handle non-existent file target by using parent directory', async () => {
|
||||
const rootDir = await createEmptyDir(
|
||||
path.join(testRootDir, 'jit_nonexistent'),
|
||||
);
|
||||
const subDir = await createEmptyDir(path.join(rootDir, 'src'));
|
||||
|
||||
// Target file does NOT exist (e.g. write_file creating a new file)
|
||||
const targetFile = path.join(subDir, 'new-file.ts');
|
||||
|
||||
const subDirMemory = await createTestFile(
|
||||
path.join(subDir, DEFAULT_CONTEXT_FILENAME),
|
||||
'Rules for new files',
|
||||
);
|
||||
|
||||
const result = await loadJitSubdirectoryMemory(
|
||||
targetFile,
|
||||
[rootDir],
|
||||
new Set(),
|
||||
);
|
||||
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].path).toBe(subDirMemory);
|
||||
expect(result.files[0].content).toBe('Rules for new files');
|
||||
});
|
||||
});
|
||||
|
||||
it('refreshServerHierarchicalMemory should refresh memory and update config', async () => {
|
||||
|
||||
@@ -767,8 +767,24 @@ export async function loadJitSubdirectoryMemory(
|
||||
`(Trusted root: ${bestRoot})`,
|
||||
);
|
||||
|
||||
// Traverse from target up to the trusted root
|
||||
const potentialPaths = await findUpwardGeminiFiles(resolvedTarget, bestRoot);
|
||||
// Resolve the target to a directory before traversing upward.
|
||||
// When the target is a file (e.g. /app/src/file.ts), start from its
|
||||
// parent directory to avoid a wasted fs.access check on a nonsensical
|
||||
// path like /app/src/file.ts/GEMINI.md.
|
||||
let startDir = resolvedTarget;
|
||||
try {
|
||||
const stat = await fs.stat(resolvedTarget);
|
||||
if (stat.isFile()) {
|
||||
startDir = normalizePath(path.dirname(resolvedTarget));
|
||||
}
|
||||
} catch {
|
||||
// If stat fails (e.g. file doesn't exist yet for write_file),
|
||||
// assume it's a file path and use its parent directory.
|
||||
startDir = normalizePath(path.dirname(resolvedTarget));
|
||||
}
|
||||
|
||||
// Traverse from the resolved directory up to the trusted root
|
||||
const potentialPaths = await findUpwardGeminiFiles(startDir, bestRoot);
|
||||
|
||||
if (potentialPaths.length === 0) {
|
||||
return { files: [], fileIdentities: [] };
|
||||
|
||||
Reference in New Issue
Block a user