cli doesnt connect to vscode
Description
I tried installing Opencode into VSCode by opening it up from the integrated terminal, but it didn't work. So, I manually installed the VSCode extension and tried to start OpenCode from that extension, but even then, when the CLI pops up, it has no connection to my ide/editor.
Plugins
No response
OpenCode version
1.125
Steps to reproduce
No response
Screenshot and/or share link
No response
Operating System
on macos running vscode on my ubunutu linux box using vscode remote ssh plugin
Terminal
No response
This issue might be a duplicate of existing issues. Please check:
- #6051: Lost changes and session in vscode - Similar VSCode extension connection and session management issues
- #7275: [FEATURE]: cleaner and more aware extensions integration with vscode - Related to VSCode extension integration challenges
- #2220: Cross platform open opencode in VSCode - Related to opening OpenCode from VSCode on different platforms
- #5094: OpenCode TUI loses connection with server - General connection loss issues between CLI and server
Feel free to ignore if none of these address your specific case.
The root cause of this issue is that in Remote SSH scenarios, the VSCode extension runs on your local macOS machine, but the CLI server runs on the remote Ubuntu machine.
When the extension tries to connect via fetch(http://localhost:${port}/app) (line 78 in extension.ts), it connects to local localhost instead of the remote localhost where the CLI server is listening.
Solution
VSCode provides the vscode.env.asExternalUri() API to handle this exact scenario. It automatically sets up port forwarding for remote environments.
For the OpenCode maintainers
The fix requires modifying sdks/vscode/src/extension.ts:
Current code (line 78):
await fetch(`http://localhost:${port}/app`)
Should be:
const externalUri = await vscode.env.asExternalUri(
vscode.Uri.parse(`http://localhost:${port}/app`)
);
await fetch(externalUri.toString());
The same change is needed in the appendPrompt function (line 94).
Workaround for users
Until this is fixed, you can manually set up SSH port forwarding:
- Find the port displayed in the terminal (the random port number)
- Add SSH port forwarding in your SSH config or use:
ssh -L ${port}:localhost:${port} your-server
Reference: Supporting Remote Development and GitHub Codespaces - see the "Forwarding localhost" section