Description
Image pasting from clipboard does not work on Windows (CMD or PowerShell terminals). The root cause is in pkg/client/image.go — the PowerShell command used to read clipboard image data contains $ characters that get silently stripped by Bun's spawn on Windows.
Root Cause Analysis
The clipboard reading path calls PowerShell with an inline script like:
powershell -Command "Add-Type -Assembly System.Windows.Forms; $img = [System.Windows.Forms.Clipboard]::GetImage(); ..."
When Bun (the JS runtime bundled in OpenCode) spawns this process on Windows, it passes the command through its own argument processing which strips all $ characters from the command string. This turns $img into img, $ms into ms, etc., producing invalid PowerShell that silently fails.
Evidence
- Tested with a minimal reproduction:
Bun.spawn(["powershell", "-Command", "echo $env:USERNAME"]) returns empty output on Windows, while cmd /c echo %USERNAME% works fine.
- The same PowerShell command works perfectly when executed directly in a terminal.
- Workaround confirmed: Changing from inline
-Command "..." to -File script.ps1 (where the script is a separate .ps1 file) bypasses the issue entirely, since Bun never sees the $ characters.
Environment
- OS: Windows Server 2022 / Windows 11
- Terminal: CMD, PowerShell, Windows Terminal
- OpenCode Version: v0.1.25 through v1.2.25
- Runtime: Bun (bundled)
Suggested Fix
Replace the inline PowerShell command with a -File based invocation. Either:
- Ship a
.ps1 helper script alongside the binary and invoke it via powershell -File clipboard.ps1
- Write a temp
.ps1 file at runtime, execute it, then clean up
- Use
-EncodedCommand with a Base64-encoded script block (avoids all escaping issues)
Option 3 is probably cleanest — no temp files, no shipped scripts, and immune to any shell escaping:
script := `Add-Type -Assembly System.Windows.Forms; $img = [System.Windows.Forms.Clipboard]::GetImage(); ...`
encoded := base64.StdEncoding.EncodeToString(utf16le(script))
exec.Command("powershell", "-EncodedCommand", encoded)
Current Workaround
Binary-patch the compiled opencode.exe to replace the inline -Command invocation with a -File call pointing to an external .ps1 script. This survives until the next update.
Related
Description
Image pasting from clipboard does not work on Windows (CMD or PowerShell terminals). The root cause is in
pkg/client/image.go— the PowerShell command used to read clipboard image data contains$characters that get silently stripped by Bun'sspawnon Windows.Root Cause Analysis
The clipboard reading path calls PowerShell with an inline script like:
When Bun (the JS runtime bundled in OpenCode) spawns this process on Windows, it passes the command through its own argument processing which strips all
$characters from the command string. This turns$imgintoimg,$msintoms, etc., producing invalid PowerShell that silently fails.Evidence
Bun.spawn(["powershell", "-Command", "echo $env:USERNAME"])returns empty output on Windows, whilecmd /c echo %USERNAME%works fine.-Command "..."to-File script.ps1(where the script is a separate.ps1file) bypasses the issue entirely, since Bun never sees the$characters.Environment
Suggested Fix
Replace the inline PowerShell command with a
-Filebased invocation. Either:.ps1helper script alongside the binary and invoke it viapowershell -File clipboard.ps1.ps1file at runtime, execute it, then clean up-EncodedCommandwith a Base64-encoded script block (avoids all escaping issues)Option 3 is probably cleanest — no temp files, no shipped scripts, and immune to any shell escaping:
Current Workaround
Binary-patch the compiled
opencode.exeto replace the inline-Commandinvocation with a-Filecall pointing to an external.ps1script. This survives until the next update.Related