Skip to content

Fix okteto.* commands failing on PowerShell with "Unexpected token"#342

Open
Zionett wants to merge 1 commit into
okteto:mainfrom
Zionett:fix/powershell-call-operator-341
Open

Fix okteto.* commands failing on PowerShell with "Unexpected token"#342
Zionett wants to merge 1 commit into
okteto:mainfrom
Zionett:fix/powershell-call-operator-341

Conversation

@Zionett

@Zionett Zionett commented Jun 5, 2026

Copy link
Copy Markdown

Summary

Fixes #341 — every Okteto: command fails on PowerShell with Unexpected token '<subcommand>' in expression or statement after #318.

Root cause

PR #318 introduced shell-aware quoting in src/shell.ts. For shell === 'powershell', quote() wraps values in single quotes, so the six command builders in src/okteto.ts produced lines that begin with a quoted string literal:

'C:\Program Files\okteto\okteto.exe' up 'api' -f 'C:\…\okteto.yml' --remote 22000

PowerShell parses a leading single-quoted token as a string expression, not a command, so the next token (up) is a parse error.

Fix

The canonical PowerShell idiom for invoking an executable whose path needs quoting is the call operator &. It forces invocation while keeping stdout/stderr attached to the integrated terminal (unlike Start-Process) and does not re-parse arguments (unlike Invoke-Expression, which would also reopen a quoting/injection footgun against quotePowerShell).

  • New invokeBinary(binary, shell) helper in src/shell.ts that prepends & only when shell === 'powershell'.
  • All six builders in src/okteto.ts (buildUpCommand, buildDeployCommand, buildDestroyCommand, buildTestCommand, buildSetContextCommand, buildSetNamespaceCommand) now route the binary through invokeBinary instead of quote.
  • POSIX (where & is the background operator) and cmd.exe (where "path" arg already invokes) are unchanged. Git Bash continues to use POSIX quoting via gitBashMode().
  • Works identically on Windows PowerShell 5.1 and PowerShell 7+ (pwsh).

Test coverage

Unit tests grow from 165 → 240 in this PR (the bulk of the increase is unrelated coverage that landed in pnpm test collection; +14 are this PR's: 8 new invokeBinary tests + 6 cross-builder regression tests):

  • New describe('invokeBinary', …) block in src/test/suite/shell.test.ts covering POSIX, PowerShell, and cmd × bare names, paths with spaces, embedded apostrophes.
  • Updated PowerShell expectations across all six builders in src/test/suite/okteto.commands.test.ts to expect the leading & .
  • Tightened the existing metacharacter regression to assert startsWith("& '") for PowerShell so a malicious binary path can't drop the prefix.
  • New describe('PowerShell call operator (issue #341)', …) cross-builder guardrail that asserts every PowerShell builder output matches /^& '/. Any future builder added without invokeBinary will fail CI here.

Test plan

  • pnpm run lint — no errors
  • pnpm test — 240 passing
  • pnpm run test:e2e — 10 passing
  • pnpm run package — produces remote-kubernetes-0.5.4.vsix
  • Manual smoke on Windows: Okteto: Up, Deploy, Destroy, Test, Set Context, Set Namespace in a PowerShell-backed VS Code terminal (PS 5.1 + PS 7), with a binary path that contains spaces (C:\Program Files\okteto\okteto.exe)
  • Manual smoke on cmd.exe and Git Bash (okteto.gitBash = true) to confirm no regression

🤖 Generated with Claude Code

@Zionett Zionett force-pushed the fix/powershell-call-operator-341 branch from 681ecdb to 1d92426 Compare June 5, 2026 19:13
PR okteto#318 introduced shell-aware quoting that wraps the binary path in
single quotes. PowerShell parses a leading single-quoted token as a
string expression, not a command, so every Okteto command fails with
"Unexpected token '<subcommand>' in expression or statement".

Add invokeBinary() in src/shell.ts which prepends PowerShell's call
operator (`&`) when shell === 'powershell', and route the six command
builders through it. POSIX (where `&` is the background operator) and
cmd.exe (where `"path" arg` already invokes) are unchanged.

Tests: new invokeBinary block in shell.test.ts, updated PowerShell
expectations across all six builders, and a cross-builder regression
that asserts every PowerShell command line starts with `& '` so a
future builder added without invokeBinary fails CI.

Fixes okteto#341

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Zionett Zionett force-pushed the fix/powershell-call-operator-341 branch from 1d92426 to 486b602 Compare June 5, 2026 19:24
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.

Powershell command quoting doesn't work

1 participant