fix: default LANG to UTF-8 for terminal character display (#423)

* fix: address review findings

* fix: improve error handling in terminal e2e helper JSON parsing

* fix: remove unused catch parameter in terminal e2e helper

* fix: use nullish coalescing assignment for LANG default in PTY spawn

The object spread approach ({LANG: default, ...process.env}) produced
undefined on CI (Ubuntu/Node 22) despite working locally. Switch to
explicit ??= assignment after the spread for reliable cross-platform
behavior.
This commit is contained in:
Jinjing
2026-04-09 14:02:02 -07:00
committed by GitHub
parent df54597cc3
commit c908bdfbb2
3 changed files with 313 additions and 3 deletions
+65 -3
View File
@@ -81,6 +81,70 @@ describe('registerPtyHandlers', () => {
})
})
/** Helper: trigger pty:spawn and return the env passed to node-pty. */
function spawnAndGetEnv(
argsEnv?: Record<string, string>,
processEnvOverrides?: Record<string, string | undefined>
): Record<string, string> {
const savedEnv: Record<string, string | undefined> = {}
if (processEnvOverrides) {
for (const [k, v] of Object.entries(processEnvOverrides)) {
savedEnv[k] = process.env[k]
if (v === undefined) {
delete process.env[k]
} else {
process.env[k] = v
}
}
}
try {
// Clear previously registered handlers so re-registration doesn't
// accumulate stale state across calls within one test.
handlers.clear()
registerPtyHandlers(mainWindow as never)
handlers.get('pty:spawn')!(null, {
cols: 80,
rows: 24,
...(argsEnv ? { env: argsEnv } : {})
})
const spawnCall = spawnMock.mock.calls.at(-1)!
return spawnCall[2].env as Record<string, string>
} finally {
for (const [k, v] of Object.entries(savedEnv)) {
if (v === undefined) {
delete process.env[k]
} else {
process.env[k] = v
}
}
}
}
describe('spawn environment', () => {
it('defaults LANG to en_US.UTF-8 when not inherited from process.env', () => {
const env = spawnAndGetEnv(undefined, { LANG: undefined })
expect(env.LANG).toBe('en_US.UTF-8')
})
it('inherits LANG from process.env when already set', () => {
const env = spawnAndGetEnv(undefined, { LANG: 'ja_JP.UTF-8' })
expect(env.LANG).toBe('ja_JP.UTF-8')
})
it('lets caller-provided env override LANG', () => {
const env = spawnAndGetEnv({ LANG: 'fr_FR.UTF-8' })
expect(env.LANG).toBe('fr_FR.UTF-8')
})
it('always sets TERM and COLORTERM regardless of env', () => {
const env = spawnAndGetEnv()
expect(env.TERM).toBe('xterm-256color')
expect(env.COLORTERM).toBe('truecolor')
expect(env.TERM_PROGRAM).toBe('Orca')
})
})
it('rejects missing WSL worktree cwd instead of validating only the fallback Windows cwd', () => {
const originalPlatform = process.platform
const originalUserProfile = process.env.USERPROFILE
@@ -107,9 +171,7 @@ describe('registerPtyHandlers', () => {
rows: 24,
cwd: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\missing'
})
).toThrow(
'Working directory "\\\\wsl.localhost\\Ubuntu\\home\\jin\\missing" does not exist.'
)
).toThrow('Working directory "\\\\wsl.localhost\\Ubuntu\\home\\jin\\missing" does not exist.')
expect(spawnMock).not.toHaveBeenCalled()
} finally {
Object.defineProperty(process, 'platform', {
+8
View File
@@ -164,6 +164,14 @@ export function registerPtyHandlers(mainWindow: BrowserWindow, runtime?: OrcaRun
FORCE_HYPERLINK: '1'
} as Record<string, string>
// Why: When Electron is launched from Finder (not a terminal), the process
// does not inherit the user's shell locale settings. Without an explicit
// UTF-8 locale, multi-byte characters (e.g. em dashes U+2014) are
// misinterpreted by the PTY and rendered as garbled sequences like "�~@~T".
// We default LANG to en_US.UTF-8 but let the inherited or caller-provided
// env override it so user locale preferences are respected.
spawnEnv.LANG ??= 'en_US.UTF-8'
let ptyProcess: pty.IPty | undefined
try {
ptyProcess = pty.spawn(shellPath, shellArgs, {