Skip to content

Commit 1eed734

Browse files
committed
fix(terminal): clear profile override when Inline Terminal is enabled (Zoo-Code-Org#321)
- BaseTerminal.ts: clear terminalProfile in setShellIntegrationDisabled when disabled=true, preventing a ghost override from surviving the Inline Terminal mode switch via the backend static-field path. - TerminalSettings.tsx: add useEffect with useRef to clear terminalProfile on the true→false transition of isVSCodeTerminalEnabled, so React state, the extension host, and persisted settings are all updated before the execa path next reads getShell(). - TerminalProfile.spec.ts: add tests for both clearing (disabled=true) and non-clearing (disabled=false) paths; add setShellIntegrationDisabled(false) to afterEach for test isolation. - TerminalSettings.profile.spec.tsx: add renderWithInlineOff helper and three tests covering positive case (clears on mode switch), negative case (does not clear on VS Code mode), and nil-profile case (does not error when profile is already undefined).
1 parent b564140 commit 1eed734

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎src/integrations/terminal/BaseTerminal.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ export abstract class BaseTerminal implements RooTerminal {
181181

182182
public static setShellIntegrationDisabled(disabled: boolean): void {
183183
BaseTerminal.shellIntegrationDisabled = disabled
184+
if (disabled) {
185+
BaseTerminal.terminalProfile = undefined
186+
}
184187
}
185188

186189
public static getShellIntegrationDisabled(): boolean {

‎src/integrations/terminal/__tests__/TerminalProfile.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe("Terminal VS Code terminal profile (#277)", () => {
7878

7979
afterEach(() => {
8080
Terminal.setTerminalProfile(undefined)
81+
Terminal.setShellIntegrationDisabled(false)
8182
vi.restoreAllMocks()
8283
})
8384

@@ -99,6 +100,24 @@ describe("Terminal VS Code terminal profile (#277)", () => {
99100
Terminal.setTerminalProfile(" ")
100101
expect(Terminal.getTerminalProfile()).toBeUndefined()
101102
})
103+
104+
describe("setShellIntegrationDisabled", () => {
105+
it("clears terminalProfile when Inline Terminal is enabled", () => {
106+
Terminal.setTerminalProfile("Git Bash")
107+
expect(Terminal.getTerminalProfile()).toBe("Git Bash")
108+
109+
Terminal.setShellIntegrationDisabled(true)
110+
expect(Terminal.getTerminalProfile()).toBeUndefined()
111+
})
112+
113+
it("does not clear terminalProfile when VS Code Terminal mode is selected", () => {
114+
Terminal.setTerminalProfile("Git Bash")
115+
expect(Terminal.getTerminalProfile()).toBe("Git Bash")
116+
117+
Terminal.setShellIntegrationDisabled(false)
118+
expect(Terminal.getTerminalProfile()).toBe("Git Bash")
119+
})
120+
})
102121
})
103122

104123
describe("getConfiguredProfiles / getAvailableProfileNames", () => {

‎webview-ui/src/components/settings/TerminalSettings.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HTMLAttributes, useState, useCallback, useEffect, useId } from "react"
1+
import { HTMLAttributes, useState, useCallback, useEffect, useId, useRef } from "react"
22
import { useAppTranslation } from "@/i18n/TranslationContext"
33
import { vscode } from "@/utils/vscode"
44
import { VSCodeCheckbox, VSCodeLink, VSCodeButton } from "@vscode/webview-ui-toolkit/react"
@@ -106,6 +106,19 @@ export const TerminalSettings = ({
106106
}
107107
}, [isProfilesLoaded, profileNames, setCachedStateField, terminalProfile])
108108

109+
// Defense in depth: the backend also clears the profile when Inline
110+
// Terminal is enabled (BaseTerminal.setShellIntegrationDisabled), but
111+
// we clear here for immediate UI consistency — so React state, the
112+
// extension host, and persisted settings are all updated before the
113+
// execa path next reads getShell().
114+
const prevVSCodeTerminalEnabled = useRef(isVSCodeTerminalEnabled)
115+
useEffect(() => {
116+
if (prevVSCodeTerminalEnabled.current && !isVSCodeTerminalEnabled) {
117+
setCachedStateField("terminalProfile", undefined)
118+
}
119+
prevVSCodeTerminalEnabled.current = isVSCodeTerminalEnabled
120+
}, [isVSCodeTerminalEnabled, setCachedStateField])
121+
109122
return (
110123
<div className={cn("flex flex-col", className)} {...props}>
111124
<SectionHeader>{t("settings:sections.terminal")}</SectionHeader>

‎webview-ui/src/components/settings/__tests__/TerminalSettings.profile.spec.tsx‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ describe("TerminalSettings VS Code terminal profile (#277)", () => {
9090
return { onTerminalProfilePickerOpened, setCachedStateField }
9191
}
9292

93+
// Render with Inline Terminal OFF (VS Code Terminal mode), with an active override.
94+
const renderWithInlineOff = (terminalProfile?: string) => {
95+
const setCachedStateField = vi.fn()
96+
const onTerminalProfilePickerOpened = vi.fn()
97+
const { rerender } = render(
98+
<TerminalSettings
99+
terminalShellIntegrationDisabled={false}
100+
terminalProfile={terminalProfile}
101+
onTerminalProfilePickerOpened={onTerminalProfilePickerOpened}
102+
setCachedStateField={setCachedStateField}
103+
/>,
104+
)
105+
const rerenderWithInlineOn = () => {
106+
rerender(
107+
<TerminalSettings
108+
terminalShellIntegrationDisabled={true}
109+
terminalProfile={terminalProfile}
110+
onTerminalProfilePickerOpened={onTerminalProfilePickerOpened}
111+
setCachedStateField={setCachedStateField}
112+
/>,
113+
)
114+
}
115+
return { setCachedStateField, rerender: rerenderWithInlineOn }
116+
}
117+
93118
it("requests the terminal profile names on mount via the allowlisted message", () => {
94119
setup()
95120
const types = postMessageMock.mock.calls.map((c) => c[0]?.type)
@@ -228,4 +253,49 @@ describe("TerminalSettings VS Code terminal profile (#277)", () => {
228253
expect(overrideRadio).not.toBeDisabled()
229254
expect(screen.queryByTestId("terminal-profile-no-profiles-hint")).not.toBeInTheDocument()
230255
})
256+
257+
it("clears terminalProfile when Inline Terminal is enabled (mode switch)", () => {
258+
const { setCachedStateField, rerender } = renderWithInlineOff("Git Bash")
259+
260+
// Simulate the user checking "Use Inline Terminal"
261+
rerender()
262+
263+
expect(setCachedStateField).toHaveBeenCalledWith("terminalProfile", undefined)
264+
})
265+
266+
it("does not clear terminalProfile when switching from Inline back to VS Code Terminal", () => {
267+
// Start with Inline Terminal ON, no profile override
268+
const setCachedStateField = vi.fn()
269+
const onTerminalProfilePickerOpened = vi.fn()
270+
const { rerender } = render(
271+
<TerminalSettings
272+
terminalShellIntegrationDisabled={true}
273+
terminalProfile={undefined}
274+
onTerminalProfilePickerOpened={onTerminalProfilePickerOpened}
275+
setCachedStateField={setCachedStateField}
276+
/>,
277+
)
278+
279+
// Switch to VS Code Terminal mode
280+
rerender(
281+
<TerminalSettings
282+
terminalShellIntegrationDisabled={false}
283+
terminalProfile={undefined}
284+
onTerminalProfilePickerOpened={onTerminalProfilePickerOpened}
285+
setCachedStateField={setCachedStateField}
286+
/>,
287+
)
288+
289+
expect(setCachedStateField).not.toHaveBeenCalledWith("terminalProfile", undefined)
290+
})
291+
292+
it("does not error when clearing an already-undefined profile", () => {
293+
const { setCachedStateField, rerender } = renderWithInlineOff(undefined)
294+
295+
// Switch to Inline Terminal with no override set
296+
rerender()
297+
298+
// Should still work — no crash, no unexpected calls
299+
expect(setCachedStateField).toHaveBeenCalledWith("terminalProfile", undefined)
300+
})
231301
})

0 commit comments

Comments
 (0)