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

Edit Tool Fails with 'File Unexpectedly Modified' on Windows/Git Bash

Open vimholic opened this issue 2 months ago • 5 comments

Edit Tool Fails with "File Unexpectedly Modified" Error on Windows/Git Bash

Description

The Edit tool consistently fails with the error "File has been unexpectedly modified. Read it again before attempting to write it." even when:

  • The file content has not changed
  • No other process is modifying the file
  • Edit is called immediately after Read with no intermediate operations

This makes the Edit tool essentially unusable on Windows with Git Bash, forcing users to use bash commands (cat, sed, etc.) instead.

Environment

  • OS: Windows 11
  • Shell: Git Bash (MSYS2)
  • Claude Code Version: Latest (as of 2025-11-15)
  • Terminal: PowerShell / Windows Terminal

Steps to Reproduce

# 1. Create a test file
cat > ~/test.txt << 'EOF'
Line 1
Line 2
EOF

# 2. Read the file
Read: ~/test.txt

# 3. Immediately attempt to edit (no other operations)
Edit: ~/test.txt
  old_string: "Line 2"
  new_string: "Line 2 Modified"

# Result: ❌ "File has been unexpectedly modified" error

Expected Behavior

The Edit tool should successfully modify the file since:

  • No actual modifications occurred between Read and Edit
  • File content is unchanged
  • Only Claude Code has accessed the file

Actual Behavior

Edit tool fails with timestamp mismatch error.

Root Cause Analysis

After investigation, we found:

  1. Claude Code executes each Bash command as a login shell (bash -l)
  2. This causes .bash_profile to be sourced on every Bash tool invocation
  3. File system access during profile loading updates file timestamps
  4. Edit tool's timestamp check detects this change and rejects the edit

Evidence:

# Each Bash command shows this (from .bash_profile):
✓ UTF-8 encoding functions loaded: win, pwsh

# Shell flags confirm login shell:
$-: hmtBc  (contains 'l' characteristics)

Impact

  • High: Edit tool is completely broken for Windows/Git Bash users
  • Affects all files, not just shell configuration files
  • Forces workarounds using bash commands instead of built-in tools
  • Reduces productivity and developer experience

Suggested Solutions

Option 1: Relax Timestamp Checking (Recommended)

Add a grace period (1-2 seconds) or check content hash instead of just timestamps.

Option 2: Use Content-Based Verification

Compare file content hash instead of relying solely on timestamps.

Option 3: Document Behavior

If this is intentional, document that Edit tool requires no intermediate operations between Read and Edit.

Option 4: Optimize Bash Invocation

Avoid using login shell (bash -l) for Bash tool commands, or provide an option to configure shell invocation.

Current Workaround

Users must use bash commands instead of Edit tool:

# Instead of Edit tool, use:
sed -i 's/old_string/new_string/' file.txt
# or
cat > file.txt << 'EOF'
new content
EOF

Additional Context

  • Windows has DisableLastAccess = 1 (last access time updates disabled)
  • Issue occurs even with minimal .bash_profile
  • Problem persists across different file types (source code, config files, etc.)

Test Case

A reproducible test case is available in the bug report file created during investigation.

Related Issues

This may be related to how Claude Code handles file system operations on Windows, particularly with MSYS2/Git Bash environments.


Priority: High
Component: Edit Tool
Platform: Windows + Git Bash

vimholic avatar Nov 15 '25 13:11 vimholic

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/10882
  2. https://github.com/anthropics/claude-code/issues/10437
  3. https://github.com/anthropics/claude-code/issues/10498

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

github-actions[bot] avatar Nov 15 '25 13:11 github-actions[bot]

This is NOT a duplicate issue

While the symptoms are similar to the referenced issues, this issue identifies a specific root cause and provides concrete solutions that the other issues do not address.

Key differences from duplicates:

#10882, #10437 - General "File has been unexpectedly modified" errors

  • Report the symptom without identifying the cause
  • No specific platform/environment details

#10498 - IDE opening files (macOS)

  • Different platform (macOS vs Windows)
  • Different cause (IDE file access vs shell initialization)

This issue (#11684) - Git Bash login shell root cause

  • Root cause identified: bash -l invocation sources .bash_profile on every command
  • Specific reproduction: Git Bash on Windows with any shell profile
  • Concrete solutions proposed:
    • Avoid login shell mode (bash instead of bash -l)
    • Grace period for timestamp validation
    • Content-hash verification instead of timestamp
  • Platform-specific: Git Bash behavior on Windows

This issue provides actionable information for fixing the underlying problem, not just reporting symptoms.

Label request:

Please add the following labels to properly categorize this issue:

  • bug
  • platform:windows
  • area:tools
  • area:bash
  • has repro

Thank you for reviewing!

vimholic avatar Nov 17 '25 04:11 vimholic

Option 4 (Optimize Bash Invocation) will also fix https://github.com/anthropics/claude-code/issues/11867

bnordli avatar Nov 18 '25 14:11 bnordli

✅ Fix Confirmed: CLAUDE_BASH_NO_LOGIN works perfectly

I tested the fix proposed by @wolffiex in #11867 and can confirm it completely resolves the issue.

Setup:

Added to ~/.claude/settings.local.json:

{
  "env": {
    "CLAUDE_BASH_NO_LOGIN": "1"
  }
}

Test Results:

Before the fix:

  • ❌ Edit tool consistently failed with "File has been unexpectedly modified"
  • ❌ Failed even when using Python to modify JSON (had to work around the bug)

After setting CLAUDE_BASH_NO_LOGIN=1:

  • ✅ Multiple consecutive Read → Edit operations succeeded
  • ✅ No "File has been unexpectedly modified" errors
  • ✅ Edit tool works reliably on all file types

Root Cause Validation:

The fix confirms the root cause I identified:

  • bash -l sources .bash_profile on every invocation
  • Profile execution touches filesystem, modifying timestamps
  • Timestamp validation in Edit tool correctly rejects the "modified" file

Recommendation:

This environment variable should be:

  1. Documented in official docs as a workaround
  2. Considered as default behavior - login shell is rarely needed for tool execution
  3. Or implement grace period/content-hash validation as originally suggested

Thank you @wolffiex for the quick fix!

vimholic avatar Nov 21 '25 01:11 vimholic

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 21 '25 10:12 github-actions[bot]