fix(cli): make lint-translations exit non-zero when translations differ - #2996
fix(cli): make lint-translations exit non-zero when translations differ#2996Zuhef wants to merge 2 commits into
Conversation
|
@codex review |
There was a problem hiding this comment.
Pull request overview
Updates the translation linter so translation differences can reliably fail CI.
Changes:
- Returns detected differences and aggregates their count.
- Produces deterministic file ordering.
- Exits with status 1 and prints a summary when differences exist.
- Adds coverage for return values, counting, and file filtering.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
backend/chainlit/translations.py |
Returns detected structural differences. |
backend/chainlit/config.py |
Aggregates differences across sorted translation files. |
backend/chainlit/cli/__init__.py |
Exits non-zero when differences are found. |
backend/tests/test_translations.py |
Tests difference reporting and aggregation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
`chainlit lint-translations` reported differences and then exited 0, so it could not be used as a CI gate - which is how missing locale keys accumulate unnoticed. The chain was silent end to end: `lint_translation_json` printed the differences and returned None, `lint_translations` returned None, and the command ignored the result. On a checkout of main the command reports six real missing keys across the packaged locales and still exits 0. `lint_translation_json` now returns the differences it printed, and `lint_translations` returns the total count, so the command can fail. Both are additive: existing callers that ignore the return value are unaffected. Also dedent the per-file loop out of the ground-truth `with open(...)` block, which held that handle open for the whole run, and sort the directory listing so the report order is deterministic. Rebased onto main after Chainlit#3006, which introduced `_safe_print`. The new summary line carries a marker that cp1252 cannot encode, so it goes through `_safe_print` too rather than a bare `print` - otherwise the CLI layer would reintroduce the UnicodeEncodeError that Chainlit#3006 just fixed in the library layer. Verified on Windows against a directory holding all 23 packaged locales: before, 6 differences reported and exit 0; after, the same 6 differences and exit 1. A clean directory still exits 0, and a broken one on a cp1252 stdout still exits 1 with the marker degraded instead of crashing.
af2f7ad to
c700d1a
Compare
|
Done — rebased onto The conflicts came from #3006, which landed in the same two functions while this was open. Both sides were mechanical:
One change beyond the merge itself, which I want to flag explicitly rather than slip in: the summary line this PR adds also carries a
Two notes:
|
I'll need to take some time to manual review as this is clearly a bot talking. Please consider marking bot posts and PR's as such (e.g. through a GH app or marked-as-such bot account). |
|
I manually reviewed the changes, conflict resolution, and tests before pushing. AI was only used to help rephrase the PR documentation.. |
Problem
chainlit lint-translationsreports differences and then exits 0, so it cannot gate anything. Issue #2993 identified this as the reason locale keys drift unnoticed, and it is true even for the app translations the command is actually pointed at.The chain is silent end to end:
lint_translation_jsonprints the differences and returnsNonelint_translationsreturnsNonechainlit_lint_translationscalls it and ignores the result, so Click exits 0Change
lint_translation_jsonreturns the differences it printed. Additive - existing callers that ignore the return value are unaffected, and the printed output is unchanged.lint_translationsreturns the total count across all linted files.SystemExit(1)when the count is non-zero.Two incidental cleanups in the same function: the per-file loop was nested inside the ground-truth
with open(...)block, holding that handle open for the entire run, so it is dedented; andos.listdiris now sorted so the report order is deterministic. Happy to drop either if you would rather keep the diff to the exit code alone.Verification
Ran on Windows against a directory containing all 23 packaged locales, comparing a clean
mainworktree with the patched tree:mainSame six differences either way - only the exit code changes. A directory whose translations match the ground truth still exits 0.
The six it finds are the real gaps from #2993, which is a useful independent cross-check: this is the CLI path rather than the test suite, and it reports exactly
chat.favorites.remove(ar-SA, da-DK),components.DatePickerInput(de-DE, it, ko) andchat.fileUpload.browse(ja).pytest backend/tests/test_translations.py- 41 passed. That is the 39 from this PR plus the two cp1252 regression tests added by #3006, both preserved through the rebase. New tests here cover the returned error list, the aggregate count for both clean and broken input, and that non-JSON files in the directory are ignored.ruff format --checkclean on all four files.ruff checkreports two RUF036 findings atconfig.py:477-478(on_app_startup/on_app_shutdown), which are pre-existing onmainand untouched by this change.Note
The
UnicodeEncodeErrorI originally flagged in this section - a stock Windows console cannot encode the✅/❌markers - was filed as #2997 and fixed upstream by #3006 while this PR was open. So that note is resolved, and #3006 touching the same two files is what produced the merge conflicts.Now rebased onto current
main. The conflicts were both mechanical:translations.py- kept fix(cli): avoid lint-translations UnicodeEncodeError on legacy consoles #3006's_safe_printcalls, re-applied this PR's docstring andreturn errors.test_translations.py- union of the two import sets; the test bodies did not overlap, so both sets are intact.One follow-through worth calling out: the summary line this PR adds also carries a
❌, so it now goes through #3006's_safe_printrather than a bareprint. Otherwise the CLI layer would reintroduce in the command exactly the crash #3006 just fixed in the library. Verified against a cp1252 stdout - the command still exits 1 and the marker degrades to?instead of raising. Happy to add a regression test for that CLI path, or to drop the change, whichever you prefer.Summary by cubic
Make
chainlit lint-translationsfail fast when translations differ. Previously it printed differences and exited 0; now it exits 1 so CI can gate locale changes.lint_translation_jsonreturns the list of differences (printed output unchanged).lint_translationsreturns the total difference count and sorts the directory listing for deterministic reports.with open(...)block to avoid holding the handle open.Written for commit af2f7ad. Summary will update on new commits.