1+ import * as fs from "node:fs" ;
12import * as os from "node:os" ;
23import { type IpcMainInvokeEvent , ipcMain , type WebContents } from "electron" ;
34import * as pty from "node-pty" ;
@@ -38,13 +39,44 @@ export function registerShellIpc(): void {
3839
3940 const shell = getDefaultShell ( ) ;
4041 const homeDir = os . homedir ( ) ;
42+ let workingDir = cwd || homeDir ;
43+
44+ // Validate that the directory exists
45+ if ( ! fs . existsSync ( workingDir ) ) {
46+ console . warn (
47+ `Shell session ${ sessionId } : cwd "${ workingDir } " does not exist, falling back to home directory` ,
48+ ) ;
49+ workingDir = homeDir ;
50+ }
51+
52+ console . log (
53+ `Creating shell session ${ sessionId } : shell=${ shell } , cwd=${ workingDir } ` ,
54+ ) ;
55+
56+ // Build environment with proper locale settings for macOS
57+ const env = { ...process . env } as Record < string , string > ;
58+
59+ // On macOS, ensure locale is properly set for shell compatibility
60+ if ( os . platform ( ) === "darwin" && ! process . env . LC_ALL ) {
61+ const locale = process . env . LC_CTYPE || "en_US.UTF-8" ;
62+ env . LANG = locale ;
63+ env . LC_ALL = locale ;
64+ env . LC_MESSAGES = locale ;
65+ env . LC_NUMERIC = locale ;
66+ env . LC_COLLATE = locale ;
67+ env . LC_MONETARY = locale ;
68+ }
69+
70+ // Spawn as login shell to properly load PATH and environment
71+ const shellArgs = [ "-l" ] ;
4172
42- const ptyProcess = pty . spawn ( shell , [ ] , {
73+ const ptyProcess = pty . spawn ( shell , shellArgs , {
4374 name : "xterm-256color" ,
4475 cols : 80 ,
4576 rows : 24 ,
46- cwd : cwd || homeDir ,
47- env : process . env as Record < string , string > ,
77+ cwd : workingDir ,
78+ env,
79+ encoding : null ,
4880 } ) ;
4981
5082 // Send data to renderer
@@ -53,7 +85,10 @@ export function registerShellIpc(): void {
5385 } ) ;
5486
5587 // Handle exit
56- ptyProcess . onExit ( ( ) => {
88+ ptyProcess . onExit ( ( { exitCode, signal } ) => {
89+ console . error (
90+ `Shell session ${ sessionId } exited with code ${ exitCode } , signal ${ signal } ` ,
91+ ) ;
5792 event . sender . send ( `shell:exit:${ sessionId } ` ) ;
5893 sessions . delete ( sessionId ) ;
5994 } ) ;
0 commit comments