Skip to content

Commit cc136f2

Browse files
authored
fix: LC_ALL env should not be changed (electron#26507)
1 parent 968cb97 commit cc136f2

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

shell/browser/electron_browser_main_parts.cc

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/base_switches.h"
1212
#include "base/command_line.h"
1313
#include "base/feature_list.h"
14+
#include "base/optional.h"
1415
#include "base/path_service.h"
1516
#include "base/run_loop.h"
1617
#include "base/strings/string_number_conversions.h"
@@ -316,23 +317,34 @@ int ElectronBrowserMainParts::PreCreateThreads() {
316317
// which keys off of getenv("LC_ALL").
317318
// We must set this env first to make ui::ResourceBundle accept the custom
318319
// locale.
319-
g_setenv("LC_ALL", locale.c_str(), TRUE);
320+
std::unique_ptr<base::Environment> env(base::Environment::Create());
321+
base::Optional<std::string> lc_all;
322+
if (!locale.empty()) {
323+
std::string str;
324+
if (env->GetVar("LC_ALL", &str))
325+
lc_all.emplace(std::move(str));
326+
env->SetVar("LC_ALL", locale.c_str());
327+
}
320328
#endif
321329

322330
// Load resources bundle according to locale.
323331
std::string loaded_locale = LoadResourceBundle(locale);
324332

325-
#if defined(OS_LINUX)
326-
// Reset to the loaded locale if the custom locale is invalid.
327-
if (loaded_locale != locale)
328-
g_setenv("LC_ALL", loaded_locale.c_str(), TRUE);
329-
#endif
330-
331333
// Initialize the app locale.
332334
std::string app_locale = l10n_util::GetApplicationLocale(loaded_locale);
333335
ElectronBrowserClient::SetApplicationLocale(app_locale);
334336
fake_browser_process_->SetApplicationLocale(app_locale);
335337

338+
#if defined(OS_LINUX)
339+
// Reset to the original LC_ALL since we should not be changing it.
340+
if (!locale.empty()) {
341+
if (lc_all)
342+
env->SetVar("LC_ALL", *lc_all);
343+
else
344+
env->UnSetVar("LC_ALL");
345+
}
346+
#endif
347+
336348
// Force MediaCaptureDevicesDispatcher to be created on UI thread.
337349
MediaCaptureDevicesDispatcher::GetInstance();
338350

spec-main/chromium-spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,13 @@ describe('command line switches', () => {
303303
});
304304
describe('--lang switch', () => {
305305
const currentLocale = app.getLocale();
306-
const testLocale = async (locale: string, result: string) => {
306+
const testLocale = async (locale: string, result: string, printEnv: boolean = false) => {
307307
const appPath = path.join(fixturesPath, 'api', 'locale-check');
308-
const electronPath = process.execPath;
309-
appProcess = ChildProcess.spawn(electronPath, [appPath, `--set-lang=${locale}`]);
308+
const args = [appPath, `--set-lang=${locale}`];
309+
if (printEnv) {
310+
args.push('--print-env');
311+
}
312+
appProcess = ChildProcess.spawn(process.execPath, args);
310313

311314
let output = '';
312315
appProcess.stdout.on('data', (data) => { output += data; });
@@ -318,6 +321,15 @@ describe('command line switches', () => {
318321

319322
it('should set the locale', async () => testLocale('fr', 'fr'));
320323
it('should not set an invalid locale', async () => testLocale('asdfkl', currentLocale));
324+
325+
const lcAll = String(process.env.LC_ALL);
326+
ifit(process.platform === 'linux')('current process has a valid LC_ALL env', async () => {
327+
// The LC_ALL env should not be set to DOM locale string.
328+
expect(lcAll).to.not.equal(app.getLocale());
329+
});
330+
ifit(process.platform === 'linux')('should not change LC_ALL', async () => testLocale('fr', lcAll, true));
331+
ifit(process.platform === 'linux')('should not change LC_ALL when setting invalid locale', async () => testLocale('asdfkl', lcAll, true));
332+
ifit(process.platform === 'linux')('should not change LC_ALL when --lang is not set', async () => testLocale('', lcAll, true));
321333
});
322334

323335
describe('--remote-debugging-pipe switch', () => {

spec/fixtures/api/locale-check/main.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
const { app } = require('electron');
22

33
const locale = process.argv[2].substr(11);
4-
app.commandLine.appendSwitch('lang', locale);
4+
if (locale.length !== 0) {
5+
app.commandLine.appendSwitch('lang', locale);
6+
}
57

68
app.whenReady().then(() => {
7-
process.stdout.write(app.getLocale());
9+
if (process.argv[3] === '--print-env') {
10+
process.stdout.write(String(process.env.LC_ALL));
11+
} else {
12+
process.stdout.write(app.getLocale());
13+
}
814
process.stdout.end();
915

1016
app.quit();

0 commit comments

Comments
 (0)