Skip to content

Conversation

@oussamadouhou
Copy link

Summary

Fixes the --dir parameter in opencode attach command not being passed to the server, causing sessions to use the client's current directory instead of the specified path.

Changes:

  • Parse and resolve --dir argument in attach command
  • Pass directory through TUI app to SDKProvider
  • SDK client sends directory via existing x-opencode-directory header

Context

When running opencode attach <url> --dir /path/to/project, the parameter was being ignored. This particularly affected users running the server remotely and attaching from a different machine.

The SDK already supported the x-opencode-directory header; it just wasn't being wired through from the attach command.

Fixes #5380

Testing

Verified with:

  • Local server: opencode attach http://localhost:4096 --dir ~/project
  • Remote server: opencode attach http://remote:4096 --dir /remote/path
  • Relative paths correctly resolved to absolute paths
  • Without --dir: Falls back to current directory (backwards compatible)

Session API confirms correct directory is set on server.

The --dir parameter was being ignored in attach command, causing
sessions to use the client's current directory instead of the
specified path. Wire the parameter through TUI to SDK client.

Changes:
- Parse and resolve --dir argument in attach command
- Pass directory through TUI app to SDKProvider
- SDK client sends x-opencode-directory header to server

Fixes anomalyco#5380
proboscis added a commit to proboscis/orch that referenced this pull request Jan 4, 2026
Workaround for upstream bug where opencode attach --dir doesn't work
correctly (anomalyco/opencode#6413). Each worktree now gets its own
server to ensure correct project context.

- Remove server reuse logic that would share one server across projects
- Remove unused findRunningOpenCodeServer function
- Add TODO for future cleanup once upstream fix is merged
@proboscis
Copy link

Thanks for working on this fix! I noticed the same issue affects the main TUI command (thread.ts), not just the attach command.

When running opencode with bun's --cwd flag, process.cwd() returns the --cwd path, but process.env.PWD preserves the original shell directory.

Two additional changes needed in packages/opencode/src/cli/cmd/tui/thread.ts:

  1. Line 50: Use baseCwd instead of process.cwd() when no project arg:
-    const cwd = args.project ? path.resolve(baseCwd, args.project) : process.cwd()
+    const cwd = args.project ? path.resolve(baseCwd, args.project) : baseCwd
  1. Line 96: Pass directory to tui args (like attach.ts does):
         prompt,
+        directory: cwd,
       },

This ensures the directory context is correctly passed when using the default opencode command, not just opencode attach.

@oussamadouhou
Copy link
Author

Closing - fixed upstream in #6715

@oussamadouhou oussamadouhou deleted the fix/attach-dir-parameter branch January 6, 2026 09:16
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.

TUI referencing local directory when using attach --dir to connect to a remote server.

2 participants