fix: initialize xterm.js before connecting WebSocket

Critical fix for completely broken terminal (blinking cursor, no input).

Root Cause:
The WebSocket was being connected before xterm.js was initialized,
causing the WebSocket reference to be lost because this.terminals
map didn't have the entry yet.

Broken Flow:
1. createTerminalUI() - Creates DOM elements only
2. connectTerminal() - Tries to store ws in terminal, but terminal = null
3. WebSocket reference lost immediately
4. No input/output possible

Fixed Flow:
1. createTerminalUI() - Creates DOM elements
2. initializeXTerm() - Creates xterm.js instance AND stores in map
3. connectTerminal() - Now finds terminal in map, stores ws successfully
4. switchToTerminal() - Terminal works!

Changes:
- Add explicit initializeXTerm() call before connectTerminal()
- Add error checking in connectTerminal() to verify terminal exists
- Add comments enforcing ordering requirements

Resolves: "All I see is blinging | and nothing else, no claude cli,
no commands, i cant type in anything. terminal is not woring."

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
uroma
2026-01-19 18:43:20 +00:00
Unverified
parent f57b01c82b
commit 8c448315c1

View File

@@ -213,7 +213,11 @@ class TerminalManager {
// Create terminal UI // Create terminal UI
await this.createTerminalUI(terminalId, selectedDir, mode); await this.createTerminalUI(terminalId, selectedDir, mode);
// Connect WebSocket // Initialize xterm.js FIRST (before connecting WebSocket)
// This ensures this.terminals map has the entry ready
await this.initializeXTerm(terminalId);
// NOW connect WebSocket (terminal entry exists in map)
await this.connectTerminal(terminalId); await this.connectTerminal(terminalId);
// Switch to new terminal // Switch to new terminal
@@ -773,6 +777,19 @@ class TerminalManager {
ws.onopen = () => { ws.onopen = () => {
console.log(`[TerminalManager] Connected to terminal ${terminalId}`); console.log(`[TerminalManager] Connected to terminal ${terminalId}`);
// Store WebSocket in terminal entry
// NOTE: This assumes initializeXTerm() has already been called
// and this.terminals has the entry
const terminal = this.terminals.get(terminalId);
if (terminal) {
terminal.ws = ws;
} else {
console.error(`[TerminalManager] CRITICAL: Terminal ${terminalId} not found in map! WebSocket connection will be lost.`);
reject(new Error(`Terminal ${terminalId} not initialized`));
return;
}
resolve(); resolve();
}; };
@@ -789,12 +806,6 @@ class TerminalManager {
ws.onclose = () => { ws.onclose = () => {
console.log(`[TerminalManager] WebSocket closed for terminal ${terminalId}`); console.log(`[TerminalManager] WebSocket closed for terminal ${terminalId}`);
}; };
// Store WebSocket
const terminal = this.terminals.get(terminalId);
if (terminal) {
terminal.ws = ws;
}
} catch (error) { } catch (error) {
console.error('[TerminalManager] Error connecting WebSocket:', error); console.error('[TerminalManager] Error connecting WebSocket:', error);
reject(error); reject(error);
@@ -908,11 +919,12 @@ class TerminalManager {
this.sendTerminalResize(terminalId, cols, rows); this.sendTerminalResize(terminalId, cols, rows);
}); });
// Store terminal instance // Store terminal instance in map
// This MUST happen before connectTerminal() is called
this.terminals.set(terminalId, { this.terminals.set(terminalId, {
terminal, terminal,
fitAddon, fitAddon,
ws: null, ws: null, // Will be set by connectTerminal()
container, container,
mode: 'mixed' mode: 'mixed'
}); });