Repository navigation
Write package.json in bun's layout - #7812
Conversation
Reflex rendered .web/package.json (persisted byte-for-byte to reflex.lock/package.json) as compact one-line JSON, while bun rewrites it 2-space indented whenever `bun add` changes it, so the persisted file flipped between the two layouts and produced content-free VCS diffs. Render it with `indent=2`, a trailing newline and raw UTF-8 (bun does not escape non-ASCII) in `package_json_template` and `update_package_json_overrides`, writing and reading the file as UTF-8. bun keeps the file's trailing newline, so a `bun add` now changes only the entries it adds, not the layout. Found in the reflex 0.11.0a1 pre-release testing (FINDING-029). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
[Medium impact] The PR appears safe to merge; no new actionable issues were found. SummaryThis PR writes
Reviews (3) · Last reviewed commit: "Treat a non-UTF-8 .web/package.json as u..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Review follow-ups for writing package.json in bun's layout: - Read package.json as UTF-8 in `get_package_json_and_hash` and `_existing_web_package_sections` too. Now that reflex writes raw UTF-8, their locale-encoding reads raise `UnicodeDecodeError` on a cp1252 Windows locale for non-ASCII content. - Treat a package.json that is not valid UTF-8 as unusable in `_read_package_json_object`, like invalid JSON, instead of raising. - Write package.json with `newline="\n"`, so Windows writes LF like bun does instead of CRLF. - Classify the changelog fragments as bugfix. The tests run the readers and writers under a `windows_text_io` fixture that gives text-mode file access the defaults of a Windows locale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
Merging this PR will regress 1 benchmark2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
|
CodSpeed is red here on Generated by Claude Code |
_existing_web_package_sections read the file as UTF-8 but caught only JSONDecodeError and OSError, so a file that is not valid UTF-8 raised a traceback during the frontend install instead of the usual warning. Also route the three package.json writes through one _write_package_json helper so the UTF-8 / LF write mode is defined once.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
* Write package.json in bun's layout Reflex rendered .web/package.json (persisted byte-for-byte to reflex.lock/package.json) as compact one-line JSON, while bun rewrites it 2-space indented whenever `bun add` changes it, so the persisted file flipped between the two layouts and produced content-free VCS diffs. Render it with `indent=2`, a trailing newline and raw UTF-8 (bun does not escape non-ASCII) in `package_json_template` and `update_package_json_overrides`, writing and reading the file as UTF-8. bun keeps the file's trailing newline, so a `bun add` now changes only the entries it adds, not the layout. Found in the reflex 0.11.0a1 pre-release testing (FINDING-029). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Read and write package.json as UTF-8 with LF newlines everywhere Review follow-ups for writing package.json in bun's layout: - Read package.json as UTF-8 in `get_package_json_and_hash` and `_existing_web_package_sections` too. Now that reflex writes raw UTF-8, their locale-encoding reads raise `UnicodeDecodeError` on a cp1252 Windows locale for non-ASCII content. - Treat a package.json that is not valid UTF-8 as unusable in `_read_package_json_object`, like invalid JSON, instead of raising. - Write package.json with `newline="\n"`, so Windows writes LF like bun does instead of CRLF. - Classify the changelog fragments as bugfix. The tests run the readers and writers under a `windows_text_io` fixture that gives text-mode file access the defaults of a Windows locale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Treat a non-UTF-8 .web/package.json as unreadable in the package check _existing_web_package_sections read the file as UTF-8 but caught only JSONDecodeError and OSError, so a file that is not valid UTF-8 raised a traceback during the frontend install instead of the usual warning. Also route the three package.json writes through one _write_package_json helper so the UTF-8 / LF write mode is defined once. --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Farhan <www.mfarvirus@gmail.com> (cherry picked from commit f13348a)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
Type of change
Description
Found in the reflex 0.11.0a1 pre-release testing (FINDING-029).
Reflex wrote
.web/package.jsonas compact one-line JSON, andreflex.lock/package.jsonis a byte copy of that file. Wheneverbun addchanges the file, bun rewrites it 2-space indented. The persisted file therefore flipped between the two layouts and produced VCS diffs with no content change.Every package.json writer now produces bun's layout:
reflex_base.compiler.templates.package_json_template, whichinitialize_package_jsonandsync_root_package_json_to_webusefrontend_skeleton.update_package_json_overridesThe layout is
json.dumps(..., indent=2, ensure_ascii=False) + "\n", written as UTF-8 withnewline="\n", so Windows writes LF like bun does. bun writes non-ASCII characters as raw UTF-8 rather than\uXXXXescapes. For that reason every package.json reader now usesencoding="utf-8":_read_package_json_object,js_runtimes._existing_web_package_sectionsandexec.get_package_json_and_hash. A file that is not valid UTF-8 is treated as unusable, the same as invalid JSON. The persisted copy is made withpath_ops.cp, so it gets the same layout.Checked against bun 1.4.4, the version reflex pins:
bun addon a reflex-rendered file changes only the entries it adds. Its output equalsjson.dumps(json.loads(out), indent=2, ensure_ascii=False) + "\n".bun addandbun removebyte-identical.Changes To Core Features:
Every test below fails on main and passes with this change. Most of them run under a new
windows_text_iofixture intests/units/conftest.py. When no encoding or newline is given, the fixture makes text-mode file access decode as ASCII and translate LF to CRLF on writes.tests/units/utils/test_frontend_skeleton.py:test_rendered_package_json_matches_bun_formatandtest_update_package_json_overrides_matches_bun_format: layout, UTF-8, and no\rin the file.test_sync_root_package_json_repairs_unusable_file[\xff{}]: a non-UTF-8 file is repaired instead of raising.tests/units/utils/test_exec.py::test_get_package_json_and_hash_reads_utf8tests/units/utils/test_js_runtimes.py::test_existing_web_package_sections_reads_utf8test_frontend_skeleton.py,test_exec.py,test_js_runtimes.py,test_prerequisites.pyandtests/units/compilerpass, and ruff, ruff format, pyright and pre-commit are clean on the changed files.🤖 Generated with Claude Code
https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi