mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
fix(core): ensure directory exists before writing conversation file (#18429)
Co-authored-by: godwiniheuwa <godwiniheuwa@users.noreply.github.com> Co-authored-by: ruintheextinct <deepkarma001@gmail.com>
This commit is contained in:
@@ -745,4 +745,36 @@ describe('ChatRecordingService', () => {
|
|||||||
expect(result[1].functionResponse!.id).toBe(callId);
|
expect(result[1].functionResponse!.id).toBe(callId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ENOENT (missing directory) handling', () => {
|
||||||
|
it('should ensure directory exists before writing conversation file', () => {
|
||||||
|
chatRecordingService.initialize();
|
||||||
|
|
||||||
|
const mkdirSyncSpy = vi.spyOn(fs, 'mkdirSync');
|
||||||
|
const writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync');
|
||||||
|
|
||||||
|
chatRecordingService.recordMessage({
|
||||||
|
type: 'user',
|
||||||
|
content: 'Hello after dir cleanup',
|
||||||
|
model: 'gemini-pro',
|
||||||
|
});
|
||||||
|
|
||||||
|
// mkdirSync should be called with the parent directory and recursive option
|
||||||
|
const conversationFile = chatRecordingService.getConversationFilePath()!;
|
||||||
|
expect(mkdirSyncSpy).toHaveBeenCalledWith(
|
||||||
|
path.dirname(conversationFile),
|
||||||
|
{ recursive: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
// mkdirSync should be called before writeFileSync
|
||||||
|
const mkdirCallOrder = mkdirSyncSpy.mock.invocationCallOrder;
|
||||||
|
const writeCallOrder = writeFileSyncSpy.mock.invocationCallOrder;
|
||||||
|
const lastMkdir = mkdirCallOrder[mkdirCallOrder.length - 1];
|
||||||
|
const lastWrite = writeCallOrder[writeCallOrder.length - 1];
|
||||||
|
expect(lastMkdir).toBeLessThan(lastWrite);
|
||||||
|
|
||||||
|
mkdirSyncSpy.mockRestore();
|
||||||
|
writeFileSyncSpy.mockRestore();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -455,6 +455,8 @@ export class ChatRecordingService {
|
|||||||
conversation.lastUpdated = new Date().toISOString();
|
conversation.lastUpdated = new Date().toISOString();
|
||||||
const newContent = JSON.stringify(conversation, null, 2);
|
const newContent = JSON.stringify(conversation, null, 2);
|
||||||
this.cachedLastConvData = newContent;
|
this.cachedLastConvData = newContent;
|
||||||
|
// Ensure directory exists before writing (handles cases where temp dir was cleaned)
|
||||||
|
fs.mkdirSync(path.dirname(this.conversationFile), { recursive: true });
|
||||||
fs.writeFileSync(this.conversationFile, newContent);
|
fs.writeFileSync(this.conversationFile, newContent);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user