From 4b48d42a0760313de4e6314a4c1532342d2b7f8b Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 19 Jul 2023 06:44:35 +0200 Subject: [PATCH 1/2] locale: set LANG to the fallback value if we have invalid locale See #201 for more information. Problem is that while we fall back to a default value to pass to `setlocale`, we don't set a `LANG` and instead reset it to `""`. What this does here is it changes the resetting to `""` and instead sets it to the default value. --- src/os/locale.zig | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/os/locale.zig b/src/os/locale.zig index bd46735e1..49900f061 100644 --- a/src/os/locale.zig +++ b/src/os/locale.zig @@ -35,16 +35,17 @@ pub fn ensureLocale() void { // If the locale is valid, we also use "" because LANG is already set. if (localeIsValid(new_lang)) break :locale ""; - // If the locale is not valid, and our lang changed, then we reset - // to the old value. - if (!std.mem.eql(u8, lang, new_lang)) { - if (setenv("LANG", lang.ptr, 1) < 0) { - log.err("error resetting locale env var", .{}); - } + // If the locale is not valid we fall back to en_US.UTF-8 and need to + // set LANG to this value too. + const default_locale = "en_US.UTF-8"; + log.info("LANG is not valid according to libc, will use en_US.UTF-8", .{}); + + if (setenv("LANG", default_locale.ptr, 1) < 0) { + log.err("error setting LANG env var to {s}", .{default_locale}); } - log.info("LANG is not valid according to libc, will use en_US.UTF-8", .{}); - break :locale "en_US.UTF-8"; + // Use it as locale + break :locale default_locale; }; // Set the locale From 5472d038322df79bad4a58874b95f2b6373dd218 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 19 Jul 2023 06:46:01 +0200 Subject: [PATCH 2/2] locale: remove default value for local dev env I commented this out to test something locally and it compiles & runs fine on macOS with Xcode. Looks like it's not needed anymore. --- src/os/locale.zig | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/os/locale.zig b/src/os/locale.zig index 49900f061..e6a354f76 100644 --- a/src/os/locale.zig +++ b/src/os/locale.zig @@ -65,20 +65,6 @@ fn localeIsValid(locale: []const u8) bool { /// This sets the LANG environment variable based on the macOS system /// preferences selected locale settings. fn setLangFromCocoa() void { - // Unknown Zig bug where in debug mode we can't pull the cocoa - // value without crashing so we just force it to en_US.UTF-8. - // Debug mode is only used for testing so to avoid this, devs can - // just set LANG manually! - if (builtin.mode == .Debug) { - log.warn("in debug mode, we always set LANG to en_US.UTF-8 if not set", .{}); - if (setenv("LANG", "en_US.UTF-8", 1) < 0) { - log.err("error setting locale env var", .{}); - return; - } - - return; - } - const pool = objc.AutoreleasePool.init(); defer pool.deinit();