opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat(cli): add --cwd flag to web and serve commands

Open temujin9 opened this issue 1 week ago • 1 comments

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.

temujin9 avatar Jan 06 '26 00:01 temujin9