[PR #9101] feat: export server URL and port as environment variables #12981

Closed
opened 2026-02-16 18:17:51 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/9101

State: closed
Merged: No


What does this PR do?

Exports OPENCODE_SERVER_URL and OPENCODE_SERVER_PORT as environment variables when the HTTP server starts. This allows child processes (tmux panes, shell scripts, background tasks) to discover which port OpenCode is actually running on, instead of relying on the hardcoded fallback URL.

Why this matters:

  • When port 4096 is taken, OpenCode silently falls back to a random port
  • Child processes had no way to discover the actual port
  • Plugins like tmux integrations need to spawn opencode attach <url> but don't know the URL

Fixes #9099

How did you verify your code works?

Manual testing:

  1. Started OpenCode server and verified env vars are exported:

    # Terminal 1: Start server
    bun dev -- serve --port 0
    
    # Terminal 2: Check environment
    ps aux | grep opencode  # Get the PID
    cat /proc/<PID>/environ | tr '\0' '\n' | grep OPENCODE
    # Output shows:
    # OPENCODE_SERVER_URL=http://localhost:4096
    # OPENCODE_SERVER_PORT=4096
    
  2. Tested port fallback scenario:

    # Terminal 1: Occupy port 4096
    python3 -m http.server 4096
    
    # Terminal 2: Start OpenCode (should use random port)
    bun dev -- serve --port 0
    
    # Terminal 3: Verify env var shows actual port, not 4096
    # OPENCODE_SERVER_URL shows something like http://localhost:58342
    
  3. Verified child process can attach using env var:

    # Start OpenCode with server
    bun dev -- serve --port 0
    
    # In another terminal (child process inherits env)
    opencode attach $OPENCODE_SERVER_URL
    # Successfully connects
    
  4. Verified cleanup on server stop:

    # Start server, check env is set, stop server
    # Confirmed OPENCODE_SERVER_URL and OPENCODE_SERVER_PORT are deleted
    

Code review:

  • Verified the env vars are set immediately after _url = server.url (line 559-560)
  • Verified cleanup happens in server.stop() before returning (line 578-579)
  • No breaking changes - purely additive feature
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9101 **State:** closed **Merged:** No --- ### What does this PR do? Exports `OPENCODE_SERVER_URL` and `OPENCODE_SERVER_PORT` as environment variables when the HTTP server starts. This allows child processes (tmux panes, shell scripts, background tasks) to discover which port OpenCode is actually running on, instead of relying on the hardcoded fallback URL. **Why this matters:** - When port 4096 is taken, OpenCode silently falls back to a random port - Child processes had no way to discover the actual port - Plugins like tmux integrations need to spawn `opencode attach <url>` but don't know the URL Fixes #9099 --- ### How did you verify your code works? **Manual testing:** 1. **Started OpenCode server and verified env vars are exported:** ```bash # Terminal 1: Start server bun dev -- serve --port 0 # Terminal 2: Check environment ps aux | grep opencode # Get the PID cat /proc/<PID>/environ | tr '\0' '\n' | grep OPENCODE # Output shows: # OPENCODE_SERVER_URL=http://localhost:4096 # OPENCODE_SERVER_PORT=4096 ``` 2. **Tested port fallback scenario:** ```bash # Terminal 1: Occupy port 4096 python3 -m http.server 4096 # Terminal 2: Start OpenCode (should use random port) bun dev -- serve --port 0 # Terminal 3: Verify env var shows actual port, not 4096 # OPENCODE_SERVER_URL shows something like http://localhost:58342 ``` 3. **Verified child process can attach using env var:** ```bash # Start OpenCode with server bun dev -- serve --port 0 # In another terminal (child process inherits env) opencode attach $OPENCODE_SERVER_URL # Successfully connects ``` 4. **Verified cleanup on server stop:** ```bash # Start server, check env is set, stop server # Confirmed OPENCODE_SERVER_URL and OPENCODE_SERVER_PORT are deleted ``` **Code review:** - Verified the env vars are set immediately after `_url = server.url` (line 559-560) - Verified cleanup happens in `server.stop()` before returning (line 578-579) - No breaking changes - purely additive feature
yindo added the pull-request label 2026-02-16 18:17:51 -05:00
yindo closed this issue 2026-02-16 18:17:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12981