Repository navigation
fix(opencode): resolve rtk binary path for GUI-launched OpenCode (#4462) - #4507
Conversation
|
Automatic message from CI checks : It seems like this branch is targeting the wrong branch, any contribution should target develop branch. See CONTRIBUTING.md for details. |
📊 Automated PR Analysis
SummaryFixes OpenCode plugin failing to find the rtk binary when OpenCode is launched from a GUI (e.g. macOS Dock) and doesn't inherit the shell PATH. Adds multi-strategy path resolution (RTK_BIN, cargo/homebrew paths, bundled binary) and plumbs a resolved absolute bin_path through to the generated rewrite command. Also bundles unrelated CI/dependency maintenance (pinned GitHub Actions SHAs, Cargo.lock bumps, a new rustdoc CI job, and doc link fixes). Review Checklist
Linked issues: #4462 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
da01fd0 to
dad2649
Compare
|
Gentle reminder @KuSh, could you please review this PR when you have a moment? Thanks! |
KuSh
left a comment
There was a problem hiding this comment.
Round 1 of 3 — CHANGES REQUESTED
Claim: when rtk is installed outside OpenCode's PATH (~/.cargo/bin, Homebrew, RTK_BIN), the OpenCode plugin still finds it and rewrites commands with its absolute path, so they run (#4462).
Scope: accept (round 1, frozen); maintainer PRs: checked against #4458: disjoint files, merges clean, plugin suite unchanged on the merge.
Ran: real OpenCode 1.18.10 (mock provider, env -i, PATH=/usr/bin:/bin) × plugin develop vs this head × {rtk only in ~/.cargo/bin, only at RTK_BIN, on PATH} × 5 command shapes = 30 rows; 8 permission rows (rules naming rtk, ask and allow, rtk on PATH vs in ~/.cargo/bin); 4 quoting rows (RTK_BIN path with a space, with an apostrophe). Invariants: every decision; the command OpenCode actually evaluated, read from its log. No savings figure: the change is transport only.
Pins: 9 decisions, 11 mutations, 9 survived the plugin suite (everything except the on-PATH flag).
Matrix: executed this round; pin check: executed this round.
CI @dad2649fd: all green (15/15). CI does not run hooks/opencode/rtk.test.mjs yet (#4588, PR #4529).
Blocks merge (3, frozen at round 1)
-
hooks/opencode/rtk.ts:212: only the firstrtkof a rewrite gets the path.rtk hook opencodeputsrtkwherever it rewrote a segment, not only at the start:cd sub && rtk ls -la,FOO=1 rtk ls -la,rtk ls -la && rtk read a.txt,rtk ls; rtk read a.txt,timeout 5 rtk git status. ThestartsWith("rtk ")arm reaches none of the later ones, so with rtk outsidePATHthey now fail, where develop ran them unrewritten:command (rtk only in ~/.cargo/bin)develop this head ls -laraw ls -lartk output ✓ cd sub && ls -laraw /usr/bin/bash: line 1: rtk: command not foundFOO=1 ls -laraw rtk: command not foundls -la && cat a.txtraw rtk lsoutput, thenrtk: command not foundls; cat a.txtraw lsoutput, thenrtk: command not foundThe same four rows fail with rtk only at
RTK_BIN; with rtk onPATH, all five behave as on develop. Agents chain commands withcd … &&constantly, so most real calls go from a working pass-through to a failed command. -
hooks/opencode/rtk.ts:208: OpenCode judges a different command from the one rtk judged.rtk hook opencodeonly answers a rewrite after checking thatrtk <cmd>gets the same verdict as<cmd>under OpenCode's own rules (opencode_answerinsrc/hooks/hook_cmd.rs, #4195). The plugin then replacesrtkwith an absolute path, and OpenCode evaluates that new string, which a rule namingrtkno longer matches:{"*":"allow","ls *":"ask","rtk *":"ask"}, commandls -la. Develop asks, whether rtk is onPATHor not. This head, with rtk in~/.cargo/bin, runs it without asking. OpenCode logspattern="…/.cargo/bin/rtk ls -la" action.pattern=* action.action=allow. The ask is silenced.{"*":"ask","ls *":"allow","rtk *":"allow"}. Develop runs the command. This head:permission requested: bash (…/.cargo/bin/rtk ls -la); auto-rejecting. The allow is blocked.
With rtk on
PATH, both rows behave as on develop. -
hooks/opencode/rtk.ts:71: unpinned behaviour, and a test that still asserts the old one. The PR adds no test. These mutations all passnode --test hooks/opencode/rtk.test.mjs(run with a cleanHOME): drop theRTK_BINbranch; accept a directory asRTK_BINor as a candidate; drop the~/.cargo/bincandidate; never prefix (if (!cachedRtkOnPath)→if (false)); drop the quoting; drop either rewrite arm; checkRTK_BINbeforePATH. Only flipping the on-PATHflag is caught, by the existing tests that expect a barertk ls -la. Separately, test (a) inan rtk without hook opencode registers nothingstill assertsresolveRtkPath() === nullwhen nothing is onPATH. It now fails on any machine that has~/.cargo/bin/rtk(9/10 here) and passes only where none exists. The tests need these rows, withHOMEpointed at a temp dir andPATHempty:- rtk only in
$HOME/.cargo/bin:ls -laand a chained command both come back runnable against that binary; - the same with only
RTK_BINset; - rtk on
PATH:rtk ls -lais unchanged; - a rule set naming
rtk: the verdict is unchanged.
The "never prefix" mutation must turn them red, and (a) must stop depending on the developer's
HOME. - rtk only in
Blockers 1 and 2 have one root: the plugin edits rtk's answer after rtk judged it. #4462 names two shapes that leave the answer as judged; neither has been tried here:
- (A) make
rtkresolvable in the spawned shell: the 1.xshell.envhook and the 2.xctx.shell.hook("create.before"), appending the resolved directory toPATH. Appending rather than prepending, so no other command resolves differently. The rewrite staysrtk …, so OpenCode judges the same string rtk judged, and every segment resolves. - (B) have
rtk hook opencodeemit the absolute path itself and judge that string. This is what the PR description says (opencode_answer()withbin_path,current_exe()inmain.rs), but those changes are not in the pushed commit.
Either way, the rows to pass:
- the five shapes above × {
~/.cargo/bin,RTK_BIN}: each comes back as rtk output; - the two permission rows: each gives develop's verdict;
- rtk on
PATH: unchanged.
Optional (will not hold merge)
- Quoting covers only a space. With
RTK_BINat…/bin'quote/rtk, the command fails withbash: -c: line 1: unexpected EOF while looking for matching '''. A$or a backtick inside the double quotes would expand. This goes away if the fix stops splicing the path into the command. - The
rewritten === "rtk"arm is unreachable:rtk hook opencodenever answers a barertk. isRtkOnPath()is exported but nothing calls it. ThePATHloop and the candidate loop are the same probe written twice.- The candidates leave out
~/.local/bin, which isinstall.sh's default (INSTALL_DIR="${RTK_INSTALL_DIR:-$HOME/.local/bin}"). A Linux user who installed that way and starts OpenCode from a desktop entry still gets "plugin disabled". RTK_BINis checked afterPATH, so it cannot select a build over an rtk that is already onPATH. Is that intended? No row decides it today, and moving it first passes the suite.hooks/opencode/README.md:14, and the comment in test (a), still say rtk is resolved fromPATHonly.RTK_BINand the probed directories are not documented.- The PR description lists changes to
src/hooks/hook_cmd.rsandsrc/main.rs, and Rust test updates, that are not in the commit. Please make it match what is pushed.
Filed as follow-ups
Checked and correct — no need to re-verify
- rtk on
PATH: all five command shapes and both permission rows match develop. - The issue's three rows for a single
ls -la:~/.cargo/binonly andRTK_BINonly are rewritten and run; onPATHis unchanged. - An
RTK_BINpath containing a space runs. - The PR merges clean with develop and with #4458.
Your questions
- None asked.
Next
Fix blockers 1–3: every rtk in the answer has to resolve without changing the command OpenCode judges. Pick (A) or (B), or another shape that passes the rows above, and add tests for them. Please also update the PR description.
Rounds: 1/3. Threads: 3 open (waiting on the author), 0 resolved.
…g command (rtk-ai#4462) Address review feedback on rtk-ai#4507: - Make rtk resolvable in spawned shells via OpenCode 2.x create.before and 1.x shell.env hooks, appending rtk directory to PATH - Leave command rewrites untouched as bare 'rtk ...' so OpenCode permissions and multi-command shapes (cd, env vars, chains, semicolons) remain valid - Check RTK_BIN before PATH, include ~/.local/bin candidate, and deduplicate directory probes - Add node test coverage across ~/.cargo/bin candidate, RTK_BIN, and PATH rows with isolated HOME
…in the shell (rtk-ai#4462) The shell hooks now append rtk's directory under the PATH key OpenCode's environment already has (`Path` on a GUI-launched Windows process), so spreading the hook's env over it leaves one PATH instead of two that differ only in case. Candidate directories move to candidateRtkDirs(). Tests, with HOME pointed at a temp dir and a launcher PATH without rtk: - rtk only in ~/.cargo/bin, only in ~/.local/bin, only at RTK_BIN: the hooks' env carries OpenCode's PATH plus rtk's directory, and a real shell run with it resolves every rtk in `cd . && rtk`, `FOO=1 rtk`, `rtk && rtk`, `rtk; rtk` and `env rtk` (cmd.exe forms on Windows). - the command OpenCode judges is rtk's answer byte for byte whether rtk is on PATH, in ~/.cargo/bin or at RTK_BIN, so a rule naming `rtk` keeps its verdict. - test (a) no longer fails on a machine with a system-wide rtk.
The rewrite stays a bare `rtk …` and the shell hooks only append rtk's directory to the shell's PATH, so the shell runs the first rtk on PATH. Resolving RTK_BIN ahead of it let a different binary probe and judge the command than the one that ran it, and an RTK_BIN file not named `rtk` was accepted although the shell can never reach it as `rtk`, so every rewrite came back `rtk: command not found`. An rtk on PATH now comes first. RTK_BIN is used when it is an absolute path to a file named `rtk`, and a warning names it when it is ignored. Tests pin both orders and the RTK_BIN shapes that are skipped, and the shell-PATH test skips on a machine whose /usr/bin holds an rtk, as test (a) does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
KuSh
left a comment
There was a problem hiding this comment.
Round 2 of 3 — APPROVED
Claim: when rtk is installed outside OpenCode's PATH (~/.cargo/bin, ~/.local/bin, Homebrew, RTK_BIN), the OpenCode plugin finds it, and every rtk in a rewrite runs, because rtk's directory is appended to the spawned shell's PATH (#4462).
Scope: accept (round 1, frozen); maintainer PRs: checked against #4458, #4331, #4341, #4269 and #4103. None touches hooks/opencode/, and each merges clean with this branch.
Ran: OpenCode 1.18.10 (host) and 2.3.0 (Docker), plugin develop vs 92629e053, with a mock provider:
- 3 install locations × 5 command shapes on each version (60 rows);
- 4 permission rows on each version, using rules that name
rtk; RTK_BINpointing at a file namedrtk-nightly;RTK_BINset while a different program namedrtkis onPATH;- rtk on
PATHwith a differentrtkin~/.cargo/bin; - an
RTK_BINpath with a space, and one with an apostrophe.
The shell-hook payloads were captured on 1.18.10, 1.19.5, 2.0.0, 2.0.26 and 2.3.0. Invariants: every decision; the command OpenCode judged (1.18.10 logs pattern="rtk ls -la"). No savings figure: the change is transport only.
Pins: on 92629e053, 18 mutations of the new decisions, 14 killed. Survivors:
- probing the install directories before
PATH: reachable, and now pinned by the fixup; - the
envdefaults in both shell hooks, and the case of noPATHat all: no tested OpenCode version reaches them. 1.x passes{env: {}}, and 2.x passes its whole environment.
On the fixup: 7 mutations, 7 killed.
Matrix: executed this round on 92629e053 and on the fixup; pin check executed this round.
CI @5b62da02: all green (16/16). On 92629e053: 15 green, check-target skipped. CI does not run hooks/opencode/rtk.test.mjs yet (#4588, PR #4529).
Blocks merge — round 1's three are closed, plus one new item in 92629e053, fixed by fixup
-
Closed. Every
rtkin a rewrite runs.cd sub && ls -la,FOO=1 ls -la,ls -la && cat a.txtandls; cat a.txtreturn rtk output with rtk in~/.cargo/binand with rtk atRTK_BIN, on 1.18.10 and on 2.3.0. -
Closed. OpenCode judges rtk's answer as rtk judged it. The rewrite stays a bare
rtk ls -la, and the ask and allow rows give develop's verdicts on both versions. -
Closed. The decisions are pinned (see Pins), and test (a) no longer depends on the developer's
HOME. -
New in
92629e053: anRTK_BINthe shell cannot reach as a barertk.RTK_BIN=…/rtk-nightlywith no rtk onPATH:rtk: command not found(exit 127 on 2.3.0). Develop ran the command raw.RTK_BIN=…/rtkwhile a different program namedrtkis onPATH: thePATHone runs, givingOTHER-RTK ls -la(exit 2). Develop disabled itself and ran the command raw.
The second case follows from my round-1 question about whether
RTK_BINshould come first. With the shell resolving a barertkthroughPATHand rtk's directory appended last, it cannot. That makes it mine to fix, so I pushed it as a fixup.
Fixup 5b62da02
resolveRtkPathchecksPATHfirst, because that rtk is the one every rewrite runs.RTK_BINis then used when it is an absolute path to a file namedrtk. Otherwise a warning says why it was ignored, and when aPATHrtk overrides it, the warning names that rtk.- The install directories come after both.
- Tests pin both orders (
PATHoverRTK_BINand over~/.cargo/bin;RTK_BINover~/.cargo/bin) and the skipped shapes (another file name, a relative path, a directory namedrtk). - The shell-
PATHtest skips where/usr/binholds an rtk, as test (a) already does. - The README and the comments say "keeps every command that already resolves on
PATHresolving to the same binary", and that the other programs in rtk's directory become reachable too. They no longer say "changes nothing for any other command", which was not true.
On the fixup, with OpenCode 1.18.10 and 2.3.0:
- the
rtk-nightlyrow runs raw, with the warning; - the foreign-
rtkrow runs raw, as develop does; - every other row is unchanged from
92629e053.
Optional (will not hold merge)
- On Windows,
candidateRtkDirs()also lists/opt/homebrew/bin,/usr/local/binand/usr/bin.path.win32turns them into\usr\bin\rtk.EXEand similar, which resolve against the current drive's root (C:\usr\bin\rtk.EXE). Any local user can create that directory, and it is probed before%LOCALAPPDATA%\cargo\bin. Listing those three only whenprocess.platform !== "win32"closes this. I have not run it on Windows. - The 1.x hook test went from
deepEqual(names, ["tool.execute.before"])to twoincludeschecks, so an extra hook would no longer fail it. candidateRtkDirs()uses~/.cargo/binand ignoresCARGO_HOME. Acargo install rtkunder a customCARGO_HOMEis still not found from a GUI launch.SHAPESand theinstallslists in the new tests are independent cases. Alphabetical order would make a missing or duplicated case easier to spot.
Filed as follow-ups
Checked and correct — no need to re-verify
- The shell hooks exist and get the payload the code expects. 1.18.10 and 1.19.5 call
shell.envwith{env: {}}; 2.0.0, 2.0.26 and 2.3.0 callcreate.beforewith{command, cwd, timeout, shell, env}, whereenvcarriesPATH. - Appending rather than prepending: with rtk on
PATHand a differentrtkin~/.cargo/bin, thePATHone runs. - With rtk on
PATH, every row matches develop. RTK_BINpaths with a space or an apostrophe now run.
Your questions
- None asked.
Next
Fixup 5b62da02 pushed and code-reviewed. It puts PATH before RTK_BIN, and uses RTK_BIN only when the shell can reach it as rtk. Approving.
Rounds: 2/3. Threads: 0 open. The three round-1 threads were resolved by you, and the rows above confirm each fix.
Summary
Fixes #4462: OpenCode plugin resolves rtk when launched from GUI environments (macOS Dock, Linux desktop entries) whose
PATHlacks the install directory, without modifying rewritten command strings or breaking permission rules. Plugin-only change: no Rust code is modified.Problem
When OpenCode is launched from a desktop GUI launcher, it does not inherit user shell profiles (
.profile,.bashrc), soPATHmay lack~/.cargo/bin,~/.local/bin, or/opt/homebrew/bin.Finding rtk elsewhere is not enough:
rtk hook opencodeanswers a barertk …, in any position (cd sub && rtk ls,FOO=1 rtk ls,rtk ls && rtk read a), and OpenCode's shell then fails withrtk: command not found. Splicing absolute paths into the rewritten command string (/path/to/rtk ...) breaks OpenCode permission evaluation (which expectsrtk *globs, #4195) and misses every non-leadingrtk.Solution (Shape A)
hooks/opencode/rtk.ts:resolveRtkPath()checksRTK_BINfirst (explicit override, so it wins over an rtk onPATH), then OpenCode'sPATH(PATHEXTon Windows), thencandidateRtkDirs():~/.cargo/bin,~/.local/bin(install.sh's default),/opt/homebrew/bin,/usr/local/bin,/usr/bin, and on Windows%LOCALAPPDATA%\cargo\bin,%ProgramFiles%\rtk,%ProgramFiles(x86)%\rtk. A directory namedrtkis never returned.rtk hook opencodeanswered it, so OpenCode judges the same string rtk judged and every multi-command shape keeps its barertk.appendRtkDirToPath()appends the resolved rtk directory to the end of the spawned shell'sPATHvia OpenCode 2.x'sctx.shell.hook("create.before")and OpenCode 1.x'sshell.envhook. It starts from OpenCode's ownPATHwhen the hook's env has none, reuses the key the environment already has (Pathon Windows, so the merged env never carries two case-variantPATHkeys), and does nothing when the directory is already there. Appending rather than prepending ensures no other command resolves differently.isRtkOnPath()export, the duplicated probe loop and the unreachablerewritten === "rtk"arm.hooks/opencode/README.md:RTK_BIN, the probed directories, and why the rewrite stays a barertk …with the shellPATHappend.hooks/opencode/rtk.test.mjs(tempHOME,PATHwithout rtk):~/.cargo/bin, only in~/.local/bin, only atRTK_BIN: the hooks' env is OpenCode'sPATHplus rtk's directory, and a real shell run with that env resolves everyrtkinrtk …,cd . && rtk …,FOO=1 rtk …,rtk … && rtk …,rtk …; rtk …andenv rtk …(cmd.exe equivalents on Windows).PATH, in~/.cargo/binor atRTK_BIN(2.x and 1.x hooks), so a rule namingrtkkeeps its verdict.RTK_BINwins overPATH; a directory atRTK_BINor namedrtkis skipped; rtk onPATHleaves the shellPATHunchanged.HOME.PATHappend a no-op, dropping a shell hook, dropping a candidate directory or theRTK_BINbranch, prepending instead of appending, or splicing an absolute path each turns a test red.Acceptance Criteria
~/.cargo/bin, not on OpenCode'sPATH→ls -laand chained commands rewritten and runRTK_BIN→ samePATH→ unchanged behaviorrtk *) evaluate the same string as with rtk onPATHTests:
node --test hooks/opencode/rtk.test.mjs(CI does not run this suite yet, #4588).