Skip to content

patch: case-only rename deletes the file on case-insensitive filesystems #63126

Description

@kambleakash0

Description

On a case-insensitive filesystem (the macOS and Windows defaults), a patch that only changes the case of a file name deletes the file.

packages/core/src/tool/plugin/patch.ts:185 treats the hunk as a move because same.txt and Same.txt are different strings. That check comes from #59197, which fixed moves onto the exact same path (./same.txt), but it doesn't catch a case change. The apply step then writes Same.txt, which overwrites same.txt in place, and removes same.txt, which is the same file (patch.ts:233-239). The tool then fails with:

Failed to read <dir>/Same.txt: file does not exist. Completed before failure: Same.txt

On a case-sensitive filesystem the same patch renames the file correctly.

The v1 branch has the same write-then-remove in apply_patch.ts and no guard even for the exact same path (#45310 was closed, but its fix #45316 was not merged).

Plugins

None

OpenCode version

2.2.7 (dev at edfe651)

Steps to reproduce

  1. On a case-insensitive filesystem, create same.txt containing before.
  2. Run the patch tool with:
    *** Begin Patch
    *** Update File: same.txt
    *** Move to: Same.txt
    @@
    -before
    +after
    *** End Patch
    
  3. The directory is now empty and the tool returns the error above. Expected: Same.txt containing after.

I reproduced it with a test added to packages/core/test/tool-patch.test.ts (using withTempTool and executeTool), run with TMPDIR on a vfat mount. The same test passes on ext4.

Screenshot

No response

Operating System

macOS and Windows (any case-insensitive volume); reproduced on Linux with a vfat mount

Terminal

No response

Activity

  1. opencodejr commented on Nov 10, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced this on 2.2.7, and it still happens on the latest dev (8f0bf22d6).

    Setup: a case-insensitive vfat mount on Linux with same.txt containing before. A simulated model calls the patch tool with *** Update File: same.txt / *** Move to: Same.txt and changes -before to +after. The tool fails with Failed to read .../Same.txt: file does not exist. Completed before failure: .../Same.txt, and the directory is left empty. As you describe, the move is detected by a plain string comparison of the paths, so writing Same.txt and then removing same.txt deletes the only file.

    The recording and screenshot below show it on 2.2.7 and on dev, and the script below reproduces it (it needs a case-insensitive mount at /mnt/fat).

    Related: #59197 (the earlier fix for moves onto the exact same path) and #45310 (the same-path problem on v1).

    recording.mp4

    Screenshot

    Reproduction script (drive.ts)
    // Reproduces anomalyco/opencode#63126: a case-only rename via the patch tool
    // deletes the file on a case-insensitive filesystem.
    //
    // Prerequisite (as root): a vfat (case-insensitive) mount at /mnt/fat:
    //   truncate -s 64M /tmp/fat.img && mkfs.vfat /tmp/fat.img
    //   mkdir -p /mnt/fat && mount -o loop /tmp/fat.img /mnt/fat
    // Run: OPENCODE_DRIVE_MEDIA_DIR=/workspace/media opencode-drive run drive.ts
    //   (set OC_DEV=/path/to/opencode-checkout to run from source)
    import { Effect, Stream } from "effect"
    import { mkdirSync, readdirSync, rmSync, writeFileSync, readFileSync } from "node:fs"
    import { Llm, OpenCodeDriver } from "opencode-drive"
    
    const dir = "/mnt/fat/case-repro"
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        config: { permission: "allow" },
        tui: { recording: true },
      },
      ({ ui, llm }) =>
        Effect.gen(function* () {
          rmSync(dir, { recursive: true, force: true })
          mkdirSync(dir, { recursive: true })
          writeFileSync(`${dir}/same.txt`, "before\n")
          console.log("BEFORE:", readdirSync(dir))
    
          const patchText = [
            "*** Begin Patch",
            `*** Update File: ${dir}/same.txt`,
            `*** Move to: ${dir}/Same.txt`,
            "@@",
            "-before",
            "+after",
            "*** End Patch",
          ].join("\n")
    
          // Title requests go to the same simulated model; answer them separately.
          let main = 0
          yield* llm.serve((request) => {
            if (JSON.stringify(request).includes("You name conversations")) return Stream.make(Llm.text("Case rename"))
            main++
            if (main === 1)
              return Stream.make(
                Llm.toolCall({ index: 0, id: "call_patch", name: "patch", input: { patchText } }),
                Llm.finish("tool-calls"),
              )
            return Stream.make(Llm.text("Patch step finished."))
          })
          yield* ui.submit(`Rename ${dir}/same.txt to Same.txt and change before to after`)
          yield* ui.waitFor("Patch step finished.", { timeout: 25000 })
          yield* Effect.sleep(1500)
          yield* ui.screenshot("after-patch")
    
          const files = readdirSync(dir)
          console.log("AFTER:", files)
          for (const f of files) console.log(`${f}:`, JSON.stringify(readFileSync(`${dir}/${f}`, "utf8")))
          console.log(files.length === 0 ? "BUG REPRODUCED: file was deleted" : "file survived")
        }),
    )

    Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.

  2. KrBeTect2025 commented on Nov 10, 2026

    @KrBeTect2025

    I'd like to work on this. My plan is to treat a move whose new path differs from the old one only in letter case as a rename: rename the file first, then write the updated content, instead of writing the new name and removing the old one. I'll add a regression test to tool-patch.test.ts that fails on a case-insensitive filesystem without the fix. Could you assign it to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions