Skip to content

fix(opencode): resolve rtk binary path for GUI-launched OpenCode (#4462) - #4507

Merged
KuSh merged 5 commits into
rtk-ai:developfrom
TechWizard9999:fix/4462-opencode-path-resolution
Nov 16, 2026
Merged

KuSh merged 5 commits into
rtk-ai:developfrom
TechWizard9999:fix/4462-opencode-path-resolution

Conversation

@TechWizard9999

@TechWizard9999 TechWizard9999 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #4462: OpenCode plugin resolves rtk when launched from GUI environments (macOS Dock, Linux desktop entries) whose PATH lacks 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), so PATH may lack ~/.cargo/bin, ~/.local/bin, or /opt/homebrew/bin.

Finding rtk elsewhere is not enough: rtk hook opencode answers a bare rtk …, in any position (cd sub && rtk ls, FOO=1 rtk ls, rtk ls && rtk read a), and OpenCode's shell then fails with rtk: command not found. Splicing absolute paths into the rewritten command string (/path/to/rtk ...) breaks OpenCode permission evaluation (which expects rtk * globs, #4195) and misses every non-leading rtk.

Solution (Shape A)

  1. hooks/opencode/rtk.ts:
    • resolveRtkPath() checks RTK_BIN first (explicit override, so it wins over an rtk on PATH), then OpenCode's PATH (PATHEXT on Windows), then candidateRtkDirs(): ~/.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 named rtk is never returned.
    • The rewrite is applied exactly as rtk hook opencode answered it, so OpenCode judges the same string rtk judged and every multi-command shape keeps its bare rtk.
    • appendRtkDirToPath() appends the resolved rtk directory to the end of the spawned shell's PATH via OpenCode 2.x's ctx.shell.hook("create.before") and OpenCode 1.x's shell.env hook. It starts from OpenCode's own PATH when the hook's env has none, reuses the key the environment already has (Path on Windows, so the merged env never carries two case-variant PATH keys), and does nothing when the directory is already there. Appending rather than prepending ensures no other command resolves differently.
    • Removed the unused isRtkOnPath() export, the duplicated probe loop and the unreachable rewritten === "rtk" arm.
  2. hooks/opencode/README.md:
    • Documents the lookup order, RTK_BIN, the probed directories, and why the rewrite stays a bare rtk … with the shell PATH append.
  3. hooks/opencode/rtk.test.mjs (temp HOME, PATH without rtk):
    • rtk only in ~/.cargo/bin, only in ~/.local/bin, only at RTK_BIN: the hooks' env is OpenCode's PATH plus rtk's directory, and a real shell run with that env resolves every rtk in rtk …, cd . && rtk …, FOO=1 rtk …, rtk … && rtk …, rtk …; rtk … and env rtk … (cmd.exe equivalents on Windows).
    • The command OpenCode evaluates is byte-identical whether rtk is on PATH, in ~/.cargo/bin or at RTK_BIN (2.x and 1.x hooks), so a rule naming rtk keeps its verdict.
    • RTK_BIN wins over PATH; a directory at RTK_BIN or named rtk is skipped; rtk on PATH leaves the shell PATH unchanged.
    • The "nothing found" test no longer depends on the developer's HOME.
    • Making the PATH append a no-op, dropping a shell hook, dropping a candidate directory or the RTK_BIN branch, prepending instead of appending, or splicing an absolute path each turns a test red.

Acceptance Criteria

  • ✅ rtk only in ~/.cargo/bin, not on OpenCode's PATH → ls -la and chained commands rewritten and run
  • ✅ rtk only at RTK_BIN → same
  • ✅ rtk on PATH → unchanged behavior
  • ✅ OpenCode permissions (rtk *) evaluate the same string as with rtk on PATH

Tests: node --test hooks/opencode/rtk.test.mjs (CI does not run this suite yet, #4588).

@rtk-release-bot rtk-release-bot Bot added the wrong-base PR targets master instead of develop label Oct 10, 2026
@rtk-release-bot

Copy link
Copy Markdown
Contributor

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.

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes 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

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4462


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@TechWizard9999
TechWizard9999 changed the base branch from master to develop October 23, 2026 18:34
@TechWizard9999

TechWizard9999 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Retargeted base to develop and resolved the CI formatting issue. @KuSh @aeppling, could you please review this PR when you get a chance? Thanks!

@TechWizard9999
TechWizard9999 force-pushed the fix/4462-opencode-path-resolution branch from da01fd0 to dad2649 Compare October 30, 2026 05:55
@TechWizard9999

Copy link
Copy Markdown
Contributor Author

Gentle reminder @KuSh, could you please review this PR when you have a moment? Thanks!

@KuSh KuSh self-assigned this Nov 1, 2026

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

  1. hooks/opencode/rtk.ts:212: only the first rtk of a rewrite gets the path. rtk hook opencode puts rtk wherever 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. The startsWith("rtk ") arm reaches none of the later ones, so with rtk outside PATH they now fail, where develop ran them unrewritten:

    command (rtk only in ~/.cargo/bin) develop this head
    ls -la raw ls -la rtk output ✓
    cd sub && ls -la raw /usr/bin/bash: line 1: rtk: command not found
    FOO=1 ls -la raw rtk: command not found
    ls -la && cat a.txt raw rtk ls output, then rtk: command not found
    ls; cat a.txt raw ls output, then rtk: command not found

    The same four rows fail with rtk only at RTK_BIN; with rtk on PATH, all five behave as on develop. Agents chain commands with cd … && constantly, so most real calls go from a working pass-through to a failed command.

  2. hooks/opencode/rtk.ts:208: OpenCode judges a different command from the one rtk judged. rtk hook opencode only answers a rewrite after checking that rtk <cmd> gets the same verdict as <cmd> under OpenCode's own rules (opencode_answer in src/hooks/hook_cmd.rs, #4195). The plugin then replaces rtk with an absolute path, and OpenCode evaluates that new string, which a rule naming rtk no longer matches:

    • {"*":"allow","ls *":"ask","rtk *":"ask"}, command ls -la. Develop asks, whether rtk is on PATH or not. This head, with rtk in ~/.cargo/bin, runs it without asking. OpenCode logs pattern="…/.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.

  3. hooks/opencode/rtk.ts:71: unpinned behaviour, and a test that still asserts the old one. The PR adds no test. These mutations all pass node --test hooks/opencode/rtk.test.mjs (run with a clean HOME): drop the RTK_BIN branch; accept a directory as RTK_BIN or as a candidate; drop the ~/.cargo/bin candidate; never prefix (if (!cachedRtkOnPath) → if (false)); drop the quoting; drop either rewrite arm; check RTK_BIN before PATH. Only flipping the on-PATH flag is caught, by the existing tests that expect a bare rtk ls -la. Separately, test (a) in an rtk without hook opencode registers nothing still asserts resolveRtkPath() === null when nothing is on PATH. 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, with HOME pointed at a temp dir and PATH empty:

    • rtk only in $HOME/.cargo/bin: ls -la and a chained command both come back runnable against that binary;
    • the same with only RTK_BIN set;
    • rtk on PATH: rtk ls -la is 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.

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 rtk resolvable in the spawned shell: the 1.x shell.env hook and the 2.x ctx.shell.hook("create.before"), appending the resolved directory to PATH. Appending rather than prepending, so no other command resolves differently. The rewrite stays rtk …, so OpenCode judges the same string rtk judged, and every segment resolves.
  • (B) have rtk hook opencode emit the absolute path itself and judge that string. This is what the PR description says (opencode_answer() with bin_path, current_exe() in main.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_BIN at …/bin'quote/rtk, the command fails with bash: -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 opencode never answers a bare rtk.
  • isRtkOnPath() is exported but nothing calls it. The PATH loop and the candidate loop are the same probe written twice.
  • The candidates leave out ~/.local/bin, which is install.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_BIN is checked after PATH, so it cannot select a build over an rtk that is already on PATH. 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 from PATH only. RTK_BIN and the probed directories are not documented.
  • The PR description lists changes to src/hooks/hook_cmd.rs and src/main.rs, and Rust test updates, that are not in the commit. Please make it match what is pushed.

Filed as follow-ups

  • None new. CI not running the plugin suite is #4588 (PR #4529).

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/bin only and RTK_BIN only are rewritten and run; on PATH is unchanged.
  • An RTK_BIN path 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.

Comment thread hooks/opencode/rtk.ts Resolved
Comment thread hooks/opencode/rtk.ts Resolved
Comment thread hooks/opencode/rtk.ts Resolved
TechWizard9999 and others added 4 commits November 2, 2026 10:25
…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 KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_BIN pointing at a file named rtk-nightly;
  • RTK_BIN set while a different program named rtk is on PATH;
  • rtk on PATH with a different rtk in ~/.cargo/bin;
  • an RTK_BIN path 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 env defaults in both shell hooks, and the case of no PATH at 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

  1. Closed. Every rtk in a rewrite runs. cd sub && ls -la, FOO=1 ls -la, ls -la && cat a.txt and ls; cat a.txt return rtk output with rtk in ~/.cargo/bin and with rtk at RTK_BIN, on 1.18.10 and on 2.3.0.

  2. 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.

  3. Closed. The decisions are pinned (see Pins), and test (a) no longer depends on the developer's HOME.

  4. New in 92629e053: an RTK_BIN the shell cannot reach as a bare rtk.

    • RTK_BIN=…/rtk-nightly with no rtk on PATH: rtk: command not found (exit 127 on 2.3.0). Develop ran the command raw.
    • RTK_BIN=…/rtk while a different program named rtk is on PATH: the PATH one runs, giving OTHER-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_BIN should come first. With the shell resolving a bare rtk through PATH and rtk's directory appended last, it cannot. That makes it mine to fix, so I pushed it as a fixup.

Fixup 5b62da02

  • resolveRtkPath checks PATH first, because that rtk is the one every rewrite runs.
  • RTK_BIN is then used when it is an absolute path to a file named rtk. Otherwise a warning says why it was ignored, and when a PATH rtk overrides it, the warning names that rtk.
  • The install directories come after both.
  • Tests pin both orders (PATH over RTK_BIN and over ~/.cargo/bin; RTK_BIN over ~/.cargo/bin) and the skipped shapes (another file name, a relative path, a directory named rtk).
  • The shell-PATH test skips where /usr/bin holds an rtk, as test (a) already does.
  • The README and the comments say "keeps every command that already resolves on PATH resolving 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-nightly row runs raw, with the warning;
  • the foreign-rtk row 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/bin and /usr/bin. path.win32 turns them into \usr\bin\rtk.EXE and 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 when process.platform !== "win32" closes this. I have not run it on Windows.
  • The 1.x hook test went from deepEqual(names, ["tool.execute.before"]) to two includes checks, so an extra hook would no longer fail it.
  • candidateRtkDirs() uses ~/.cargo/bin and ignores CARGO_HOME. A cargo install rtk under a custom CARGO_HOME is still not found from a GUI launch.
  • SHAPES and the installs lists in the new tests are independent cases. Alphabetical order would make a missing or duplicated case easier to spot.

Filed as follow-ups

  • None new. The plugin suite not running in CI is #4588 (PR #4529).

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.env with {env: {}}; 2.0.0, 2.0.26 and 2.3.0 call create.before with {command, cwd, timeout, shell, env}, where env carries PATH.
  • Appending rather than prepending: with rtk on PATH and a different rtk in ~/.cargo/bin, the PATH one runs.
  • With rtk on PATH, every row matches develop.
  • RTK_BIN paths 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.

@KuSh
KuSh merged commit 2f90f4a into rtk-ai:develop Nov 16, 2026
16 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ci dependencies opencode path-resolution wrong-base PR targets master instead of develop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenCode plugin: no rewrite when rtk is not on OpenCode's PATH (Desktop launched from the macOS GUI)

2 participants