Skip to content

Commit 8f58737

Browse files
authored
Fix #4113: Move relPath & newContent checks in writeToFileTool earlie… (#4378)
Fix #4113: Move relPath & newContent checks in writeToFileTool earlier and run after they exist or block is non-partial Add tests covering the issue.
1 parent 91a477d commit 8f58737

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/core/tools/__tests__/writeToFileTool.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,33 @@ describe("writeToFileTool", () => {
399399
expect(mockCline.diffViewProvider.reset).toHaveBeenCalled()
400400
})
401401
})
402+
403+
describe("parameter validation", () => {
404+
it("errors and resets on missing path parameter", async () => {
405+
await executeWriteFileTool({ path: undefined })
406+
407+
expect(mockCline.consecutiveMistakeCount).toBe(1)
408+
expect(mockCline.recordToolError).toHaveBeenCalledWith("write_to_file")
409+
expect(mockCline.sayAndCreateMissingParamError).toHaveBeenCalledWith("write_to_file", "path")
410+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalled()
411+
})
412+
413+
it("errors and resets on empty path parameter", async () => {
414+
await executeWriteFileTool({ path: "" })
415+
416+
expect(mockCline.consecutiveMistakeCount).toBe(1)
417+
expect(mockCline.recordToolError).toHaveBeenCalledWith("write_to_file")
418+
expect(mockCline.sayAndCreateMissingParamError).toHaveBeenCalledWith("write_to_file", "path")
419+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalled()
420+
})
421+
422+
it("errors and resets on missing content parameter", async () => {
423+
await executeWriteFileTool({ content: undefined })
424+
425+
expect(mockCline.consecutiveMistakeCount).toBe(1)
426+
expect(mockCline.recordToolError).toHaveBeenCalledWith("write_to_file")
427+
expect(mockCline.sayAndCreateMissingParamError).toHaveBeenCalledWith("write_to_file", "content")
428+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalled()
429+
})
430+
})
402431
})

src/core/tools/writeToFileTool.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,28 @@ export async function writeToFileTool(
2626
let newContent: string | undefined = block.params.content
2727
let predictedLineCount: number | undefined = parseInt(block.params.line_count ?? "0")
2828

29-
if (!relPath || newContent === undefined) {
29+
if (block.partial && (!relPath || newContent === undefined)) {
3030
// checking for newContent ensure relPath is complete
3131
// wait so we can determine if it's a new file or editing an existing file
3232
return
3333
}
3434

35+
if (!relPath) {
36+
cline.consecutiveMistakeCount++
37+
cline.recordToolError("write_to_file")
38+
pushToolResult(await cline.sayAndCreateMissingParamError("write_to_file", "path"))
39+
await cline.diffViewProvider.reset()
40+
return
41+
}
42+
43+
if (newContent === undefined) {
44+
cline.consecutiveMistakeCount++
45+
cline.recordToolError("write_to_file")
46+
pushToolResult(await cline.sayAndCreateMissingParamError("write_to_file", "content"))
47+
await cline.diffViewProvider.reset()
48+
return
49+
}
50+
3551
const accessAllowed = cline.rooIgnoreController?.validateAccess(relPath)
3652

3753
if (!accessAllowed) {
@@ -96,22 +112,6 @@ export async function writeToFileTool(
96112

97113
return
98114
} else {
99-
if (!relPath) {
100-
cline.consecutiveMistakeCount++
101-
cline.recordToolError("write_to_file")
102-
pushToolResult(await cline.sayAndCreateMissingParamError("write_to_file", "path"))
103-
await cline.diffViewProvider.reset()
104-
return
105-
}
106-
107-
if (newContent === undefined) {
108-
cline.consecutiveMistakeCount++
109-
cline.recordToolError("write_to_file")
110-
pushToolResult(await cline.sayAndCreateMissingParamError("write_to_file", "content"))
111-
await cline.diffViewProvider.reset()
112-
return
113-
}
114-
115115
if (predictedLineCount === undefined) {
116116
cline.consecutiveMistakeCount++
117117
cline.recordToolError("write_to_file")

0 commit comments

Comments
 (0)