Skip to content

fix: add explicit encoding to open() calls and improve error handling#5428

Open
ashishgowdamj wants to merge 1 commit into
Aider-AI:mainfrom
ashishgowdamj:fix/missing-encoding-open-calls
Open

fix: add explicit encoding to open() calls and improve error handling#5428
ashishgowdamj wants to merge 1 commit into
Aider-AI:mainfrom
ashishgowdamj:fix/missing-encoding-open-calls

Conversation

@ashishgowdamj

Copy link
Copy Markdown

Summary

Add explicit encoding='utf-8' to open() calls across the codebase to prevent UnicodeDecodeError on Windows where the default encoding (e.g. cp1252) differs from UTF-8.

Changes

Production code

  • aider/watch.py: Fixes Uncaught UnicodeDecodeError in cp1252.py line 23 #2888 - added encoding='utf-8', errors='replace' when reading .gitignore files to handle non-UTF8 content gracefully
  • aider/editor.py: Added encoding='utf-8' + try/except around temp file read to handle missing/unreadable files after editor closes
  • aider/main.py: Added encoding to config file read, Streamlit credentials write, and installs file read/write
  • aider/models.py: Added encoding to model settings YAML read
  • aider/analytics.py: Added encoding to analytics log write
  • aider/history.py: Added encoding to history file read

Tests

  • test_editor.py: Added test_pipe_editor_read_failure and test_pipe_editor_unicode_decode_error to verify graceful fallback
  • test_watch.py: Added test_load_gitignores_handles_non_utf8, test_load_gitignores_handles_utf8_bom, test_load_gitignores_none_paths

Testing

All existing tests pass (109 passed, excluding 1 pre-existing failure unrelated to this change).

Add explicit encoding='utf-8' to open() calls across the codebase to
prevent UnicodeDecodeError on Windows where default encoding may differ.

Fixes:
- editor.py: add try/except around temp file read + encoding
- watch.py: add encoding + errors='replace' for gitignore files (fixes Aider-AI#2888)
- main.py: encoding for config, credentials, and installs files
- models.py, analytics.py, history.py: encoding for file I/O

Adds tests:
- editor: read failure and UnicodeDecodeError graceful fallback
- watch: non-UTF8 gitignore handling, BOM handling, None/empty input
@CLAassistant

CLAassistant commented Jul 10, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uncaught UnicodeDecodeError in cp1252.py line 23

2 participants