codex - 💡(How to fix) Fix Support for different file encodings and windows style line endings in apply_patch internal functions and code.exe command line apply patch [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#21164Fetched 2026-05-06 06:25:34
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

Fix Action

Fix / Workaround

Currently apply_patch fails on non UTF-8 encoded files, making codex revert to other commands/tools for patching code and as a consequence there is no file diff report in the app window. I suggest making apply_patch encoding aware either by ascertaining the encoding or by having the encoding passed in as an argument.

Other issue on windows is also that apply_patch uses linux style line endings only (LF) and this causes files with mixed line endings that IDEs then complain about. It should either maintain the current file line endings or have a setting or input argument to allow windows style line endings (CRLF).

RAW_BUFFERClick to expand / collapse

What variant of Codex are you using?

App

What feature would you like to see?

Currently apply_patch fails on non UTF-8 encoded files, making codex revert to other commands/tools for patching code and as a consequence there is no file diff report in the app window. I suggest making apply_patch encoding aware either by ascertaining the encoding or by having the encoding passed in as an argument.

Other issue on windows is also that apply_patch uses linux style line endings only (LF) and this causes files with mixed line endings that IDEs then complain about. It should either maintain the current file line endings or have a setting or input argument to allow windows style line endings (CRLF).

Additional information

No response

extent analysis

TL;DR

Modify the apply_patch function to handle non-UTF-8 encoded files and support both Linux and Windows line endings.

Guidance

  • Investigate adding an optional encoding parameter to the apply_patch function to allow users to specify the file encoding.
  • Consider using a library that can automatically detect the file encoding to handle cases where the encoding is unknown.
  • Update the apply_patch function to preserve the original line endings of the file or add an option to specify the desired line endings (LF or CRLF).
  • Test the updated apply_patch function with files having different encodings and line endings to ensure compatibility.

Example

def apply_patch(file_path, patch, encoding='utf-8', line_ending='lf'):
    # ...
    with open(file_path, 'r', encoding=encoding) as file:
        # ...
    # ...
    with open(file_path, 'w', encoding=encoding, newline=line_ending) as file:
        # ...

Notes

The solution may require additional error handling and testing to ensure compatibility with different file encodings and line endings.

Recommendation

Apply workaround by modifying the apply_patch function to handle non-UTF-8 encoded files and support both Linux and Windows line endings, as this will provide a more flexible and compatible solution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING