Codex CLI tends to add and remove blank lines when patching files

Open 💬 6 comments Opened May 5, 2026 by pmetzger
💡 Likely answer: A maintainer (etraut-openai, contributor) responded on this thread — see the highlighted reply below.

What version of Codex CLI is running?

codex-cli 0.128.0

What subscription do you have?

Pro

Which model were you using?

GPT 5.5

What platform is your computer?

_No response_

What terminal emulator and version are you using (if applicable)?

Terminal.app

What issue are you seeing?

Whatever patch tool is being used by Codex when rewriting files has a habit of adding or removing blank lines at the ends of files, seemingly at random. I've gotten to the point where my prompts and AGENTS files repeatedly say things like "double check that you don't modify blank lines at the ends of files". I suspect this is a bug in the tool the agent is using and not in the main part of the agent itself, but that's a guess.

What steps can reproduce the bug?

Modify a lot of files, look at the diffs, notice that blank lines get removed or sometimes added to the ends of files without much rhyme or reason.

What is the expected behavior?

_No response_

Additional information

_No response_

View original on GitHub ↗

6 Comments

etraut-openai contributor · 2 months ago

If you have a repro, please use /feedback to upload the logs and session details and then post the session ID here.

pmetzger · 2 months ago

FYI: This is a comment the agent makes over and over again to me in various forms:

apply_patch is not preserving trailing empty lines at EOF, so I’m using a
narrow mechanical Perl rewrite to restore the exact trailing-newline counts
from HEAD for the affected files. This is only for EOF whitespace restoration.

I presume apply_patch is a tool. I'd see what it does to empty lines at EOF; maybe it simply is a mechanical problem. Removal is far more frequent in my experience than addition.

pmetzger · 2 months ago

@etraut-openai It's session ID 019df459-19b3-7c41-8ecc-13dd9b9fb48b

pmetzger · 2 months ago

FWIW, it may be that I'm mistaken and that it's only removing blank lines at the end as a result of apply_patch and that problems with adding them back are caused by it fighting apply_patch; the underlying problem may very well just be an issue with apply_patch.

LPTK · 1 month ago

We have exactly the same issue. Our SKILL states that empty lines should be left alone, but this causes Codex to visibly fight with its tooling for long periods of time, and particularly apply_patch, as that tool keeps trimming empty lines, especially at the end of files.

pmetzger · 1 month ago

I was wasting tons of tokens on this, so my interim mechanism (which I hate) has been to eliminate blank lines at the ends of files, after which the problem has gotten significantly better. However, it's still an issue; this shouldn't be a problem in the first place. There's clearly a bug in the patch tool.