opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: pass --dir parameter from attach command to server

Open oussamadouhou opened this issue 3 weeks ago • 1 comments

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.

oussamadouhou avatar Dec 30 '25 00:12 oussamadouhou

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.

proboscis avatar Jan 04 '26 16:01 proboscis

Closing - fixed upstream in #6715

oussamadouhou avatar Jan 06 '26 09:01 oussamadouhou