Files
orca/src/shared/powershell-native-argument.ts
T
3d7b306183 fix(terminal): submit startup commands with Enter on every platform (#23672)
* fix(terminal): submit startup commands with Enter on every platform

LF is Ctrl+J to a line editor, so a shell that remaps it left the command at the prompt.

Fixes #23250

Co-authored-by: Aashish Mahato <145881415+aashish254@users.noreply.github.com>

* test(terminal): expect CR in pty ipc startup tests

* fix(terminal): submit every line of a raw multiline startup command with CR

The CR switch only replaced the trailing terminator. On the raw path — a shell
with no bracketed paste, so each line break submits its own line — interior LFs
stayed LF, which is the same remappable Ctrl+J the fix set out to remove. A
multiline startup command in sh/dash/nushell/PowerShell therefore still stalled
partway through for a user who rebinds Ctrl+J. Normalise interior breaks to CR
on that path only; the bracketed-paste payload keeps its literal newlines.

* fix(terminal): quote multi-line PowerShell arguments on one line

A multi-line agent prompt was quoted as a single-quoted PowerShell string
with raw line breaks. Typed into Windows PowerShell 5.1 without PSReadLine,
each break submits a line and the shell waits at `>>` forever. Values with
CR/LF now become one double-quoted line with backtick escapes, keeping every
byte; single-line values keep the single-quoted form unchanged.

* style(terminal): keep typographic quotes as \u escapes in PowerShell quoting

Restores the escape form the single-quoted regex used on main and applies
it to the new double-quoted regex and the native argv test, so the diff
against main stays minimal and the characters are visible in review.

---------

Co-authored-by: Aashish Mahato <145881415+aashish254@users.noreply.github.com>
Co-authored-by: Neil <neil@stably.ai>
Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
2026-10-05 16:20:15 -07:00

27 lines
1.1 KiB
TypeScript

export function quotePowerShellLiteral(value: string): string {
if (/[\r\n]/.test(value)) {
return quotePowerShellMultilineLiteral(value)
}
// Why: PowerShell also ends single-quoted strings at typographic single quotes.
return `'${value.replace(/['\u2018\u2019\u201A\u201B]/g, '$&$&')}'`
}
/**
* Why one physical line: a raw line break typed into PowerShell submits the line, and Windows
* PowerShell 5.1 without PSReadLine then waits at `>>` for an empty line that never comes.
* Backtick escapes keep every byte, so `\r\n` stays `\r\n` exactly as the single-quoted form
* delivered it.
*/
function quotePowerShellMultilineLiteral(value: string): string {
const escaped = value.replace(/[`$"\u201C\u201D\u201E\r\n]/g, (char) =>
char === '\r' ? '`r' : char === '\n' ? '`n' : `\`${char}`
)
return `"${escaped}"`
}
export function quotePowerShellNativeArgument(value: string): string {
// Why: Windows PowerShell 5.1 drops unescaped embedded quotes when it
// constructs argv for native executables such as wsl.exe.
return quotePowerShellLiteral(value.replace(/(\\*)"/g, '$1$1\\"'))
}