Skip to content

Commit 36fe57b

Browse files
committed
fix(desktop): agent shells inherit a UTF-8 locale on macOS
GUI-launched apps inherit no LANG/LC_* from launchd, so spawned agent CLIs ran in the C locale and pbcopy decoded their UTF-8 output as MacRoman, corrupting any non-ASCII text copied to the clipboard. Harvest LANG/LC_ALL/LC_CTYPE from the login shell alongside the existing PATH and SSH_AUTH_SOCK, and fall back to C.UTF-8 on macOS when neither the process nor the login shell provides one.
1 parent c196f42 commit 36fe57b

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,85 @@ describe("DesktopShellEnvironment", () => {
152152
}),
153153
);
154154

155+
it.effect("hydrates the locale from the login shell on macOS", () =>
156+
Effect.gen(function* () {
157+
const env: NodeJS.ProcessEnv = {
158+
SHELL: "/bin/zsh",
159+
PATH: "/usr/bin",
160+
};
161+
162+
yield* runShellEnvironment({
163+
env,
164+
platform: "darwin",
165+
handler: () =>
166+
envOutput({
167+
PATH: "/opt/homebrew/bin:/usr/bin",
168+
LANG: "de_DE.UTF-8",
169+
}),
170+
});
171+
172+
assert.equal(env.LANG, "de_DE.UTF-8");
173+
}),
174+
);
175+
176+
it.effect("preserves an inherited locale over the login shell on macOS", () =>
177+
Effect.gen(function* () {
178+
const env: NodeJS.ProcessEnv = {
179+
SHELL: "/bin/zsh",
180+
PATH: "/usr/bin",
181+
LANG: "en_US.UTF-8",
182+
};
183+
184+
yield* runShellEnvironment({
185+
env,
186+
platform: "darwin",
187+
handler: () =>
188+
envOutput({
189+
PATH: "/opt/homebrew/bin:/usr/bin",
190+
LANG: "de_DE.UTF-8",
191+
}),
192+
});
193+
194+
assert.equal(env.LANG, "en_US.UTF-8");
195+
}),
196+
);
197+
198+
it.effect("falls back to C.UTF-8 when no locale is available on macOS", () =>
199+
Effect.gen(function* () {
200+
const env: NodeJS.ProcessEnv = {
201+
SHELL: "/bin/zsh",
202+
PATH: "/usr/bin",
203+
};
204+
205+
yield* runShellEnvironment({
206+
env,
207+
platform: "darwin",
208+
handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
209+
});
210+
211+
assert.equal(env.LANG, "C.UTF-8");
212+
assert.equal(env.LC_ALL, undefined);
213+
assert.equal(env.LC_CTYPE, undefined);
214+
}),
215+
);
216+
217+
it.effect("does not apply the locale fallback on linux", () =>
218+
Effect.gen(function* () {
219+
const env: NodeJS.ProcessEnv = {
220+
SHELL: "/bin/zsh",
221+
PATH: "/usr/bin",
222+
};
223+
224+
yield* runShellEnvironment({
225+
env,
226+
platform: "linux",
227+
handler: () => envOutput({ PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin" }),
228+
});
229+
230+
assert.equal(env.LANG, undefined);
231+
}),
232+
);
233+
155234
it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () =>
156235
Effect.gen(function* () {
157236
const env: NodeJS.ProcessEnv = {

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const LOGIN_SHELL_ENV_NAMES = [
7171
"PATH",
7272
"DBUS_SESSION_BUS_ADDRESS",
7373
"DISPLAY",
74+
"LANG",
75+
"LC_ALL",
76+
"LC_CTYPE",
7477
"SSH_AUTH_SOCK",
7578
"HOMEBREW_PREFIX",
7679
"HOMEBREW_CELLAR",
@@ -84,6 +87,8 @@ const LOGIN_SHELL_ENV_NAMES = [
8487
"WAYLAND_DISPLAY",
8588
] as const;
8689
const WINDOWS_PROFILE_ENV_NAMES = ["PATH", "FNM_DIR", "FNM_MULTISHELL_PATH"] as const;
90+
const LOCALE_ENV_NAMES = ["LANG", "LC_ALL", "LC_CTYPE"] as const;
91+
const FALLBACK_LOCALE = "C.UTF-8";
8792
const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
8893
const LOGIN_SHELL_TIMEOUT = Duration.seconds(5);
8994
const LAUNCHCTL_TIMEOUT = Duration.seconds(2);
@@ -462,6 +467,9 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
462467
"HOMEBREW_PREFIX",
463468
"HOMEBREW_CELLAR",
464469
"HOMEBREW_REPOSITORY",
470+
"LANG",
471+
"LC_ALL",
472+
"LC_CTYPE",
465473
"XDG_CONFIG_HOME",
466474
"XDG_DATA_HOME",
467475
"XDG_RUNTIME_DIR",
@@ -472,6 +480,17 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
472480
}
473481
}
474482

483+
// GUI launches inherit no locale from launchd, so spawned agents land in the C
484+
// locale and pbcopy decodes their UTF-8 output as MacRoman. C.UTF-8 is what
485+
// /etc/zprofile already falls back to: it fixes the codeset while keeping
486+
// C-stable collation and formatting, so output parsing is unaffected.
487+
if (
488+
config.platform === "darwin" &&
489+
LOCALE_ENV_NAMES.every((name) => Option.isNone(trimNonEmpty(config.env[name])))
490+
) {
491+
config.env.LANG = FALLBACK_LOCALE;
492+
}
493+
475494
if (
476495
config.platform === "linux" &&
477496
Option.isNone(trimNonEmpty(config.env.DBUS_SESSION_BUS_ADDRESS))

0 commit comments

Comments
 (0)