Repository navigation
Conversation
…gle/zx#1552 Signed-off-by: Aryan Sachin <aryansachinixd@gmail.com>
1AbdullahButt
left a comment
There was a problem hiding this comment.
This fixes quoted executable paths, but it also changes commands that start with a quoted value instead of a program, which work today. PowerShell then reports success for them.
The issue: a leading quoted string isn't always a command. The new check in fullCmd (src/core.ts:520-531) adds & whenever the command starts with ' or ". In PowerShell, a quoted string at the start of a statement is a value, and that's a common pattern with interpolation, e.g. $`${text} | Set-Content out.txt` or $`${json} | ConvertFrom-Json`. quotePowerShell quotes any value with a space or backslash, so these start with '...' today. I ran both forms in Windows PowerShell 5.1:
'some text' | Write-Outputprintssome text& 'some text' | Write-OutputgivesThe term 'some text' is not recognized as the name of a cmdlet...
The exit code is worse. A CommandNotFoundException doesn't set $LastExitCode, so with zx's postfix ; exit $LastExitCode the process still exits 0 (I got exit=0 for the second form). The command fails, but zx reports success.
Suggested fix: syntax alone can't settle this. I tried requiring an argument after the quoted token (/^\s*('(?:[^']|'')*'|"[^"]*")\s+(?![\s|;>)&]|$)/). That leaves 'text' | … and a bare 'text' alone, but still matches 'a b' -replace 'a','b', because a PowerShell operator looks like a flag. Keying on the value works better: add & only when the leading token, unquoted, is an existing file or ends in .exe/.cmd/.bat/.ps1. Or leave it to the caller ($& ${path} -v) and document that. Either way, a test with `$`${'a b'} | Write-Output next to the new path test would pin the pipeline case.
What I checked that is fine:
- The bug is real for any Windows path.
quotePowerShellonly leaves[\w/.\-@:=,+%]unquoted, so a backslash alone forces quoting.'C:\Program Files\PowerShell\x.exe' --versionfails withUnexpected token 'version'in my PowerShell, as in #1551. - The shell regex matches what
whichreturns.usePwsh()gives a path like...\pwsh.EXE, and theiflag covers it.usePowerShell()/usePwsh()set$.prefix = '', so the!prefix.trim().startsWith('&')check doesn't get in the way by default.
How I tested: the PowerShell commands ran directly in Windows PowerShell 5.1. The zx side is from reading the branch. I didn't run zx's tests.
Reviewed with help from Claude Code.
…oogle#1551) In PowerShell, passing a quoted command or executable path at the start of a statement causes PowerShell to evaluate the first token as a string literal expression rather than invoking it as a command, throwing 'Unexpected token in expression or statement'. However, a leading quoted string can also be an interpolated value piped to another command (e.g. $${text} | Set-Content ...) or used with an operator (e.g. $${text} -replace 'a','b'). In those value cases, prepending the call operator '&' would cause CommandNotFoundException. Update fullCmd to inspect the leading quoted token and only prepend the call operator '&' when the token represents an executable or script (ends in .exe/.cmd/.bat/.ps1/.com or exists on disk as a file), preserving normal pipeline and operator expressions. Closes google#1551 Signed-off-by: Aryan Sachin <aryansachinixd@gmail.com>
81d6728 to
0b3229d
Compare
|
@1AbdullahButt Thank you for the detailed and insightful review! You are completely right: prepending Updates in commit
|
Fixes #1551
Summary
In PowerShell, passing a quoted command or executable path (e.g.
"C:\Program Files\nodejs\node.exe" --version) at the start of a command statement causes PowerShell to evaluate the first token as a string literal expression rather than invoking it as a command, throwingUnexpected token '--version' in expression or statement.In PowerShell syntax (PowerShell Language Specification §7.1.1), invoking an executable whose path is quoted or contained in a variable requires the call operator
&(e.g.& "C:\Program Files\nodejs\node.exe" --version).This PR updates
ProcessPromise.fullCmdto automatically prepend the PowerShell call operator&when:$.quote === quotePowerShellor shell binary matches/(?:powershell|pwsh)(?:\.exe)?$/i),$.prefixdoes not already specify a call operator (!prefix.trim().startsWith('&')), and/^['"]/.test(c.trimStart())).Usage Demo
Checklist
src/core.tsandbuild/core.cjs.test/smoke/win32.test.js.