Trailing Whitespace Consistently Added by Claude During Code Generation

Status Fixed / completed
Maintainer reply None cached
Activity 12 comments · opened Mar 6, 2025 · closed May 8, 2025

Bug Description
Claude introduces trailing whitespace frequently in my code files. Such as on new lines before functions I let it add.

Additionally, I asked it to stop doing this going forward, and it said it would, but it still continued to do so.

Environment Info

  • Platform: macos
  • Terminal: iTerm.app
  • Version: 0.2.32
  • Feedback ID: 4a9bd58c-6499-4d8f-9884-1061055acbc9

View original on GitHub ↗

12 Comments

greggh · 1 year ago

I have this issue in nearly every file Claude Code edits. I've given up on convincing it to stop doing this. Instead I have installed every styler and linter I could possibly need and I use them to clean up afterword in git pre-commit hooks. For a good project I should have those anyways, but it would be nice if this was solved.

For reference, Linux here.

superchordate · 1 year ago

Same here on Windows/WSL working on NextJS development. Adds lots of blank lines. Maybe 10% of the time.

I also see the "String to replace not found in file" pretty often. Might be related.

 Update(file_path: src/components/PlanCard.js)…
  ⎿  String to replace not found in file.

● I apologize for the error. Let me try again with the exact content from the file:

● Update(file_path: src/components/PlanCard.js)…
  ⎿  String to replace not found in file.

Let me view the exact content of the function to edit it correctly:

● Search(pattern: "getOutOfNetworkCoverageLevel", path: "src/components/PlanCard.js")…
  ⎿  Found 1 file

another example:

Now I'll use the exact content to make the edits:

● Update(file_path: src/components/PlanCard.js)…
  ⎿  String to replace not found in file.

● Let me try to update the file using Replace instead:

● Read(file_path: "src/components/PlanCard.js")…

Will follow up to add more info.

Update: this step did it.

Bash(sed -n '60,65p' /mnt/c/Users/.../Documents/.../src/components/PlanResults.js)…
  ⎿  if (initialProvidersString !== providersString) {
           setFilters(prev => ({ ...prev, providers: JSON.parse(initialProvidersString) }));
         }
       }, [initialProvidersString]);

       // Extract unique values for filter options

● Now I can use the exact text:

● Update(file_path: src/components/PlanResults.js)…
  ⎿ Updated src/components/PlanResults.js with 1 addition and 1 removal
     60      if (initialProvidersString !== providersString) {
     61        setFilters(prev => ({ ...prev, providers: JSON.parse(initialProvidersString) }));
     62      }
     63    }, [initialProvidersString]);
     63    }, [initialProvidersString, providersString]);
     64
     65    // Extract unique values for filter options
     66    const filterOptions = useMemo(() => ({

Looks like it thinks it added one line but actually added many blank lines.

mrhyde · 1 year ago

!Image

I have tried multiple variations of prompts forcing him not to generate empty lines with whitespaces, but it keeps happening. However, it calls linter afterwards correctly and ends up fixing these issues, which just wastes tokens on each iteration.

StoneHub · 1 year ago
Same here on Windows/WSL working on NextJS development. Adds lots of blank lines. Maybe 10% of the time. I also see the "String to replace not found in file" pretty often. Might be related. `` Update(file_path: src/components/PlanCard.js)… ⎿ String to replace not found in file. ● I apologize for the error. Let me try again with the exact content from the file: ● Update(file_path: src/components/PlanCard.js)… ⎿ String to replace not found in file. Let me view the exact content of the function to edit it correctly: ● Search(pattern: "getOutOfNetworkCoverageLevel", path: "src/components/PlanCard.js")… ⎿ Found 1 file ` another example: ` Now I'll use the exact content to make the edits: ● Update(file_path: src/components/PlanCard.js)… ⎿ String to replace not found in file. ● Let me try to update the file using Replace instead: ● Read(file_path: "src/components/PlanCard.js")… ` Will follow up to add more info. Update: this step did it. ` Bash(sed -n '60,65p' /mnt/c/Users/.../Documents/.../src/components/PlanResults.js)… ⎿ if (initialProvidersString !== providersString) { setFilters(prev => ({ ...prev, providers: JSON.parse(initialProvidersString) })); } }, [initialProvidersString]); // Extract unique values for filter options ● Now I can use the exact text: ● Update(file_path: src/components/PlanResults.js)… ⎿ Updated src/components/PlanResults.js with 1 addition and 1 removal 60 if (initialProvidersString !== providersString) { 61 setFilters(prev => ({ ...prev, providers: JSON.parse(initialProvidersString) })); 62 } 63 }, [initialProvidersString]); 63 }, [initialProvidersString, providersString]); 64 65 // Extract unique values for filter options 66 const filterOptions = useMemo(() => ({ `` Looks like it thinks it added one line but actually added many blank lines.

Yes, thank you same issue here working on Kotlin files. Might be related to line ending conversions from WSL to windows, event edited the line endings generated by Android Studio, no luck. Claude desktop app with MCP server is what im still using until this gets fixed. I would love a native powershell Claude tool.

snarfed · 1 year ago

I've said pretty clearly in my CLAUDE.md to not do this:

Blank lines are good, but don't leave any whitespace on them.
Don't leave trailing whitespace at the end of lines.

...but it doesn't help. I asked Claude itself, and its answer was unsatisfying.

> you tend to leave trailing whitespace on blank lines in the code you generate, 
  even though I've asked you not to in my CLAUDE.md. how can I get you to not do 
  that?

⏺ I apologize for not consistently following your instruction about avoiding
  trailing whitespace on blank lines. Let me look at your CLAUDE.md to confirm this
  requirement.

⏺ Read(../../.claude/CLAUDE.md)…
  ⎿  Read 12 lines (ctrl+r to expand)

⏺ You're right - I see on line 7: "Blank lines are good, but don't leave any
  whitespace on them."

  To prevent this in the future:
  1. Add this to CLAUDE.md with more emphasis
  2. Create an issue at https://github.com/anthropics/claude-code/issues
  3. When reviewing code I generate, point it out immediately

"more emphasis" indeed. 🤷

djechlin · 1 year ago

I noticed this issue was closed but doesn't appear to be resolved. Can you supply the reason it's closed?

Keno · 1 year ago

Same here - it's extremely annoying. Happening in both Julia source files and TypeScript.

hjkatz · 1 year ago

I'm also experiencing this issue. Please can we configure Claude to remove trailing whitespace and empty whitespace newlines from outputs? Both in CLI claude-code and in the web interface?

aaronjensen · 1 year ago

It's also neglecting to add trailing newlines, no matter how much it's instructed to add them. When you tell it to add it explicitly, it errors because its find/replace doesn't find them and then it ends up doing a different technique. It's pretty awful.

 Error: No changes to make: old_string and new_string are exactly the same.
pelillian · 1 year ago

Experiencing this as well. It's really annoying—please fix!

hjkatz · 1 year ago

@bcherny I see you closed this issue - Is the work completed/fixed in the latest npm package or is the bug mentioned by @aaronjensen the right place to track this now?

github-actions[bot] · 1 year 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.