[BUG] Trailing whitespace and no newline at end of file

Resolved 💬 19 comments Opened Jun 28, 2025 by aaronjensen Closed Jan 26, 2026

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

  1. Use Claude Code to make changes or write new files
  2. Observe trailing whitespace, especially on blank lines between indented line and no newline at EOF
  3. Add explicit instructions to CLAUDE.md to remove trailing whitespace and always include newlines at EOF
  4. 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

View original on GitHub ↗

19 Comments

pelillian · 1 year ago

Should be fixable with a simple trim of each line.

eliyahuA · 1 year ago

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.

jonathansterling · 1 year ago
3. 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.

sebthom · 1 year ago

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.
PastaPastaPasta · 1 year ago

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'
gmiroshnykov · 1 year ago

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".

ryush00 · 11 months ago

I instructed it with use \n\n for EOF It works.

github-actions[bot] · 11 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/184
  2. https://github.com/anthropics/claude-code/issues/5278
  3. 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

aspiers · 11 months ago

This issue should NOT be closed as a duplicate, for the following reasons:

  1. #184 and #3521 are only about trailing newlines, and don't cover trailing whitespace
  2. #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.
aspiers · 11 months ago

Also #5278 is specific to Python but the problem is more general than just handling of Python files.

hjkatz · 10 months ago

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)

gwpl · 10 months ago

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?

github-actions[bot] · 7 months ago

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.

luhring · 7 months ago

It's still occurring

AlDante · 6 months ago

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).

github-actions[bot] · 5 months ago

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.

fsc-eriker · 5 months ago

Incorrectly closed, apparently due to https://github.com/anthropics/claude-code/issues/16497

Notum · 5 months ago

This is definitely not fixed. A lot if trailing spaces, especially in Questionary.

github-actions[bot] · 5 months ago

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.