Skip to content

Commit 77a1ef2

Browse files
committed
fix(opencode): accept absolute Windows paths in apply_patch
1 parent a811463 commit 77a1ef2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/opencode/src/tool/apply_patch.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
5757

5858
let totalDiff = ""
5959

60+
const resolvePatchPath = (input: string) => {
61+
const normalized = path.toPosix(input)
62+
if (path.isAbsolute(normalized)) return normalized
63+
return path.resolve(Instance.directory, normalized)
64+
}
65+
6066
for (const hunk of hunks) {
61-
const filePath = path.resolve(Instance.directory, hunk.path)
67+
const filePath = resolvePatchPath(hunk.path)
6268
await assertExternalDirectory(ctx, filePath)
6369

6470
switch (hunk.type) {
@@ -116,7 +122,7 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
116122
if (change.removed) deletions += change.count || 0
117123
}
118124

119-
const movePath = hunk.move_path ? path.resolve(Instance.directory, hunk.move_path) : undefined
125+
const movePath = hunk.move_path ? resolvePatchPath(hunk.move_path) : undefined
120126
await assertExternalDirectory(ctx, movePath)
121127

122128
fileChanges.push({

packages/opencode/test/tool/apply_patch.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,26 @@ describe("tool.apply_patch freeform", () => {
259259
})
260260
})
261261

262+
test("accepts absolute Windows paths with backslashes", async () => {
263+
if (process.platform !== "win32") return
264+
265+
await using fixture = await tmpdir()
266+
const { ctx } = makeCtx()
267+
268+
await Instance.provide({
269+
directory: fixture.path,
270+
fn: async () => {
271+
const target = path.join(fixture.path, "abs.txt")
272+
await fs.writeFile(target, "before\n", "utf-8")
273+
274+
const patchText = `*** Begin Patch\n*** Update File: ${target}\n@@\n-before\n+after\n*** End Patch`
275+
await execute({ patchText }, ctx)
276+
277+
expect(await fs.readFile(target, "utf-8")).toBe("after\n")
278+
},
279+
})
280+
})
281+
262282
test("adds file overwriting existing file", async () => {
263283
await using fixture = await tmpdir()
264284
const { ctx } = makeCtx()

0 commit comments

Comments
 (0)