claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[Bug] File Edit Fails on Windows with Unexpected Modification Error

Open kohowski opened this issue 1 year ago • 24 comments

Bug Description all file edits are broken on Windows in Claude Code since v1.0.111 (.110 is the one I have to downgrade to). The error the tool throws every times is:

Update(....) ⎿  Error: File has been unexpectedly modified. Read it again before attempting to write it.

... endless repeat before the model decides to use sed or other workarounds.

no, this is not about mtime. Touching files does not change a thing.

https://github.com/anthropics/claude-code/issues/7443 see how many people have the same issue (ignore the fact the OP decided to close it)

Environment Info

  • Platform: win32
  • Terminal: windows-terminal
  • Version: 1.0.120
  • Feedback ID: bce32ccb-ad12-4bf8-b0af-87a694b18f18

Errors

[{"error":"Error: File has been unexpectedly modified. Read it again before attempting to write it.\n    at Object.call (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:1902:786)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async VJ5 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:21057)\n    at async WNB (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:17339)\n    at async XJ5 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:16202)\n    at async WJ5 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:15739)\n    at async lO (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:12754)\n    at async lO (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:13667)\n    at async _3 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3421:38227)\n    at async iX (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3421:39182)","timestamp":"2025-09-20T01:04:33.375Z"},{"error":"Error: File has been unexpectedly modified. Read it again before attempting to write it.\n    at Object.call (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:1902:786)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async VJ5 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:21057)\n    at async WNB (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:17339)\n    at async XJ5 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:16202)\n    at async WJ5 (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:15739)\n    at async lO (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:12754)\n    at async lO (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:13667)\n    at async lO (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:13667)\n    at async lO (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:3350:13667)","timestamp":"2025-09-20T01:04:57.233Z"},{"error":"Error: Request was aborted.\n    at y51._createMessage (file:///C:/Users/mpotanin/AppData/Roaming/npm/node_modules/@anthropic-ai/claude-code/cli.js:1000:7955)\n    at processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at runNextTicks (node:internal/process/task_queues:69:3)\n    at listOnTimeout (node:internal/timers:569:9)\n    at process.processTimers (node:internal/timers:543:7)","timestamp":"2025-09-20T01:05:06.388Z"}]

kohowski avatar Sep 20 '25 01:09 kohowski

#7935

JosiahSiegel avatar Sep 20 '25 14:09 JosiahSiegel

It can temporarily be fixed by entering this: "There's a file modification bug in Claude Code. The workaround is: always use complete absolute Windows paths with drive letters and backslashes for ALL file operations. Apply this rule going forward, not just for this file."

SShadowS avatar Sep 20 '25 21:09 SShadowS

I place this workaround in CLAUDE.md and it has worked so far:

## CRITICAL: File Editing on Windows

### ⚠️ MANDATORY: Always Use Backslashes on Windows for File Paths

**When using Edit or MultiEdit tools on Windows, you MUST use backslashes (`\`) in file paths, NOT forward slashes (`/`).**

#### ❌ WRONG - Will cause errors:
```
Edit(file_path: "D:/repos/project/file.tsx", ...)
MultiEdit(file_path: "D:/repos/project/file.tsx", ...)
```

#### ✅ CORRECT - Always works:
```
Edit(file_path: "D:\repos\project\file.tsx", ...)
MultiEdit(file_path: "D:\repos\project\file.tsx", ...)
```

JosiahSiegel avatar Sep 26 '25 11:09 JosiahSiegel

In my testing the bug appeared on 1.0.111 and 1.0.110 . The following changelog sounds like whatever this shell fix was is the cause of the windows File editing issues. The posted workaround does not even work for me and shouldn't be necessary.

## 1.0.111

- /model now validates provided model names
- Fixed Bash tool crashes caused by malformed shell syntax parsing

https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md?plain=1#L89-L92

spAnser avatar Sep 30 '25 12:09 spAnser

@JosiahSiegel are we sure that is actually the cause of this? Sometimes backslashes work. I am wondering if it might have to do with some other process touching the files, like shell snapshots or something.

I have also experienced Read() failing too.

I am on V2.0.22. it seems very random, some sessions have no issues, while others become completely useless. Restarting Claude doesn't always work. Sometimes I have to restart my whole machine.

BenNewman100 avatar Oct 20 '25 16:10 BenNewman100

It is mostly an issue when using Git Bash as shell (default), then it tries to use a mix of Windows and Linux semantics in all kinds of commands first. When they fail, it tries again with bash format and all is fine again. Ofte have commands with 2>nul, which creates an empty nul file which Windows has trouble deleting and cause all kinds of issues. So think it is an issue with this mixed Windows env and Bash which causes strange outcomes.

SShadowS avatar Oct 20 '25 16:10 SShadowS

@ant-kurt @wolffiex

I've been testing Windows path issues (see #7935) and wondering if this is the same bug or separate.

#7935 (Cache Bug):

  • Root cause: C:/path vs C:\path treated as different cache keys
  • Trigger: Reading with forward slashes poisons cache
  • Workaround: Always use backslash paths for Read

#7918 (This Issue):

  • Same symptom: "File has been unexpectedly modified"
  • Same workaround: Use backslash paths

BenNewman100 avatar Oct 23 '25 19:10 BenNewman100

In case anyone is looking for a workaround (there have been posts for this before, but they haven't worked reliably for me): I also gave examples about correct / incorrect usage in my CLAUDE.md, but I used the tool_use message content format, in the hopes that this would more reliably be followed (under the assumption that it is closer to the actual tool call produced by the model). So far it has worked well for me:

#### ⚠️ CRITICAL Bug Workaround

CRITICAL: Follow these guidelines when using tools operating on file paths EXCEPT `Bash` in any skill or agent. Otherwise, they will not work correctly.

- When referencing file paths in tool calls, ALWAYS use the Windows-style paths with backslashes (`\`), as we're running on Windows.
- When referencing paths to the home directory, ALWAYS EXPAND the tilde symbol (`~`) representing the home directory to the full path, e.g., `C:\Users\some.user`.

#### Applies To

- `Edit`
- `Glob`
- `Grep`
- `Read`

#### Examples (CRITICAL Bug Workaround)

##### ✅ Correct (File Paths)

```json
{
    "type": "tool_use",
    "id": "...",
    "name": "Read",
    "input": {
        "file_path": "C:\\path\\to\\some\\file.txt"
    }
}
```

```json
{
    "type": "tool_use",
    "id": "...",
    "name": "Edit",
    "input": {
        "file_path": "C:\\path\\to\\some\\file.txt",
        "old_string": "...",
        "new_string": "..."
    }
}
```

##### ❌ Incorrect (File Paths)

```json
{
    "type": "tool_use",
    "id": "...",
    "name": "Read",
    "input": {
        "file_path": "/c/path/to/some/file.txt"
    }
}
```

... (rest omitted)

Dennis-Renz avatar Nov 12 '25 12:11 Dennis-Renz

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Dec 13 '25 10:12 github-actions[bot]

The issue is definitely still happening.

Aureliaar avatar Dec 16 '25 05:12 Aureliaar

I confirm too, still happening, last version of CC.

bucheronbanlieusard avatar Dec 16 '25 16:12 bucheronbanlieusard

It's happening to me as well (oddly, it just started happening to me fairly recently, within the last week or two).

I just tried the workaround and it seems to be successful for me, at least for now.

GoluxHat avatar Dec 16 '25 16:12 GoluxHat

Issue is still happening. Typical example of second class Windows support.

DonRedwoood avatar Dec 17 '25 17:12 DonRedwoood

i get Error: File has been unexpectedly modified. Read it again before attempting to write it. on each new file. Even of it has accept to write it cant do it IN ONE SESSION. my ass is going to explode. it can update some file and then some how on my new message it cant write anymore. WTF ????

K4leri avatar Dec 23 '25 16:12 K4leri

i get Error: File has been unexpectedly modified. Read it again before attempting to write it. on each new file. Even of it has accept to write it cant do it IN ONE SESSION. my ass is going to explode. it can update some file and then some how on my new message it cant write anymore. WTF ????

Just press Esc and type: Use full windows paths for all tool calls.

SShadowS avatar Dec 23 '25 16:12 SShadowS

i get Error: File has been unexpectedly modified. Read it again before attempting to write it. on each new file. Even of it has accept to write it cant do it IN ONE SESSION. my ass is going to explode. it can update some file and then some how on my new message it cant write anymore. WTF ????

Just press Esc and type: Use full windows paths for all tool calls.

Image

this is full paths, no ?

K4leri avatar Dec 23 '25 20:12 K4leri

As described above the fix (PATCH) for now is to add that prompt when that happen:

use absolute path to modify files

This patch the issue.

bucheronbanlieusard avatar Dec 23 '25 20:12 bucheronbanlieusard

i get Error: File has been unexpectedly modified. Read it again before attempting to write it. on each new file. Even of it has accept to write it cant do it IN ONE SESSION. my ass is going to explode. it can update some file and then some how on my new message it cant write anymore. WTF ????

Just press Esc and type: Use full windows paths for all tool calls.

Image this is full paths, no ?

Full Windows paths. It's using unix-style forward slashes instead of Windows backslashes. It's probably translating them to the correct Windows style when it actually makes the edit, but whatever mechanism is being used to detect changes probably isn't, which is breaking it.

kabili207 avatar Dec 24 '25 03:12 kabili207

STILL happening - fresh install (new user here) - instantly hit this issue on Windoze 11. This comment unblocked the following SHOWSTOPPER priority error:

Error: File has been unexpectedly modified. Read it again before attempting to write it.

Comment (in this thread) handed to Opus 4.5 as instructions:

https://github.com/anthropics/claude-code/issues/7918#issuecomment-3315275618

It can temporarily be fixed by entering this: (Hand this sentence to Claude / Opus 4.5):

"There's a file modification bug in Claude Code. The workaround is: always use complete absolute Windows paths with drive letters and backslashes for ALL file operations. Apply this rule going forward, not just for this file."

jimandreas avatar Jan 01 '26 11:01 jimandreas

This is still occurring in Claude 2.1.4

interestingcodelol avatar Jan 11 '26 06:01 interestingcodelol