Skip to content

fix(opencode): correct PowerShell 5.1 quoting guidance - #42406

Open
JFast96 wants to merge 1 commit into
anomalyco:devfrom
JFast96:powershell-quoting-notes
Open

fix(opencode): correct PowerShell 5.1 quoting guidance#42406
JFast96 wants to merge 1 commit into
anomalyco:devfrom
JFast96:powershell-quoting-notes

Conversation

@JFast96

@JFast96 JFast96 commented Aug 13, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #42402

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

PowerShell 5.1 doesn't escape double quotes when it builds the command line for an
external program. ssh host 'sed -i "s/\r$//" file' reaches the remote unquoted, does
the wrong thing, and still exits 0.

The 5.1 shell notes told the agent to escape with the backtick, which makes it worse:

plain            ["a b","c d"]      2 args
backtick         ["a ","b","c d"]   3 args
backslash \"     ["a \"b c\" d"]    correct
single quotes    ["a 'b c' d"]      correct

Replaced that line with the caveat and the two forms that survive. Only the 5.1 notes;
pwsh 7.3+ has $PSNativeCommandArgumentPassing = 'Standard' and isn't affected.

Not fixable in code: a PowerShell variable holding the correct string is still split
when passed to a native program, and -EncodedCommand doesn't help either.

How did you verify your code works?

node -e "console.log(JSON.stringify(process.argv.slice(1)))" 'a "b c" d'
# ["a b","c d"]   expected ["a \"b c\" d"]

The recommended forms are defined once in the test, then asserted to appear in the notes
and executed through powershell.exe, so the advice can't drift from what survives.
Verified on PowerShell 5.1.19041.7663. Windows only, skipped elsewhere.

Screenshots / recordings

n/a

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Windows PowerShell 5.1 does not escape double quotes when it builds the
command line for an external program. A double-quoted segment loses its
quotes and can split into several arguments, with no error and a success
exit code, so commands are silently corrupted rather than failing.

The 5.1 shell notes told the agent to "escape special characters with the
PowerShell backtick character". For this failure the backtick makes things
worse rather than better:

  plain             ["a b","c d"]      2 args, quotes gone
  backtick escape   ["a ","b","c d"]   3 args
  backslash escape  ["a \"b c\" d"]    correct
  single quotes     ["a 'b c' d"]      correct

Replace that line with the native argument passing caveat and the two
workarounds that were measured to survive. The backtick is still described
as correct for escaping inside PowerShell's own strings, which it is.

This is a PowerShell defect, not an opencode one. The command reaches
PowerShell intact; the corruption happens on the hop to the child process.
An in-memory PowerShell variable holding the string is still split when
passed to a native program, and -EncodedCommand does not help either, so
opencode cannot escape or encode its way out of it. Fixed upstream in
PowerShell 7.3 by $PSNativeCommandArgumentPassing = 'Standard', which does
not exist in 5.1. Shell.win() already prefers pwsh when it is installed, so
this only affects users without PowerShell 7+.

The pwsh notes are deliberately left unchanged.

Verified on Windows PowerShell 5.1.19041.7663 (Desktop):

  cd packages/opencode && bun test test/tool/shell.test.ts   74 pass, 0 fail
  cd packages/core     && bun test test/shell.test.ts        11 pass, 0 fail
  cd packages/opencode && bun typecheck                      exit 0
  cd packages/core     && bun typecheck                      exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shell notes for PowerShell 5.1 recommend backtick escaping, which corrupts commands

1 participant