[PR #7011] feat(cli): add --cwd flag to web and serve commands #12200

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

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

State: open
Merged: No


Summary

The opencode web and opencode serve commands did not preserve the working directory, causing process.cwd() to return an unexpected value (often /) when the server handles requests. This resulted in sessions being created with worktree: "/" instead of the intended project directory.

Changes

  • Added --cwd flag to both web and serve commands
  • Commands now call process.chdir() before starting the server, following the same pattern used by the TUI command (thread.ts)

Testing

  • Typecheck passes
  • Manual verification: --cwd option appears in --help output for both commands

Root Cause

The server middleware in server.ts (line 249) determines the directory per-request:

const directory = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()

Unlike the TUI command which calls process.chdir(cwd) before starting, the web and serve commands did not set the working directory, causing the fallback to process.cwd() to return an unintended value.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7011 **State:** open **Merged:** No --- ## Summary The `opencode web` and `opencode serve` commands did not preserve the working directory, causing `process.cwd()` to return an unexpected value (often `/`) when the server handles requests. This resulted in sessions being created with `worktree: "/"` instead of the intended project directory. ## Changes - Added `--cwd` flag to both `web` and `serve` commands - Commands now call `process.chdir()` before starting the server, following the same pattern used by the TUI command (`thread.ts`) ## Testing - Typecheck passes - Manual verification: `--cwd` option appears in `--help` output for both commands ## Root Cause The server middleware in `server.ts` (line 249) determines the directory per-request: ```typescript const directory = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd() ``` Unlike the TUI command which calls `process.chdir(cwd)` before starting, the `web` and `serve` commands did not set the working directory, causing the fallback to `process.cwd()` to return an unintended value.
yindo added the pull-request label 2026-02-16 18:17:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12200