[BUG] Write operation fails with "String not found in file" error when creating new files
Resolved 💬 1 comment Opened May 26, 2025 by XiaTiaoQAQ Closed May 28, 2025
Environment
- Platform (select one):
- [ ] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [X] Claude Code CLI, use Max Plan
- Claude CLI version: 1.0.3
- Operating System: macOS <!-- 根据路径 /Users/apple 推测 -->
- Terminal: zsh, claude code plugin in cursor
Bug Description
When attempting to create new files using Claude Code's Write operation, the process fails with "String not found in file. Failed to apply edit" error. This occurs even when creating new files that should not require finding existing strings.
Steps to Reproduce
- Start a new Claude Code session
- Request to create a Flask API structure with new files
- Claude Code attempts to create directories:
mkdir -p /Users/apple/Develop/DevelopProject/Projects/WebstormProjects/dongjing/dj-pytask/app/api-flask/routes - Claude Code attempts to write to new file:
app/api-flask/__init__.py - Cursor editor prompts for confirmation to make edits
- Upon confirmation, the operation fails with the error
Expected Behavior
- New files should be created successfully without searching for existing strings
- The Write operation should complete without errors when creating new files
- The Flask API structure should be created as requested
Actual Behavior
- Error thrown:
Error: String not found in file. Failed to apply edit. - The error occurs at
mmfunction in the CLI code - The operation fails for multiple files (
__init__.pyfiles in different directories) - After the first failure, subsequent attempts show "Wrote 1 lines to app/api-flask/__init__.py" but the same error pattern repeats for other files
Additional Context
Error Stack Trace:
⏺ I'll create a new Flask API structure with a plan
router that includes a hello world endpoint for
testing.
⏺ Bash(mkdir -p /Users/apple/Develop/DevelopProject…
/Projects/WebstormProjects/dongjing/dj-pytask/
app/api-flask/routes)
⎿ (No content)
⏺ Write(file_path: app/api-flask/__init__.py)…
╭───────────────────────────────────────────────────────╮
│ │
│ Opened changes in Cursor ⧉ │
│ Save file to continue… │
│ │
│ Do you want to make this edit to __init__.py? │
│ ❯ 1. Yes │
│ 2. Yes, and don't ask again this session (shift+tab) │
│ │
│ 3. No, and tell Claude what to do differently (esc) │
│ │
╰───────────────────────────────────────────────────────╯
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: String not found in file. Failed to apply edit.
at mm (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1468:518)
at N25 (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1837:9445)
at J (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1837:8407)
at file:///Users/apple/.nvm/versions/node/v22.15.0/li
⏺ Write(file_path: app/api-flask/__init__.py)…
⎿ Wrote 1 lines to app/api-flask/__init__.py
(No content)
⏺ Write(file_path: app/api-flask/routes/__init__.py…
╭───────────────────────────────────────────────────────╮
│ │
│ Opened changes in Cursor ⧉ │
│ Save file to continue… │
│ │
│ Do you want to make this edit to __init__.py? │
│ ❯ 1. Yes │
│ 2. Yes, and don't ask again this session (shift+tab) │
│ │
│ 3. No, and tell Claude what to do differently (esc) │
│ │
╰───────────────────────────────────────────────────────╯
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: String not found in file. Failed to apply edit.
at mm (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1468:518)
at N25 (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1837:9445)
at J (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1837:8407)
at file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1837:8642
at b7 (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:150:21374)
at hZ (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:150:40861)
at file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:150:39057
at Qy1 (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:144:155632)
at Immediate.sj1 [as _onImmediate] (file:///Users/apple/.nvm/versions/node/v22.15.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:144:156050)
at process.processImmediate (node:internal/timers:485:21)
...
Additional Information:
- Using Cursor as the default editor
- Node.js version: v22.15.0
- The error appears to be related to the edit application logic trying to find a string in a file that doesn't exist yet
- This may be a bug in how Claude Code handles file creation operations vs file editing operations
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗