Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions extensions/cli/src/tools/writeFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@ export const writeFileTool: Tool = {
readonly: false,
isBuiltIn: true,
preprocess: async (args) => {
const filepath = args?.filepath;
const content = args?.content ?? "";
if (typeof filepath !== "string") {
throw new Error("Filepath must be a string");
}
if (typeof content !== "string") {
throw new Error("New file content must be a string");
}
try {
if (fs.existsSync(args.filepath)) {
const oldContent = fs.readFileSync(args.filepath, "utf-8");
const newContent = args.content;
if (fs.existsSync(filepath)) {
const oldContent = fs.readFileSync(filepath, "utf-8");

const diff = createTwoFilesPatch(
args.filepath,
args.filepath,
oldContent,
newContent,
content,
undefined,
undefined,
{ context: 2 },
Expand All @@ -81,7 +88,7 @@ export const writeFileTool: Tool = {
} catch {
// do nothing
}
const lines: string[] = args.content.split("\n");
const lines: string[] = content.split("\n");
const previewLines = lines.slice(0, 3);

const preview: ToolCallPreview[] = [
Expand All @@ -103,7 +110,10 @@ export const writeFileTool: Tool = {
}

return {
args,
args: {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normalized filepath/content values are only returned in the new-file path, but when the file already exists this branch still returns the original args, so run receives content as undefined and fs.writeFileSync throws. Please return the sanitized args in both branches.

Prompt for AI agents
Address the following comment on extensions/cli/src/tools/writeFile.ts at line 113:

<comment>The normalized `filepath`/`content` values are only returned in the new-file path, but when the file already exists this branch still returns the original `args`, so `run` receives `content` as `undefined` and `fs.writeFileSync` throws. Please return the sanitized args in both branches.</comment>

<file context>
@@ -103,7 +110,10 @@ export const writeFileTool: Tool = {
 
     return {
-      args,
+      args: {
+        filepath,
+        content,
</file context>
Fix with Cubic

filepath,
content,
},
preview,
};
},
Expand Down
Loading