fix: pass --dir parameter from attach command to server
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
--dirargument in attach command - Pass directory through TUI app to SDKProvider
- SDK client sends directory via existing
x-opencode-directoryheader
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.
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:
- Line 50: Use
baseCwdinstead ofprocess.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
- Line 96: Pass
directoryto tui args (likeattach.tsdoes):
prompt,
+ directory: cwd,
},
This ensures the directory context is correctly passed when using the default opencode command, not just opencode attach.
Closing - fixed upstream in #6715