claude-code - 💡(How to fix) Fix [BUG] Write tool overwrites existing file without prior Read [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#49487Fetched 2026-04-17 08:39:46
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
labeled ×4

The Write tool is supposed to fail when attempting to write to an existing file that hasn't been Read first in the current session. Instead, it silently overwrites the file, destroying the previous content with no warning or error.

Error Message

The Write tool is supposed to fail when attempting to write to an existing file that hasn't been Read first in the current session. Instead, it silently overwrites the file, destroying the previous content with no warning or error. The Write tool succeeds, overwriting the existing file contents entirely. No error, no warning. The previous content is lost. The Write call should be rejected with an error, forcing the agent to Read the file first.

Error Messages/Logs

  1. Expected: The Write tool should error with something like "You must Read this file before overwriting it"

Root Cause

Encountered during a real session where an agent used the Write tool to create what it thought was a new file. The file actually existed with ~168 lines of content from a previous session's work (a detailed post-run report with bug findings and verification results). The entire file was overwritten with ~22 lines of new content. The previous content was not committed to git and was only recoverable because the user had the diff visible in their terminal.

Code Example



---

echo "This is important existing content that should not be lost." > /tmp/test-write-bug.md
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

Summary

The Write tool is supposed to fail when attempting to write to an existing file that hasn't been Read first in the current session. Instead, it silently overwrites the file, destroying the previous content with no warning or error.

Actual Behavior

The Write tool succeeds, overwriting the existing file contents entirely. No error, no warning. The previous content is lost.

Impact

  • Data loss: Existing file content is silently destroyed
  • No recovery: If the file isn't in git, the content is unrecoverable
  • Undermines safety model: The Read-before-Write check exists specifically to prevent blind overwrites. When it doesn't fire, the agent has no awareness of what it's destroying.

Context

Encountered during a real session where an agent used the Write tool to create what it thought was a new file. The file actually existed with ~168 lines of content from a previous session's work (a detailed post-run report with bug findings and verification results). The entire file was overwritten with ~22 lines of new content. The previous content was not committed to git and was only recoverable because the user had the diff visible in their terminal.

What Should Happen?

Expected Behavior

Per the Write tool description:

"If this is an existing file, you MUST use the Read tool first to read the file's contents. This tool will fail if you did not read the file first."

The Write call should be rejected with an error, forcing the agent to Read the file first.

Error Messages/Logs

Steps to Reproduce

Reproduction Steps

  1. In session A (or manually), create a file with meaningful content:

    echo "This is important existing content that should not be lost." > /tmp/test-write-bug.md
  2. Start a new Claude Code session (session B)

  3. In session B, ask Claude to write new content to that same file path — but do NOT read it first. For example:

    "Write 'Hello world' to /tmp/test-write-bug.md"

  4. Expected: The Write tool should error with something like "You must Read this file before overwriting it"

  5. Actual: The Write tool succeeds. The original content ("This is important existing content...") is gone, replaced with "Hello world"

Key Conditions

  • The file must exist on disk but must NOT have been Read in the current session
  • The file must not be tracked in the conversation's Read history (i.e., was created in a previous session or by another process)
  • The Write tool description says it "will fail" in this case, but it does not

Claude Model

Opus

Is this a regression?

Yes, this worked in a previous version

Last Working Version

No response

Claude Code Version

2.1.110

Platform

AWS Bedrock

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

No response

extent analysis

TL;DR

The Write tool should be modified to check if a file has been read in the current session before overwriting it, and return an error if not.

Guidance

  • Review the Write tool's implementation to ensure it checks the file's read status in the current session before writing to it.
  • Verify that the Read tool correctly updates the file's read status in the session.
  • Consider adding a test case to ensure the Write tool fails when attempting to overwrite a file that hasn't been read in the current session.
  • Check the session management code to ensure that file read status is properly reset when a new session starts.

Example

No code snippet is provided as the issue does not include specific implementation details.

Notes

The issue seems to be a regression, as it worked in a previous version. The fix should focus on restoring the original behavior of the Write tool.

Recommendation

Apply workaround: Modify the Write tool to check the file's read status and return an error if it hasn't been read in the current session, until a permanent fix is available.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [BUG] Write tool overwrites existing file without prior Read [1 participants]