From dc2924db173b079defbd0f1ddaa7b60f5f671bb5 Mon Sep 17 00:00:00 2001 From: zhzhsx Date: Tue, 24 Nov 2026 00:09:43 +0800 Subject: [PATCH] fix(terminal): send the final size to a newly started PTY (#5473) A new project's first terminal can end up with the wrong size. While the shell is starting, the terminal may resize, for example when the font finishes loading. That new size never reaches the shell. Full-screen programs such as vim then draw at the old width and the text looks broken. This fix sends the current size once the shell is ready. Co-authored-by: zhzhsx <1379025+zhzhsx@users.noreply.github.com> --- src/features/terminal/session.test.ts | 52 +++++++++++++++++++++++++++ src/features/terminal/session.ts | 2 ++ 2 files changed, 54 insertions(+) diff --git a/src/features/terminal/session.test.ts b/src/features/terminal/session.test.ts index d93f6e92f..be2845a16 100644 --- a/src/features/terminal/session.test.ts +++ b/src/features/terminal/session.test.ts @@ -389,6 +389,58 @@ describe("terminal restart", () => { }); }); +describe("terminal startup size", () => { + beforeEach(() => vi.clearAllMocks()); + + it("resizes a freshly spawned PTY to the grid reached while startup was pending", async () => { + let release: ((value: { id: number; title: string }) => void) | undefined; + ipc.commands.termSpawn.mockImplementationOnce( + () => + new Promise<{ id: number; title: string }>((resolve) => { + release = resolve; + }), + ); + const session = new TerminalSession({ key: "s-start-size", cwd: "/repo", onChange() {}, onFocus() {} }); + try { + session.term.resize(80, 24); + const starting = session.start(); + await vi.waitFor(() => expect(release).toBeTypeOf("function")); + expect(ipc.commands.termSpawn).toHaveBeenCalledWith( + expect.objectContaining({ cols: 80, rows: 24 }), + expect.anything(), + ); + // Resize while startup is still pending. + session.term.resize(100, 30); + release!({ id: 12, title: "zsh" }); + await starting; + expect(ipc.commands.termResize).toHaveBeenCalledWith(12, 100, 30); + } finally { + session.dispose(); + } + }); + + it("leaves the PTY alone when the grid did not change during startup", async () => { + let release: ((value: { id: number; title: string }) => void) | undefined; + ipc.commands.termSpawn.mockImplementationOnce( + () => + new Promise<{ id: number; title: string }>((resolve) => { + release = resolve; + }), + ); + const session = new TerminalSession({ key: "s-start-same", cwd: "/repo", onChange() {}, onFocus() {} }); + try { + session.term.resize(80, 24); + const starting = session.start(); + await vi.waitFor(() => expect(release).toBeTypeOf("function")); + release!({ id: 13, title: "zsh" }); + await starting; + expect(ipc.commands.termResize).not.toHaveBeenCalled(); + } finally { + session.dispose(); + } + }); +}); + describe("terminal open", () => { afterEach(() => vi.useRealTimers()); diff --git a/src/features/terminal/session.ts b/src/features/terminal/session.ts index a5db0dd03..12bfb6954 100644 --- a/src/features/terminal/session.ts +++ b/src/features/terminal/session.ts @@ -858,6 +858,8 @@ export class TerminalSession { } this.ptyId = info.id; this.update({ status: "running", title: info.title || "Local" }); + // Send the new size if it changed while the program was starting. + if (this.term.cols !== cols || this.term.rows !== rows) this.resizePty(this.term.cols, this.term.rows); this.flushPendingInput(); } catch (e) { this.pendingInput = "";