fix(opencode): correct PowerShell 5.1 quoting guidance - #42406
Open
JFast96 wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #42402
Type of change
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, doesthe wrong thing, and still exits 0.
The 5.1 shell notes told the agent to escape with the backtick, which makes it worse:
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?
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