Skip to content

Conversation

@temujin9
Copy link

@temujin9 temujin9 commented Jan 6, 2026

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.

The web and 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.

This change adds a --cwd flag to both commands, following the same
pattern used by the TUI command (thread.ts) which correctly calls
process.chdir() before starting the server.

Fixes working directory detection for headless server modes.
@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

The following comment was made by an LLM, it may be inaccurate:

Duplicate PR Check Results

No duplicate PRs found.

The search returned only PR #7011 (the current PR) when searching for variations of the --cwd flag, working directory handling, and server middleware patterns.

Two related but distinct PRs were found that address similar directory/path issues in different contexts:

These are not duplicates of the current PR as they address different problems in different parts of the codebase.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant