[BUG] PowerShell file write after non-terminating exception wiped source file (data loss)
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Claude used PowerShell to edit a C source file. A List<string>.AddRange() call failed with a type mismatch exception.
PowerShell treated this as a non-terminating error and continued executing. The subsequent
[System.IO.File]::WriteAllLines() call ran with an empty list, overwriting the file with 3 bytes and permanently
destroying its contents.
The project had no git history (Is a git repository: false). The file was only recoverable because the user happened
to have a manual backup.
Source file (SWG_ServerInterface.c, ~2731 lines) was reduced to 3 bytes. No warning was shown to the user before the destructive write occurred.
---
Environment
- OS: Windows 11 Pro
- Shell: PowerShell 7+
- Git repository: No
- Claude Code version:2.1.173
---
Severity
High — permanent data loss with no recovery path if user has no external backup. The failure mode is silent: the
script reports apparent success while the file is already destroyed.
What Should Happen?
Claude should either:
- Use the built-in Write/Edit tools for file modifications instead of PowerShell, or
- Validate that the output list is non-empty and matches expected line count before writing, or
- Treat any exception during file construction as fatal and abort before the write step
Error Messages/Logs
Steps to Reproduce
- Open a project with no git repository
- Claude attempts to edit a file using PowerShell with ReadAllLines → modify → WriteAllLines
- An exception occurs mid-script (e.g. type mismatch on List<T>.AddRange)
- PowerShell continues past the exception
- WriteAllLines executes with an empty or partial result
- File is overwritten and original content is lost
Claude Model
Sonnet (default)
Is this a regression?
No, this never worked
Last Working Version
_No response_
Claude Code Version
2.1.173
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
<img width="1110" height="678" alt="Image" src="https://github.com/user-attachments/assets/87e5e355-9c42-47c3-ae35-92a6d24272ea" />
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗