[BUG] Trailing whitespace and no newline at end of file
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- 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
19 Comments
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.
I had Claude try to refine this rule a few times and it came up with this:
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:
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
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
diffis complaining about "No newline at end of file".I instructed it with
use \n\n for EOFIt works.Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
This issue should NOT be closed as a duplicate, for the following reasons:
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?
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.
It's still occurring
This issue is incredibly annoying when coding in Swift. Here are some examples:
optionType: optionSide. qlCode
guard (-1... 1).contains(riskFreeRate) else {
@Published var riskFreeRate: Double = 0. 03
.font(.title3. weight(.semibold))
...
which all result in "Extraneous whitespace after '.' is not permitted" which has to be corrected manually. And is trashed by Claude's next change. (Sonnet 4.5).
This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.
Incorrectly closed, apparently due to https://github.com/anthropics/claude-code/issues/16497
This is definitely not fixed. A lot if trailing spaces, especially in Questionary.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.