add --dir flag to opencode run --attach #4457

Closed
opened 2026-02-16 17:43:50 -05:00 by yindo · 3 comments
Owner

Originally created by @athal7 on GitHub (Jan 8, 2026).

Originally assigned to: @thdxr on GitHub.

opencode attach has a --dir flag to specify the working directory when attaching to a remote server:

opencode attach <url>

Options:
  --dir         directory to run in                                                     [string]

However, opencode run --attach does not expose this flag:

opencode run [message..]

Options:
  --attach      attach to a running opencode server (e.g., http://localhost:4096)       [string]
  # no --dir option

Use Case

I'm building opencode-pilot, a service that polls external sources (Linear, GitHub, etc.) and spawns OpenCode sessions for new issues. When a user runs OpenCode Desktop connected to a global server (worktree="/"), I want spawned sessions to:

  1. Appear in the Desktop UI (requires --attach)
  2. Run in the correct project directory (requires --dir)

Currently I can only achieve one or the other:

# Sessions appear in Desktop, but run in wrong directory (home)
cd ~/projects/my-app && opencode run --attach http://localhost:4096 "fix bug"
# Working directory: /Users/me (wrong!)

# Sessions run in correct directory, but don't appear in Desktop
cd ~/projects/my-app && opencode run "fix bug"  
# Working directory: /Users/me/projects/my-app (correct, but isolated)

Proposed Solution

Add --dir flag to opencode run that gets passed through when --attach is used:

opencode run --attach http://localhost:4096 --dir /path/to/project "fix bug"

The server middleware already supports this via x-opencode-directory header or ?directory= query param, so this should be straightforward to wire up.

Related

  • #7011 - adds --cwd to opencode serve/web
  • #6697 - session switching doesn't change working directory context
  • #6715 - pass attach directory to sdk client (merged)
Originally created by @athal7 on GitHub (Jan 8, 2026). Originally assigned to: @thdxr on GitHub. `opencode attach` has a `--dir` flag to specify the working directory when attaching to a remote server: ``` opencode attach <url> Options: --dir directory to run in [string] ``` However, `opencode run --attach` does not expose this flag: ``` opencode run [message..] Options: --attach attach to a running opencode server (e.g., http://localhost:4096) [string] # no --dir option ``` ## Use Case I'm building [opencode-pilot](https://github.com/athal7/opencode-pilot), a service that polls external sources (Linear, GitHub, etc.) and spawns OpenCode sessions for new issues. When a user runs OpenCode Desktop connected to a global server (worktree="/"), I want spawned sessions to: 1. Appear in the Desktop UI (requires `--attach`) 2. Run in the correct project directory (requires `--dir`) Currently I can only achieve one or the other: ```bash # Sessions appear in Desktop, but run in wrong directory (home) cd ~/projects/my-app && opencode run --attach http://localhost:4096 "fix bug" # Working directory: /Users/me (wrong!) # Sessions run in correct directory, but don't appear in Desktop cd ~/projects/my-app && opencode run "fix bug" # Working directory: /Users/me/projects/my-app (correct, but isolated) ``` ## Proposed Solution Add `--dir` flag to `opencode run` that gets passed through when `--attach` is used: ```bash opencode run --attach http://localhost:4096 --dir /path/to/project "fix bug" ``` The server middleware already supports this via `x-opencode-directory` header or `?directory=` query param, so this should be straightforward to wire up. ## Related - #7011 - adds `--cwd` to `opencode serve/web` - #6697 - session switching doesn't change working directory context - #6715 - pass attach directory to sdk client (merged)
yindo closed this issue 2026-02-16 17:43:50 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 8, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #6697: Session switching doesn't change working directory context
  • #6715: pass attach directory to sdk client (related fix that was merged)
  • #7011: adds --cwd to opencode serve/web

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 8, 2026): This issue might be a duplicate of existing issues. Please check: - #6697: Session switching doesn't change working directory context - #6715: pass attach directory to sdk client (related fix that was merged) - #7011: adds `--cwd` to `opencode serve/web` Feel free to ignore if none of these address your specific case.
Author
Owner

@ekweible commented on GitHub (Jan 23, 2026):

Hey @athal7 I noticed you closed #7378, was that because this issue was fixed elsewhere?

@ekweible commented on GitHub (Jan 23, 2026): Hey @athal7 I noticed you closed #7378, was that because this issue was fixed elsewhere?
Author
Owner

@athal7 commented on GitHub (Jan 23, 2026):

@ekweible i didn't have a chance to continue to test and it seemed there have been quite a lot of structural changes upstream with workspaces. Didn't want to waste reviewer time if it was possibly stale

@athal7 commented on GitHub (Jan 23, 2026): @ekweible i didn't have a chance to continue to test and it seemed there have been quite a lot of structural changes upstream with workspaces. Didn't want to waste reviewer time if it was possibly stale
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#4457