Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion .changeset/fix-cli-file-duplication.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
"@kilocode/agent-runtime": patch
"kilo-code": patch
---

Expand Down
7 changes: 5 additions & 2 deletions packages/agent-runtime/src/host/VSCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,10 @@ export class WorkspaceAPI {
// File doesn't exist, start with empty content
}

const originalLines = content.split("\n")
// Normalize line endings to LF for consistent offset calculation
// This handles Windows CRLF files correctly
const normalizedContent = content.replace(/\r\n/g, "\n")
const originalLines = normalizedContent.split("\n")
const getOffset = (line: number, character: number): number => {
let offset = 0
for (let i = 0; i < line && i < originalLines.length; i++) {
Expand All @@ -1522,7 +1525,7 @@ export class WorkspaceAPI {
return b.range.start.character - a.range.start.character
})

let updatedContent = content
let updatedContent = normalizedContent
for (const textEdit of sortedEdits) {
const startOffset = getOffset(textEdit.range.start.line, textEdit.range.start.character)
const endOffset = getOffset(textEdit.range.end.line, textEdit.range.end.character)
Expand Down
34 changes: 34 additions & 0 deletions packages/agent-runtime/src/host/__tests__/VSCode.applyEdit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,38 @@ describe("WorkspaceAPI.applyEdit", () => {
const finalContent = fs.readFileSync(filePath, "utf-8")
expect(finalContent).toBe("one\ntwo\nbeta\n")
})

it("handles Windows CRLF line endings correctly", async () => {
const vscode = createVSCodeAPIMock(tempDir, tempDir)
const edit = new WorkspaceEdit()
const uri = Uri.file(filePath)

// Write file with Windows CRLF line endings
fs.writeFileSync(filePath, "alpha\r\nbeta\r\ngamma\r\n", "utf-8")
// Replace "beta" on line 1
edit.replace(uri, new Range(new Position(1, 0), new Position(1, 4)), "REPLACED")

await vscode.workspace.applyEdit(edit)

const finalContent = fs.readFileSync(filePath, "utf-8")
// Content is normalized to LF
expect(finalContent).toBe("alpha\nREPLACED\ngamma\n")
})

it("handles mixed line endings correctly", async () => {
const vscode = createVSCodeAPIMock(tempDir, tempDir)
const edit = new WorkspaceEdit()
const uri = Uri.file(filePath)

// Write file with mixed line endings (CRLF and LF)
fs.writeFileSync(filePath, "line1\r\nline2\nline3\r\n", "utf-8")
// Replace "line2" on line 1
edit.replace(uri, new Range(new Position(1, 0), new Position(1, 5)), "MODIFIED")

await vscode.workspace.applyEdit(edit)

const finalContent = fs.readFileSync(filePath, "utf-8")
// Content is normalized to LF
expect(finalContent).toBe("line1\nMODIFIED\nline3\n")
})
})
5 changes: 4 additions & 1 deletion src/integrations/editor/DiffViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,14 @@ export class DiffViewProvider {
// In CLI mode, avoid multiple applyEdit calls that can duplicate content in the mock workspace
// (VS Code applies WorkspaceEdit in-memory; the CLI mock writes to disk, so multiple passes risk duplication)
if (process.env.KILO_CLI_MODE === "true") {
// Detect original EOL style to preserve it
const originalEOL = this.originalContent?.includes("\r\n") ? "\r\n" : "\n"

// Preserve empty last line if original content had one.
const hasEmptyLastLine = this.originalContent?.endsWith("\n")

if (hasEmptyLastLine && !accumulatedContent.endsWith("\n")) {
accumulatedContent += "\n"
accumulatedContent += originalEOL
}

const finalEdit = new vscode.WorkspaceEdit()
Expand Down
20 changes: 19 additions & 1 deletion src/integrations/editor/__tests__/DiffViewProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,32 @@ describe("DiffViewProvider", () => {
process.env.KILO_CLI_MODE = "true"
mockWorkspaceEdit.delete.mockClear()
vi.mocked(vscode.workspace.applyEdit).mockClear()

;(diffViewProvider as any).originalContent = "old\ncontent\n"
await diffViewProvider.update("new\ncontent\n", true)

// In CLI mode, finalization should skip the delete edit path
expect(mockWorkspaceEdit.delete).not.toHaveBeenCalled()
expect(vscode.workspace.applyEdit).toHaveBeenCalledTimes(2)
})

it("should preserve CRLF line endings when finalizing in CLI mode", async () => {
process.env.KILO_CLI_MODE = "true"
mockWorkspaceEdit.delete.mockClear()
mockWorkspaceEdit.replace.mockClear()
vi.mocked(vscode.workspace.applyEdit).mockClear()

// Original content has CRLF line endings
;(diffViewProvider as any).originalContent = "old\r\ncontent\r\n"
// New content doesn't have trailing newline
await diffViewProvider.update("new\r\ncontent", true)

// In CLI mode, should preserve CRLF when adding trailing newline
expect(mockWorkspaceEdit.replace).toHaveBeenCalled()
const replaceCall = mockWorkspaceEdit.replace.mock.calls.find(
(call: [unknown, unknown, string]) => call[2] && call[2].endsWith("\r\n"),
)
expect(replaceCall).toBeDefined()
})
})
})
})