Edit tool crashes with 'undefined is not an object (evaluating T.replaceAll)'
Description
The Edit tool intermittently crashes with a JavaScript runtime error when attempting to edit files. The error occurs before any file modification takes place.
Error Message
Error: undefined is not an object (evaluating 'T.replaceAll')
Reproduction Steps
- Read a bash script file (e.g., a hook script with ~250 lines)
- Attempt to use Edit tool to replace a multi-line block
- Error occurs consistently on retry attempts
Example That Triggered the Bug
File: .claude/hooks/skill/duplicate-code-detector.sh
Attempted edit (old_string):
# Search for this name in other files
DUPLICATES=$(echo "$CODE_FILES" | xargs grep -l "\b$NAME\b" 2>/dev/null | head -5 || true)
Attempted edit (new_string):
# Search for this name in other files
# SEC-FIX: Use xargs_grep_word for safe word-boundary matching
DUPLICATES=$(echo "$CODE_FILES" | xargs_grep_word -l "$NAME" | head -5 || true)
The Edit tool failed 3 consecutive times with the same error on different edit attempts.
Workaround
Using the Write tool to replace the entire file content works successfully. The issue is specifically in the Edit tool's replaceAll code path.
Environment
- OS: macOS (Darwin 25.2.0)
- Claude Code: Latest version
- Model: claude-opus-4-5-20251101
Analysis
The error undefined is not an object (evaluating 'T.replaceAll') indicates that in the Edit tool's JavaScript implementation, a variable T (likely holding file content or a match result) is undefined when .replaceAll() is called on it.
Possible causes:
- Special characters in the search string (regex metacharacters like
\b,$, etc.) - Multi-line content handling edge case
- File content encoding mismatch between Read and Edit operations
Expected Behavior
The Edit tool should either:
- Successfully perform the replacement, or
- Return a meaningful error message if the old_string is not found
Actual Behavior
JavaScript runtime crash with no actionable information for the user.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗