apply_patch fails on UTF-8 BOM files
Open 💬 2 comments Opened Jun 12, 2026 by wangyauneli
Description
The \^Gpply_patch\ tool fails to match context when the target file has a UTF-8 BOM (bytes \EF BB BF\). It returns \Failed to find context\ without indicating the BOM is the cause, which is misleading and causes the agent to retry endlessly.
Steps to reproduce
- Create a JS file with UTF-8 BOM encoding (e.g. saved by Windows Notepad or WeChat DevTools)
- Ask the agent to modify a line in that file using \^Gpply_patch\
- The tool reports \Failed to find context\ even though the context lines are present in the file
Expected behavior
The tool should detect and strip BOM before matching context, then preserve the original encoding when writing back.
Impact
Any .js / .json / other files saved with BOM by Windows editors cannot be patched, causing the agent to waste many retry attempts without understanding the root cause. This is a common scenario on Windows since Notepad adds BOM by default.
2 Comments
I reproduced this against current
mainwith the standaloneapply_patchbinary.Minimal repro:
Actual result:
The likely owner is
codex-rs/apply-patch/src/lib.rs::derive_new_contents_from_chunks.Current behavior:
fs.read_file_text(...)returns the file as text, including the leading UTF-8 BOM as\u{FEFF}.derive_new_contents_from_chunksimmediately splits it withoriginal_contents.split('\n')."\u{FEFF}const value = 1;", not"const value = 1;".compute_replacements(...)/seek_sequence(...)then cannot match ordinary patch context for the first line and reports a missing context line.Suggested fix shape:
Suggested regression:
Per
docs/contributing.md, I am not opening a PR unless invited, but this should be a small focused patch in the apply-patch crate.I provided a patch file with BOM detection in a patch file in this comment:
https://github.com/openai/codex/issues/13918#issuecomment-4808867609
The same mechanism can be used for detecting the BOM before patching. Can't provide the fix myself, since contributions are limited to OAI employees.