Repository navigation
uucore/locale: honour LC_ALL and LC_MESSAGES when picking the locale - #14870
sunsss123soldier wants to merge 1 commit into
Conversation
| let original_lang = env::var("LANG").ok(); | ||
| /// The locale environment variables read by [`detect_system_locale`], in | ||
| /// the order it reads them. | ||
| const LOCALE_ENV: [&str; 3] = ["LC_ALL", "LC_MESSAGES", "LANG"]; |
There was a problem hiding this comment.
same list as in detect_system_locale, could be a single const at module level, no?
There was a problem hiding this comment.
Done, moved it to a module-level LOCALE_ENV_VARS const that both detect_system_locale and the tests use.
|
|
||
| // A variable that is set but empty counts as unset. | ||
| set_locale_env(&[("LC_ALL", ""), ("LANG", "fr_FR.UTF-8")]); | ||
| assert_eq!(detect_system_locale().unwrap().to_string(), "fr-FR"); |
There was a problem hiding this comment.
could you please also add a case with LC_ALL=C and LANG=fr_FR.UTF-8? it should give English
There was a problem hiding this comment.
Added. C and POSIX aren't valid language identifiers, so they fail to parse. Rather than relying on that, I map them to the default locale explicitly, which means LC_ALL=C wins over LANG and gives English. The test covers both.
| set_locale_env(&[("LC_ALL", ""), ("LANG", "fr_FR.UTF-8")]); | ||
| assert_eq!(detect_system_locale().unwrap().to_string(), "fr-FR"); | ||
|
|
||
| restore_locale_env(saved); |
There was a problem hiding this comment.
if one of the asserts fails, the env is never restored. maybe use a drop guard?
There was a problem hiding this comment.
Done, save_locale_env() now returns a guard that restores the variables when it goes out of scope.
|
|
||
| /// `LC_ALL` selects the message locale on its own, as it does for GNU. | ||
| #[test] | ||
| fn test_help_messages_french_translation_from_lc_all() { |
There was a problem hiding this comment.
this loops over every utility, so this test now takes 3x as long
could the LC_ALL/LC_MESSAGES cases check only one or two utilities?
There was a problem hiding this comment.
Done, the two new cases check two utilities each now.
a21e4a8 to
1f1e732
Compare
Merging this PR will regress 1 benchmark
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
|
GNU testsuite comparison: |
|
Tests are failing |
Thanks, fixed the two Rust test failures were locale-selection issues (switched to LC_ALL), and the four GNU regressions are an NLS baseline mismatch I've added to the ignore list. |
|
Why can't we have the same output as GNU on the tests, it would just be enough to patch the tests, as we do elsewhere, to restore the envvars ? |
141a884 to
b53e7a0
Compare
You're right, ignoring them was wrong. I've patched the tests instead the -mb cases now use LC_CTYPE=$mb_locale LC_MESSAGES=C with LC_ALL unset, so multibyte coverage is unchanged but the messages stay in C like the non-NLS reference. All four pass locally now. |
|
Good, please squash your commits |
8f7f4e4 to
c91e485
Compare
I've done. Please check. |
|
tests/ls/ls-time. tests/ls/ls-time failing |
c91e485 to
fc33394
Compare
Rebased onto main, tests/ls/ls-time passes now. |
|
CI is green and the branch is up to date with main as of my last rebase. Could you take another look when you get a chance? |
|
sorry but it needs to be rebased |
| tests_pwd-long.patch | ||
| tests_getopt_vs_usage.patch | ||
| tests_usage_vs_refs.patch | ||
| tests_cut_messages.patch |
There was a problem hiding this comment.
4 patches and ~300 lines for the same substitution, could it be a single sed in util/build-gnu.sh instead, no?
There was a problem hiding this comment.
Replaced the four patches with a single sed command in util/build-gnu.sh. All four GNU tests pass locally.
| .stderr_contains("\x1b[33m--invalid-option\x1b[0m"); // Yellow invalid option | ||
|
|
||
| // Test French localized colored error message | ||
| // Test French localized colored error message. The test harness pins |
There was a problem hiding this comment.
please make this comment shorter, one line is enough :)
There was a problem hiding this comment.
Shortened this to one line.
| new_ucmd!() | ||
| .arg("--verbo") | ||
| .env("LANG", "fr_FR") | ||
| // The test harness pins LC_ALL=C for every child, and LC_ALL takes |
There was a problem hiding this comment.
same comment as in test_chmod, could be dropped here
There was a problem hiding this comment.
Removed the duplicate comment.
| LocalizationError::ParseLocale(format!("Failed to parse locale: {locale_str}")) | ||
| let locale_env = LOCALE_ENV_VARS | ||
| .iter() | ||
| .find_map(|&key| std::env::var(key).ok().filter(|value| !value.is_empty())) |
There was a problem hiding this comment.
this is the same lookup as get_locale_from_env in features/i18n/mod.rs, could it be shared?
There was a problem hiding this comment.
Both functions now use get_locale_env_var(category). Added a regression test for category precedence and empty values.
| /// | ||
| /// Restoring on drop rather than at the end of the test means a failing | ||
| /// assertion cannot leave the process with the values the test set. | ||
| struct LocaleEnvGuard([(&'static str, Option<String>); 3]); |
There was a problem hiding this comment.
the doc comments on these test helpers are long, nobody will read them :)
could you please keep one line each?
There was a problem hiding this comment.
The tests now use temp_env, so the custom helpers and their doc comments have been removed.
fc33394 to
482b6ce
Compare
detect_system_locale() only read LANG, so LC_ALL and LC_MESSAGES were ignored: LC_ALL=fr_FR.UTF-8 ls --help stayed in English while LANG=fr_FR.UTF-8 was translated. Read the three variables in POSIX precedence order, and treat a variable that is set but empty as unset. The tests follow. The harness pins LC_ALL=C for every child, so the chmod and env French tests now select their locale with LC_ALL. The GNU multibyte tests set LC_ALL to a French UTF-8 locale and are compared against a reference GNU built without NLS, so they now run with LC_CTYPE set to that locale and LC_MESSAGES=C, which keeps the UTF-8 coverage and compares like an NLS-less GNU.
482b6ce to
64ea72e
Compare
Summary
LC_ALLandLC_MESSAGESwere ignored when selecting the locale for help and error messages: onlyLANGwas read, soLC_ALL=fr_FR.UTF-8 ls --helpstayed in English whileLANG=fr_FR.UTF-8 ls --helpwas translated. GNU honours all three, as reported in #8922.detect_system_locale()insrc/uucore/src/lib/mods/locale.rsnow readsLC_ALL, thenLC_MESSAGES, thenLANG, and treats a variable that is set but empty as unset. That is the POSIX precedence, and it matches howfeatures/i18n/mod.rsandcharmap.rsalready read the environment.docs/src/l10n.mdis updated to match.The old test set both
LANGandLC_ALLto French, so it passed whether the code read onlyLANGor preferredLC_ALL; it never separated the two cases, which is why the bug went unnoticed. The new tests set one variable each.Testing
A unit test pins the precedence itself (three different languages, then
LC_ALLremoved, thenLC_MESSAGES, plus the empty value), and an integration test runs the built binary with onlyLC_ALLset and checks that--helpis French. Reverting the fix makes both new tests fail and restoring it makes them pass, so they do catch the bug.Closes #8922