[Windows] powershell tool: stray . prepended to every command breaks the first word
buguntriaged
### What happened?
On Windows, the built-in powershell tool prepends a UTF-8 encoding prefix to every command, but the prefix ends with a stray `.` that gets glued directly onto the first word of the user's command (no `;`/space separator). PowerShell then parses `.<first word>` as member access, so the first statement of every command errors:
The term '.Get-ChildItem' is not recognized as the name of a cmdlet, function, script file, or executable program.
or, for commands with a pipeline,
The expression after '.' in a pipeline element produced an object that was not valid...
The rest of a multi-statement command (after `;`) still runs, but the first statement is lost and the exit code is always 1 even when the command succeeds, so `$?`/exit-code checks are unreliable.
The generated command is (e.g. for `Get-ChildItem`):
try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}.Get-ChildItem
`catch {}.` + `Get-ChildItem` — the `.` makes `Get-ChildItem` a member access on the (empty) result of the try/catch block.
Root cause: the prefix constant ends with a `.` instead of a statement separator:
var UTF8_OUTPUT_PREFIX = `try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}.`;
// executed as:
operations.exec(`${UTF8_OUTPUT_PREFIX}${command}`, cwd, options)
Fix: terminate the prefix with a separator, e.g. `...catch {}; ` (change the trailing `.` to `; `), so the user command is its own statement.
### Steps to reproduce
1. Windows 11, pi 0.84.3 (scoop, official SEA pi.exe), pwsh 7 on PATH.
2. `defaultTools` includes `powershell`.
3. Run any simple command through the powershell tool, e.g. `Get-ChildItem` or `Write-Host hi`.
4. The first word is reported as `.Get-ChildItem` / `.Write-Host` → "not recognized"; exit code is 1.
### Expected behavior
The prefix sets the console encoding and then runs the user command as a separate statement, so the first word is not swallowed and the exit code reflects the command's real result.
### Version
0.84.3
关闭于 2 天前 2 条评论