Skip to content

uucore/locale: honour LC_ALL and LC_MESSAGES when picking the locale - #14870

Open
sunsss123soldier wants to merge 1 commit into
uutils:mainfrom
sunsss123soldier:locale-honour-lc-all
Open

sunsss123soldier wants to merge 1 commit into
uutils:mainfrom
sunsss123soldier:locale-honour-lc-all

Conversation

@sunsss123soldier

Copy link
Copy Markdown
Contributor

Summary

LC_ALL and LC_MESSAGES were ignored when selecting the locale for help and error messages: only LANG was read, so LC_ALL=fr_FR.UTF-8 ls --help stayed in English while LANG=fr_FR.UTF-8 ls --help was translated. GNU honours all three, as reported in #8922.
detect_system_locale() in src/uucore/src/lib/mods/locale.rs now reads LC_ALL, then LC_MESSAGES, then LANG, and treats a variable that is set but empty as unset. That is the POSIX precedence, and it matches how features/i18n/mod.rs and charmap.rs already read the environment. docs/src/l10n.md is updated to match.
The old test set both LANG and LC_ALL to French, so it passed whether the code read only LANG or preferred LC_ALL; it never separated the two cases, which is why the bug went unnoticed. The new tests set one variable each.

Testing

cargo test -p uucore locale                     # 38 passed
cargo test --test test_localization_and_colors  # 7 passed
cargo fmt --all --check                         # clean
cargo test -p coreutils --test tests            # on Windows: same failure set as before the change

A unit test pins the precedence itself (three different languages, then LC_ALL removed, then LC_MESSAGES, plus the empty value), and an integration test runs the built binary with only LC_ALL set and checks that --help is French. Reverting the fix makes both new tests fail and restoring it makes them pass, so they do catch the bug.
Closes #8922

Comment thread src/uucore/src/lib/mods/locale.rs Outdated
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"];

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.

same list as in detect_system_locale, could be a single const at module level, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, moved it to a module-level LOCALE_ENV_VARS const that both detect_system_locale and the tests use.

Comment thread src/uucore/src/lib/mods/locale.rs Outdated

// 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");

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.

could you please also add a case with LC_ALL=C and LANG=fr_FR.UTF-8? it should give English

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread src/uucore/src/lib/mods/locale.rs Outdated
set_locale_env(&[("LC_ALL", ""), ("LANG", "fr_FR.UTF-8")]);
assert_eq!(detect_system_locale().unwrap().to_string(), "fr-FR");

restore_locale_env(saved);

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.

if one of the asserts fails, the env is never restored. maybe use a drop guard?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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() {

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.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, the two new cases check two utilities each now.

@codspeed

codspeed Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 1 benchmark

⚡ 4 improved benchmarks
❌ 1 regressed benchmark
✅ 384 untouched benchmarks
⏩ 54 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation five_38_bit_primes 1.8 s 1.9 s -3.57%
⚡ Simulation three_39_bit_primes 544.2 ms 393.1 ms +38.45%
⚡ Simulation unexpand_large_file[10] 286.2 ms 267.5 ms +7.01%
⚡ Simulation unexpand_many_lines[100000] 136.9 ms 128 ms +6.99%
⚡ Simulation thirteen_39_bit_primes 9.4 s 9.1 s +3.45%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing sunsss123soldier:locale-honour-lc-all (fc33394) with main (855fbba)2

Open in CodSpeed

Footnotes

  1. 54 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 (7f71ad5) during the generation of this report, so 855fbba was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@github-actions

github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/ls/ls-time. tests/ls/ls-time is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)

@anastygnome

Copy link
Copy Markdown
Contributor

Tests are failing

@sunsss123soldier

Copy link
Copy Markdown
Contributor Author

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.

@anastygnome

anastygnome commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

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 ?

@sunsss123soldier

Copy link
Copy Markdown
Contributor Author

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 ?

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.

@anastygnome

Copy link
Copy Markdown
Contributor

Good, please squash your commits

@sunsss123soldier

Copy link
Copy Markdown
Contributor Author

Good, please squash your commits

I've done. Please check.

@anastygnome

Copy link
Copy Markdown
Contributor

tests/ls/ls-time. tests/ls/ls-time failing

@sunsss123soldier

Copy link
Copy Markdown
Contributor Author

tests/ls/ls-time. tests/ls/ls-time failing

Rebased onto main, tests/ls/ls-time passes now.

@sunsss123soldier

Copy link
Copy Markdown
Contributor Author

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?

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased

Comment thread util/gnu-patches/series Outdated
tests_pwd-long.patch
tests_getopt_vs_usage.patch
tests_usage_vs_refs.patch
tests_cut_messages.patch

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.

4 patches and ~300 lines for the same substitution, could it be a single sed in util/build-gnu.sh instead, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced the four patches with a single sed command in util/build-gnu.sh. All four GNU tests pass locally.

Comment thread tests/by-util/test_chmod.rs Outdated
.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

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.

please make this comment shorter, one line is enough :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Shortened this to one line.

Comment thread tests/by-util/test_env.rs Outdated
new_ucmd!()
.arg("--verbo")
.env("LANG", "fr_FR")
// The test harness pins LC_ALL=C for every child, and LC_ALL takes

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.

same comment as in test_chmod, could be dropped here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the duplicate comment.

Comment thread src/uucore/src/lib/mods/locale.rs Outdated
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()))

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.

this is the same lookup as get_locale_from_env in features/i18n/mod.rs, could it be shared?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Both functions now use get_locale_env_var(category). Added a regression test for category precedence and empty values.

Comment thread src/uucore/src/lib/mods/locale.rs Outdated
///
/// 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]);

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.

the doc comments on these test helpers are long, nobody will read them :)
could you please keep one line each?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The tests now use temp_env, so the custom helpers and their doc comments have been removed.

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.

This branch has not been deployed

No deployments
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.

l10n: LC_ALL is ignored

3 participants