Skip to content

forceClearScrollbackBuffer conflicts with focusConsoleOnExecute #2313

Open

Description

@ghost

Adding the following setting "powershell.integratedConsole.forceClearScrollbackBuffer":true causes the setting "powershell.integratedConsole.focusConsoleOnExecute": false to be ignored.

This means that the focus shifts to the terminal instead of staying inside the code window

System Details

System Details Output


Issue Description

I am experiencing a problem with keeping the focus on the code window instead of the terminal windows

Expected Behaviour

When i run/execute powershell code i expect the focus to stay in the code windows when this setting is added :
"powershell.integratedConsole.focusConsoleOnExecute": false

Actual Behaviour

The setting "powershell.integratedConsole.focusConsoleOnExecute": false is being ignored when i added "powershell.integratedConsole.forceClearScrollbackBuffer": true,, so everytime i execute code my cursor/marker is focused to the terminal windows.

Attached Logs

Follow the instructions in the troubleshooting docs
about capturing and sending logs.

Activity

  1. SydneyhSmith commented on Jul 28, 2020

    @SydneyhSmith
    Collaborator

    I am thinking Rain Sallow (/u/ta11ow) (@vexx32) was having this issue, but I cant seem to find the bug

  2. vexx32 commented on Jul 28, 2020

    @vexx32

    I don't think this was the same thing I was seeing, though my issue did also concern how the console focusing logic worked (it was revealing + focusing the wrong console when I had it hiding the integrated console by default and also running debug sessions in new consoles every time, forget the exact setting names but yeah).

  3. andyleejordan commented on Jun 16, 2026

    @andyleejordan
    Member

    Root cause and fix plan

    Picking this back up — the root cause is on the PowerShellEditorServices side, so I'm transferring this issue there to fix it. Summary of the investigation:

    Why it happens

    forceClearScrollbackBuffer exists because PSES's Clear-Host calls [Console]::Clear(), which clears the terminal viewport but not xterm.js's scrollback. To wipe scrollback, PSES round-trips an editor/clearTerminal notification to the extension, which runs workbench.action.terminal.clear. That workbench command always focuses the terminal — so every Clear-Host steals focus from the editor, even when focusConsoleOnExecute: false.

    Why we can't fix it cleanly in the extension

    The obvious client-side fix — "after clearing, restore focus to the editor only if an editor was focused" — isn't possible, because extension code can't read that condition:

    • window.activeTextEditor is "the one that currently has focus or, when none has focus, the one that changed input most recently" — so it stays defined when the terminal is focused; it can't distinguish editor-focused from terminal-focused.
    • TerminalState exposes only isInteractedWith + shell — no focus flag (and isInteractedWith is sticky).
    • window.activeTerminal is the active terminal, not the focused one.
    • terminalFocus / editorTextFocus are when-clause context keys only; there's no API to read a context key from code (CTRL+p stopped working microsoft/vscode#72647).
    • There's no Terminal.clear() and no focus-free clear command; only workbench.action.terminal.clear, which focuses.

    So the only client-side options are: restore focus unconditionally (regresses interactive Clear-Host/cls — typing it in the terminal now bounces focus to the editor) or do nothing (original bug). PR PowerShell/vscode-powershell#5519 took the unconditional route; that regression is why it's being closed.

    The fix (in PSES)

    Have PSES write the xterm "Erase Saved Lines" sequence (CSI 3 J = ESC[3J, i.e. \x1b[3J) directly to its own console output as part of Clear-Host, instead of round-tripping editor/clearTerminal. VS Code's integrated terminal is always xterm.js, which honors ESC[3J; the scrollback clears in-band with no focus change at all, in both the run-from-editor and the typed-in-terminal cases. No focus detection needed.

    This is safe because the extension↔PSES LSP runs over a named pipe/socket (not stdio), so PSES stdout is the terminal output and is separate from the LSP channel.

    Concrete plan:

    1. Map integratedConsole.forceClearScrollbackBuffer onto LanguageServerSettings (the extension already syncs the whole powershell config section via didChangeConfiguration).
    2. In Clear-Host / EditorOperationsService.ClearTerminal(), when that setting is true, write ESC[3J to the host output instead of sending editor/clearTerminal.
    3. In vscode-powershell (follow-up): remove the editor/clearTerminal handler once PSES lands; keep the forceClearScrollbackBuffer setting (now consumed server-side).

    Investigation drafted by Copilot for Andy Jordan (@andyleejordan).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions