Skip to content

fix(rename): read and write source files as UTF-8#6761

Open
anxkhn wants to merge 1 commit into
reflex-dev:mainfrom
anxkhn:patch-13
Open

fix(rename): read and write source files as UTF-8#6761
anxkhn wants to merge 1 commit into
reflex-dev:mainfrom
anxkhn:patch-13

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

What and why

reflex rename <new_name> walks the user's app and rewrites imports and the app
name in every .py and .md file. The read/modify/write in
rename_imports_and_app_name (reflex/utils/rename.py) used
Path.read_text() and Path.write_text() with no encoding= argument, so both
default to the platform's locale encoding. On a Western Windows locale that is
cp1252, not UTF-8.

The consequences on non-UTF-8 locales:

  • Silent corruption (Western Windows, cp1252): a UTF-8-authored file that
    contains non-ASCII bytes (curly quotes in docstrings, accented identifiers or
    string literals, emoji) is decoded as cp1252, rewritten, and written back as
    cp1252, mangling the user's own source.
  • Hard failure (CJK codepages): read_text() raises UnicodeDecodeError,
    aborting the rename partway through the tree and leaving some files already
    rewritten and others not, i.e. an inconsistent app.

On a POSIX UTF-8 locale it round-trips, so this is effectively Windows-specific.

The fix passes encoding="utf-8" on both calls. This mirrors the repo's own
reflex/utils/path_ops.py find_replace, which already reads and writes the same
read/replace/write pattern with encoding="utf-8"; rename.py was the outlier.

Changes

  • reflex/utils/rename.py: add encoding="utf-8" to the read_text() and
    write_text() calls in rename_imports_and_app_name (+2 / -2).
  • tests/units/test_prerequisites.py: add regression test
    test_rename_imports_and_app_name_preserves_utf8, colocated with the existing
    rename tests. It simulates a cp1252 default (monkeypatching Path.read_text /
    write_text so a missing encoding falls back to cp1252, while an explicit
    encoding= still passes through), writes a UTF-8 file with realistic non-ASCII
    content, runs the rename, and asserts the bytes are preserved exactly and the
    rename still happened. The test fails before the fix (UnicodeDecodeError) and
    passes after.
  • news/<pr>.bugfix.md: towncrier changelog fragment.

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them? (above)
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Locally (all green):

  • uv run pytest tests/units/test_prerequisites.py -k rename -> 6 passed
  • uv run pytest tests/units/test_prerequisites.py tests/units/utils -> 677 passed, 4 skipped
  • uv run ruff check . (changed files) -> All checks passed
  • uv run ruff format --check . (changed files) -> already formatted
  • uv run pyright reflex/utils/rename.py tests/units/test_prerequisites.py -> 0 errors
  • uv run towncrier check --compare-with origin/main -> fragment found

reflex rename reads every .py/.md file in the user's app with
Path.read_text() and writes it back with Path.write_text(), neither
of which passed encoding=. Both default to the platform locale
encoding, which on Western Windows is cp1252 rather than UTF-8.

UTF-8-authored source containing non-ASCII bytes (curly quotes, emoji,
accented identifiers or docstrings) is therefore silently corrupted on
a Western Windows locale, or aborts the rename partway with
UnicodeDecodeError on a CJK codepage, leaving the tree in an
inconsistent state. Pass encoding="utf-8" on both calls, matching
path_ops.find_replace which already does this.

Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
@anxkhn anxkhn requested a review from a team as a code owner July 14, 2026 07:50
@greptile-apps

greptile-apps Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes reflex rename handle UTF-8 source files consistently across platforms. The main changes are:

  • Reads and writes Python and Markdown files explicitly as UTF-8.
  • Adds a test that simulates a Windows cp1252 locale.
  • Adds a bug-fix changelog fragment.

Confidence Score: 5/5

This looks safe to merge.

  • The explicit encoding removes dependence on the platform locale.
  • The new test covers the reported Windows failure mode.
  • No additional blocking issue was found in the updated code.

Important Files Changed

Filename Overview
reflex/utils/rename.py Uses explicit UTF-8 encoding for the source-file read and write operations.
tests/units/test_prerequisites.py Adds coverage for preserving UTF-8 content when the platform default is cp1252.
news/6714.bugfix.md Documents the locale-dependent source-file encoding fix.

Reviews (2): Last reviewed commit: "fix(rename): read and write source files..." | Re-trigger Greptile

Comment thread reflex/utils/rename.py
"""
file_path = Path(file_path)
content = file_path.read_text()
content = file_path.read_text(encoding="utf-8")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Encoding Cookie Is Ignored

When the app contains valid Python source declared as cp1252 or latin-1, a non-ASCII byte such as 0xe9 now raises UnicodeDecodeError. The rename traversal passes every .py file here and may already have rewritten earlier files, leaving the app partially renamed; detect the source encoding and preserve it instead of always decoding as UTF-8.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 657b04cfd5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread reflex/utils/rename.py
"""
file_path = Path(file_path)
content = file_path.read_text()
content = file_path.read_text(encoding="utf-8")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor declared Python source encodings

For .py files that are valid Python but declare a non-UTF-8 source encoding (for example # -*- coding: cp1252 -*-) and contain bytes outside UTF-8, this unconditional UTF-8 read now raises UnicodeDecodeError before renaming imports. process_directory applies this to every Python file, so one such module can abort reflex rename part-way through an app; using Python's source-encoding detection for .py files would preserve the fix for UTF-8 files without rejecting valid encoded modules.

Useful? React with 👍 / 👎.

@Alek99 Alek99 closed this Jul 15, 2026
@Alek99 Alek99 reopened this Jul 15, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing anxkhn:patch-13 (657b04c) with main (65a2889)2

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (127e4d8) during the generation of this report, so 65a2889 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

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.

2 participants