Skip to content

Commit 0758efc

Browse files
committed
feat(opencode-attach): inject --dir $PWD so attached session uses terminal CWD
Bare `opencode attach <url>` falls back to the server's process.cwd() (the dir openweb was launched from), so every attached session lands in the same project regardless of where the alias was invoked. Forward `--dir "$PWD"` by default so the attached session uses the terminal's CWD instead. Detects caller-supplied `--dir` (both `--dir <path>` and `--dir=<path>` forms) and skips the default injection so the explicit value wins. Adds regression tests for default injection, CWD reflection, caller override (both flag forms), and coexistence with other passthrough args. Refs: anomalyco/opencode#14460
1 parent 4c3325a commit 0758efc

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

‎personal/opencode-attach.sh‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
# of NEVER eagerly populating secrets at shell init. Without this, `opencode
88
# attach` sends no Basic-Auth credentials and the server replies "unauthorized".
99
#
10+
# Working directory: bare `opencode attach <url>` falls back to the SERVER's
11+
# CWD (the dir `openweb` was launched from), not the terminal's CWD — so
12+
# every attach lands in the same project regardless of where the alias was
13+
# invoked. We forward `--dir "$PWD"` by default so the attached session
14+
# uses the terminal's CWD instead. The path is interpreted server-side
15+
# (sent via the `x-opencode-directory` header), which is fine on a single
16+
# host but means cross-machine attach must use a path the server can see.
17+
# Caller can override by passing `-- --dir /other/path`; we detect that and
18+
# skip the default injection so the explicit value wins.
19+
# Refs: https://github.com/anomalyco/opencode/issues/14460
20+
#
1021
# Companion: `openweb` (personal/opencode-web.sh) starts the server.
1122
#
1223
# Keychain entry consumed:
@@ -40,9 +51,14 @@ Arguments:
4051
Options:
4152
-h, --help Show this help.
4253
54+
By default, the attached session uses the current shell's CWD (forwarded
55+
as `--dir "$PWD"`). Override by passing `--dir <path>` after `--`; the
56+
explicit value wins.
57+
4358
Any args after `--` are passed through to `opencode attach`. Example:
4459
openattach -- --continue
4560
openattach http://127.0.0.1:4096 -- --session abc123
61+
openattach -- --dir /other/project # override the default CWD
4662
4763
Environment overrides:
4864
OPENCODE_WEB_PORT Port the backend is on (default: 4096)
@@ -95,8 +111,26 @@ OPENCODE_SERVER_PASSWORD="$(keychain_get 'opencode-server-password')" || exit $?
95111
export OPENCODE_SERVER_PASSWORD
96112
export OPENCODE_SERVER_USERNAME="${OPENCODE_SERVER_USERNAME:-opencode}"
97113

114+
# Inject the client's CWD as the attached session's working directory unless
115+
# the caller already supplied --dir via passthrough. The opencode server uses
116+
# the `x-opencode-directory` header (forwarded by `--dir`) per request, so
117+
# without this the session falls back to the SERVER's process.cwd() — which
118+
# is whatever dir `openweb` was launched from, not the terminal's CWD.
119+
# Refs: https://github.com/anomalyco/opencode/issues/14460
120+
HAS_DIR=0
121+
for arg in ${PASSTHROUGH[@]+"${PASSTHROUGH[@]}"}; do
122+
if [[ "$arg" == "--dir" || "$arg" == --dir=* ]]; then
123+
HAS_DIR=1
124+
break
125+
fi
126+
done
127+
98128
# bash 3.2 (macOS /bin/bash) treats "${arr[@]}" as unbound under `set -u`
99129
# when the array is empty. Use the conditional-expansion idiom that the rest
100130
# of the repo uses (see agent/scripts-doctor.sh) so an empty PASSTHROUGH is
101131
# safe across bash 3.2 → 5.x.
102-
exec opencode attach "$URL" ${PASSTHROUGH[@]+"${PASSTHROUGH[@]}"}
132+
if [[ $HAS_DIR -eq 0 ]]; then
133+
exec opencode attach "$URL" --dir "$PWD" ${PASSTHROUGH[@]+"${PASSTHROUGH[@]}"}
134+
else
135+
exec opencode attach "$URL" ${PASSTHROUGH[@]+"${PASSTHROUGH[@]}"}
136+
fi

‎tests/opencode-attach.bats‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ write_all_stubs() {
126126
run "$SCRIPT" -- --continue
127127
assert_success
128128
run cat "$STATEFILE"
129-
assert_output --partial "opencode attach http://127.0.0.1:4096 --continue"
129+
assert_output --partial "opencode attach http://127.0.0.1:4096 --dir"
130+
assert_output --partial "--continue"
130131
}
131132

132133
@test "opencode-attach: URL + passthrough args coexist" {
@@ -135,7 +136,74 @@ write_all_stubs() {
135136
run "$SCRIPT" "http://h:9" -- --session abc
136137
assert_success
137138
run cat "$STATEFILE"
138-
assert_output --partial "opencode attach http://h:9 --session abc"
139+
assert_output --partial "opencode attach http://h:9 --dir"
140+
assert_output --partial "--session abc"
141+
}
142+
143+
# --- --dir injection regression tests ---------------------------------------
144+
#
145+
# `opencode attach` without --dir falls back to the SERVER's process.cwd()
146+
# (the dir openweb was launched from), so every attached session lands in
147+
# the same project regardless of where the alias was invoked. These tests
148+
# pin the wrapper's behavior of injecting `--dir "$PWD"` by default and
149+
# yielding to an explicit caller-supplied --dir.
150+
# Refs: https://github.com/anomalyco/opencode/issues/14460
151+
152+
@test "opencode-attach: injects --dir \$PWD by default" {
153+
write_all_stubs
154+
export KEYCHAIN_VALUE="x"
155+
cd "$STUBDIR"
156+
run "$SCRIPT"
157+
assert_success
158+
run cat "$STATEFILE"
159+
assert_output --partial "opencode attach http://127.0.0.1:4096 --dir $STUBDIR"
160+
}
161+
162+
@test "opencode-attach: --dir reflects current working directory at invocation" {
163+
write_all_stubs
164+
export KEYCHAIN_VALUE="x"
165+
local subdir="$STUBDIR/nested/project"
166+
mkdir -p "$subdir"
167+
cd "$subdir"
168+
run "$SCRIPT"
169+
assert_success
170+
run cat "$STATEFILE"
171+
assert_output --partial "--dir $subdir"
172+
}
173+
174+
@test "opencode-attach: caller-supplied --dir overrides default injection" {
175+
write_all_stubs
176+
export KEYCHAIN_VALUE="x"
177+
cd "$STUBDIR"
178+
run "$SCRIPT" -- --dir /override/path
179+
assert_success
180+
run cat "$STATEFILE"
181+
# The forwarded args should contain the caller's --dir exactly once,
182+
# and must not contain the default $PWD injection.
183+
assert_output --partial "--dir /override/path"
184+
refute_output --partial "--dir $STUBDIR"
185+
}
186+
187+
@test "opencode-attach: caller-supplied --dir=value form also overrides default" {
188+
write_all_stubs
189+
export KEYCHAIN_VALUE="x"
190+
cd "$STUBDIR"
191+
run "$SCRIPT" -- --dir=/override/path
192+
assert_success
193+
run cat "$STATEFILE"
194+
assert_output --partial "--dir=/override/path"
195+
refute_output --partial "--dir $STUBDIR"
196+
}
197+
198+
@test "opencode-attach: --dir injected alongside other passthrough args" {
199+
write_all_stubs
200+
export KEYCHAIN_VALUE="x"
201+
cd "$STUBDIR"
202+
run "$SCRIPT" -- --continue
203+
assert_success
204+
run cat "$STATEFILE"
205+
assert_output --partial "--dir $STUBDIR"
206+
assert_output --partial "--continue"
139207
}
140208

141209
@test "opencode-attach: exports OPENCODE_SERVER_PASSWORD for the child process" {

0 commit comments

Comments
 (0)