From 968e09f0b50d17f7c591baa977666b991a1e59b7 Mon Sep 17 00:00:00 2001 From: Taylor Mullen Date: Thu, 15 May 2025 23:51:53 -0700 Subject: fix: Ensure filename is available for diff rendering in write-file This commit resolves a bug where the `write-file` operation could fail to render content due to a missing filename. The fix involves: - Ensuring `fileName` is consistently passed to `DiffRenderer.tsx` through `ToolConfirmationMessage.tsx`, `ToolMessage.tsx`, and `useGeminiStream.ts`. - Modifying `edit.ts` and `write-file.ts` to include `fileName` in the `FileDiff` object. - Expanding the `FileDiff` interface in `tools.ts` to include `fileName`. Additionally, this commit enhances the diff rendering by: - Adding syntax highlighting based on file extension in `DiffRenderer.tsx`. - Adding more language mappings to `getLanguageFromExtension` in `DiffRenderer.tsx`. - Added lots of tests for all the above. Fixes https://b.corp.google.com/issues/418125982 --- packages/server/src/tools/write-file.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'packages/server/src/tools/write-file.test.ts') diff --git a/packages/server/src/tools/write-file.test.ts b/packages/server/src/tools/write-file.test.ts index 2ce5cdf5..25d1e998 100644 --- a/packages/server/src/tools/write-file.test.ts +++ b/packages/server/src/tools/write-file.test.ts @@ -6,7 +6,7 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { WriteFileTool } from './write-file.js'; -import { ToolConfirmationOutcome } from './tools.js'; +import { FileDiff, ToolConfirmationOutcome } from './tools.js'; import path from 'path'; import fs from 'fs'; import os from 'os'; @@ -152,7 +152,8 @@ describe('WriteFileTool', () => { ); expect(fs.existsSync(filePath)).toBe(true); expect(fs.readFileSync(filePath, 'utf8')).toBe(content); - const display = result.returnDisplay as { fileDiff: string }; // Type assertion + const display = result.returnDisplay as FileDiff; // Type assertion + expect(display.fileName).toBe('execute_new_file.txt'); // For new files, the diff will include the filename in the "Original" header expect(display.fileDiff).toMatch(/--- execute_new_file.txt\tOriginal/); expect(display.fileDiff).toMatch(/\+\+\+ execute_new_file.txt\tWritten/); @@ -176,7 +177,8 @@ describe('WriteFileTool', () => { expect(result.llmContent).toMatch(/Successfully overwrote file/); expect(fs.readFileSync(filePath, 'utf8')).toBe(newContent); - const display = result.returnDisplay as { fileDiff: string }; // Type assertion + const display = result.returnDisplay as FileDiff; // Type assertion + expect(display.fileName).toBe('execute_existing_file.txt'); expect(display.fileDiff).toMatch(initialContent); expect(display.fileDiff).toMatch(newContent); }); -- cgit v1.2.3