Skip to content

fix(desktop): load WSL shell environment - #38252

Open
MrMushrooooom wants to merge 1 commit into
devfrom
wsl-shell-env
Open

fix(desktop): load WSL shell environment#38252
MrMushrooooom wants to merge 1 commit into
devfrom
wsl-shell-env

Conversation

@MrMushrooooom

@MrMushrooooom MrMushrooooom commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix the desktop WSL sidecar so it loads the user's WSL login environment before starting the OpenCode server.

When provider config uses environment interpolation such as:

{
  "providers": {
    "company": {
      "options": {
        "apiKey": "{env:COMPANY_KEY}",
        "baseURL": "{env:COMPANY_BASEURL}"
      }
    }
  }
}

OpenCode launched from a WSL terminal sees variables exported by the user's shell startup files, but the desktop sidecar did not. The desktop then resolved baseURL to an empty value and requests failed with errors such as "/chat/completions" cannot be parsed as a URL.

Root cause

The desktop sidecar launched an explicit non-login shell:

wsl ... -- bash -se

Explicit WSL commands do not load the user's login startup files, unlike the shell environment used to launch the TUI. As a result, the server processes had different environments depending on which client started them.

Fix

  • Build the sidecar shell command and server script at a pure, testable boundary.
  • Start an isolated interactive Bash environment and source the first readable login file using Bash's normal precedence:
    • ~/.bash_profile
    • ~/.bash_login
    • ~/.profile
  • Replace that bootstrap shell with the existing strict bash -se server script so the long-running sidecar behavior remains unchanged.
  • Preserve the existing PATH filtering, WSL-specific variables, authentication, logging, and health-check flow.

Regression coverage

The new test creates a hermetic WSL-home fixture where .profile loads .bashrc, .bashrc exports COMPANY_BASEURL, and a fake OpenCode executable reports the value inherited by the real generated sidecar command.

Before the fix the test deterministically reports an empty value. After the fix it receives https://company.example.

Verification

  • bun test src/main/wsl/sidecar-shell.test.ts src/main/wsl/servers.test.ts src/renderer/wsl/connections.test.ts — 15 passed
  • bun typecheck in packages/desktop — passed
  • Full repository typecheck from the push hook — 30 packages passed
  • Prettier check for all changed files — passed

The recording environment is Linux rather than a Windows host, so the videos use a hermetic reproduction of the exact shell boundary and generated sidecar command. They compare the TUI-style interactive shell with the desktop sidecar before and after the change.

Before

https://github.com/anomalyco/opencode/releases/download/pr-38252-videos/wsl-env-before.mp4

After

https://github.com/anomalyco/opencode/releases/download/pr-38252-videos/wsl-env-after.mp4

@huuthanh21

Copy link
Copy Markdown

Would you be able to make this fix more generic for users whose WSL login shell is not Bash?

I use zsh, and NVM initializes Node/npm from ~/.zshrc. The new bootstrap only sources Bash login files, so the Desktop WSL server still starts without node or npm in its PATH

Could the sidecar detect the user’s configured login shell and load its environment instead of hardcoding Bash startup files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants