From d7185808285bea1544286e1cc9cbf6b8b18191b2 Mon Sep 17 00:00:00 2001 From: Bharat Kaurav <117659673+TheNetherWatcher@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:30:51 +0530 Subject: [PATCH 1/4] uucore: use embedded English locale without probing the locales dir --- src/uucore/src/lib/mods/locale.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/uucore/src/lib/mods/locale.rs b/src/uucore/src/lib/mods/locale.rs index 90b13d53b3c..e30be58a7b7 100644 --- a/src/uucore/src/lib/mods/locale.rs +++ b/src/uucore/src/lib/mods/locale.rs @@ -618,6 +618,23 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> { if LOCALIZED_UTIL.with_borrow(|util| util.as_deref() == Some(p)) { return Ok(()); } + + let locale = detect_system_locale().unwrap_or_else(|_| { + LanguageIdentifier::from_str(DEFAULT_LOCALE).expect("Default locale should always be valid") + }); + + // English is embedded at build time, so there is no need to look for + // the locales directory (a readlink and several stats) or read files + if locale == DEFAULT_LOCALE + && get_embedded_locale(&format!("{p}/{DEFAULT_LOCALE}.ftl")).is_some() + { + set_localizer(Localizer::new(create_english_bundle_from_embedded( + &locale, p, + )?)); + LOCALIZED_UTIL.with_borrow_mut(|util| *util = Some(p.to_string())); + return Ok(()); + } + let locales_dir = get_locales_dir(p); // Callers such as the help template pass the invoked name (`dir`, `[`, // a prefixed binary name...), which has no strings of its own: keep the @@ -629,10 +646,6 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> { return Ok(()); } - let locale = detect_system_locale().unwrap_or_else(|_| { - LanguageIdentifier::from_str(DEFAULT_LOCALE).expect("Default locale should always be valid") - }); - // Load common strings along with utility-specific strings if let Ok(locales_dir) = locales_dir { // Load both utility-specific and common strings From 8ef60ef1cf4093c4b8ed00a83a5ea1254db2fade Mon Sep 17 00:00:00 2001 From: Bharat Kaurav <117659673+TheNetherWatcher@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:55:01 +0530 Subject: [PATCH 2/4] uucore: look up embedded utility strings once --- src/uucore/src/lib/mods/locale.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/uucore/src/lib/mods/locale.rs b/src/uucore/src/lib/mods/locale.rs index e30be58a7b7..ab323fc893a 100644 --- a/src/uucore/src/lib/mods/locale.rs +++ b/src/uucore/src/lib/mods/locale.rs @@ -625,9 +625,8 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> { // English is embedded at build time, so there is no need to look for // the locales directory (a readlink and several stats) or read files - if locale == DEFAULT_LOCALE - && get_embedded_locale(&format!("{p}/{DEFAULT_LOCALE}.ftl")).is_some() - { + let has_embedded_strings = get_embedded_locale(&format!("{p}/{DEFAULT_LOCALE}.ftl")).is_some(); + if locale == DEFAULT_LOCALE && has_embedded_strings { set_localizer(Localizer::new(create_english_bundle_from_embedded( &locale, p, )?)); @@ -639,9 +638,7 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> { // Callers such as the help template pass the invoked name (`dir`, `[`, // a prefixed binary name...), which has no strings of its own: keep the // utility that is already set up instead of switching to nothing - if LOCALIZED_UTIL.with_borrow(Option::is_some) - && locales_dir.is_err() - && get_embedded_locale(&format!("{p}/{DEFAULT_LOCALE}.ftl")).is_none() + if LOCALIZED_UTIL.with_borrow(Option::is_some) && locales_dir.is_err() && !has_embedded_strings { return Ok(()); } From 5738abd5271b030f593b7b67c590d8237924bd9b Mon Sep 17 00:00:00 2001 From: Bharat Kaurav <117659673+TheNetherWatcher@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:55:01 +0530 Subject: [PATCH 3/4] tests: check English runs do not touch locale files --- tests/test_localization_and_colors.rs | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_localization_and_colors.rs b/tests/test_localization_and_colors.rs index 58034835b96..fecbe94bead 100644 --- a/tests/test_localization_and_colors.rs +++ b/tests/test_localization_and_colors.rs @@ -305,3 +305,31 @@ fn test_french_colored_error_messages() { } } } + +/// English strings are embedded, so an English run must not look for or read +/// locale files (see #16862) +#[cfg(target_os = "linux")] +#[test] +fn test_english_does_not_touch_locales_dir() { + for utility in ["echo", "ls", "cksum"] { + let Ok(output) = Command::new("strace") + .args(["-qq", "-f", "-e", "trace=file"]) + .arg(uutests::util::get_tests_binary()) + .args([utility, "--help"]) + .env("LANG", "en_US.UTF-8") + .output() + else { + return; + }; + let trace = String::from_utf8_lossy(&output.stderr); + if !trace.contains('(') { + return; // strace unavailable, e.g. restricted ptrace in a container + } + let lookups: Vec<_> = trace.lines().filter(|l| l.contains("locales")).collect(); + assert!( + lookups.is_empty(), + "{utility} accessed locale files:\n{}", + lookups.join("\n") + ); + } +} From d0be592758da88e8dea569ec14d1391118e0b1b9 Mon Sep 17 00:00:00 2001 From: Bharat Kaurav <117659673+TheNetherWatcher@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:46:56 +0530 Subject: [PATCH 4/4] tests: run the locale strace check on true only --- tests/test_localization_and_colors.rs | 42 +++++++++++++-------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/tests/test_localization_and_colors.rs b/tests/test_localization_and_colors.rs index fecbe94bead..5954e8f5f38 100644 --- a/tests/test_localization_and_colors.rs +++ b/tests/test_localization_and_colors.rs @@ -307,29 +307,27 @@ fn test_french_colored_error_messages() { } /// English strings are embedded, so an English run must not look for or read -/// locale files (see #16862) -#[cfg(target_os = "linux")] +/// locale files +#[cfg(all(target_os = "linux", not(wasi_runner), feature = "true"))] #[test] fn test_english_does_not_touch_locales_dir() { - for utility in ["echo", "ls", "cksum"] { - let Ok(output) = Command::new("strace") - .args(["-qq", "-f", "-e", "trace=file"]) - .arg(uutests::util::get_tests_binary()) - .args([utility, "--help"]) - .env("LANG", "en_US.UTF-8") - .output() - else { - return; - }; - let trace = String::from_utf8_lossy(&output.stderr); - if !trace.contains('(') { - return; // strace unavailable, e.g. restricted ptrace in a container - } - let lookups: Vec<_> = trace.lines().filter(|l| l.contains("locales")).collect(); - assert!( - lookups.is_empty(), - "{utility} accessed locale files:\n{}", - lookups.join("\n") - ); + let Ok(output) = Command::new("strace") + .args(["-qq", "-f", "-e", "trace=file"]) + .arg(uutests::util::get_tests_binary()) + .args(["true", "--help"]) + .env("LANG", "en_US.UTF-8") + .output() + else { + return; + }; + let trace = String::from_utf8_lossy(&output.stderr); + if !trace.contains('(') { + return; // strace unavailable, e.g. restricted ptrace in a container } + let lookups: Vec<_> = trace.lines().filter(|l| l.contains("locales")).collect(); + assert!( + lookups.is_empty(), + "true accessed locale files:\n{}", + lookups.join("\n") + ); }