claude-code
claude-code copied to clipboard
[BUG] Trailing whitespace and no newline at end of file
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other:
- Claude CLI version:
1.0.35 (Claude Code) - Operating System:
macOS 15.5 - Terminal:
ghostty
Bug Description
Claude code ignores instructions to ensure that no trailing whitespace is left in files and that files end with newlines. I have to require it to run a pre-commit script that will strip whitespace. This is something it should be able to do (and should do, unless instructed otherwise) out of the box. Cursor and the like respect editor settings on this front, which Claude Code can't benefit from. I don't know if it's a matter of needing a better model that is more capable or some post-processing. Regardless, this is entry-level stuff that should be addressed.
Steps to Reproduce
- Use Claude Code to make changes or write new files
- Observe trailing whitespace, especially on blank lines between indented line and no newline at EOF
- Add explicit instructions to
CLAUDE.mdto remove trailing whitespace and always include newlines at EOF - Observe that these instructions are ignored
Expected Behavior
No trailing whitespace and all files end with newline
Actual Behavior
Trailing whitespace often and files typically do not end with newline
Additional Context
A related issue was opened and closed without explanation: https://github.com/anthropics/claude-code/issues/363
Should be fixable with a simple trim of each line.
This is likely due to limitation of the writing tool, having an empty code line is best practice for python files. It would be great if could be fixed.
- Add explicit instructions to CLAUDE.md to remove trailing whitespace and always include newlines at EOF
I had Claude try to refine this rule a few times and it came up with this:
- ALWAYS ensure every new file ends with a newline character (\n) - the file content should literally end with an empty line
Yet, it still fails to produce a newline at the end of new files.
Had the same issue and came up with this entry for CLAUDE.md which seems to work:
## Code Generation Guidelines
**IMPORTANT**: All generated text-based files must end with a trailing newline character. This ensures proper file formatting, better git diffs, and compliance with POSIX standards. When creating or modifying files:
1. **Always verify** that files end with a newline character
2. **Use bash commands** (e.g., `echo "" >> filename`) if the Write tool doesn't preserve trailing newlines
3. **Check with hexdump** (`tail -c 5 filename | xxd`) to verify proper line endings
4. **Apply to all text files** including: .dart, .md, .yaml, .json, .sh, .bat, .env, etc.
I'd hope anthropic could do some RL to avoid this behavior; in the mean time however, I would recommend a post tool use hook that matches against: Write|Edit|MultiEdit and runs
bash -c 'output=$(git --no-pager diff --check); if [ -n \"$output\" ]; then echo \"$output\" >&2; echo \"\\nERROR: Trailing whitespace detected! You must fix these issues.\" >&2; exit 2; fi'
I'd like to point out that stripping the trailing newline causes Claude Code to produce output that does not conform to the POSIX definition of a text file.
According to POSIX, a text file must consist of zero or more lines each ending with a newline character.
By omitting the final newline, the last line is technically incomplete, making the file invalid as a POSIX-compliant text file.
This can lead to interoperability issues with Unix tools that assume proper line termination, that's why diff is complaining about "No newline at end of file".
I instructed it with use \n\n for EOF It works.
Found 3 possible duplicate issues:
- https://github.com/anthropics/claude-code/issues/184
- https://github.com/anthropics/claude-code/issues/5278
- https://github.com/anthropics/claude-code/issues/3521
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
This issue should NOT be closed as a duplicate, for the following reasons:
- #184 and #3521 are only about trailing newlines, and don't cover trailing whitespace
- #5278 is a duplicate of this issue but it's also substantially newer, so if anything should be closed here it's #5278 not this one.
Also #5278 is specific to Python but the problem is more general than just handling of Python files.
Y'all please, this is a really annoying thing and makes editing code with claude more time consuming and fiddly-diddly.
How about a simple filter or post-hook for the Edit() action (or whatever) that just removes all trailing whitespace before returning the final edit text/diff?
Also, is this the correct place to track this issue now? (previous issue: https://github.com/anthropics/claude-code/issues/363)
Hi! What are best approaches so far? combination of claude hooks https://docs.anthropic.com/en/docs/claude-code/hooks with git hooks? Has someone made repo/script/etc for such setup or we make it on our own? Seems like there are more of us so having some consistent shared way how to deal with that could be beneficial to all I guess?