rtk icon indicating copy to clipboard operation
rtk copied to clipboard

OpenCode plugin missing default export — plugin never loaded by plugin loader

Open sacrtap opened this issue 5 months ago • 5 comments

Summary

The OpenCode plugin generated by rtk init -g --opencode exports only a named export (RtkOpenCodePlugin) but lacks a default export. OpenCode's plugin loader requires a default export to load the plugin module, so the RTK plugin is silently never loaded — the tool.execute.before hook never fires, and all bash commands execute without RTK rewriting.

Environment

  • rtk version: 0.42.4
  • opencode version: 1.17.8
  • OS: macOS (darwin)
  • Install method: Homebrew

Steps to Reproduce

  1. Run rtk init -g --opencode
  2. Check the generated plugin file: ~/.config/opencode/plugins/rtk.ts
  3. Start OpenCode and run git status via the bash tool
  4. Observe: command executes as raw git status, not rtk git status

Root Cause

OpenCode's plugin type definition (@opencode-ai/plugin) specifies:

export type PluginModule = {
    id?: string;
    server: Plugin;
    tui?: never;
};

The plugin loader expects a default export matching the Plugin type. The generated rtk.ts only has:

export const RtkOpenCodePlugin: Plugin = async ({ $ }) => { ... }
// ← missing: export default RtkOpenCodePlugin

Compare with a working plugin in the same directory (openviking-memory.ts), which has both:

export const OpenVikingMemoryPlugin = async (input: PluginInput): Promise<Hooks> => { ... }
export default OpenVikingMemoryPlugin  // ← this makes it load

Impact

  • 100% of OpenCode users who install via rtk init -g --opencode have RTK silently disabled
  • rtk init --show reports [ok] OpenCode: plugin installed — no warning that the plugin won't load
  • Users see no errors; commands just pass through unfiltered

Fix

Add export default RtkOpenCodePlugin to the end of the generated plugin template.

The source template is likely in src/ or hooks/ — wherever rtk init --opencode generates the rtk.ts content.

Verification

After manually adding export default RtkOpenCodePlugin to ~/.config/opencode/plugins/rtk.ts and restarting OpenCode:

  • git status → rewritten to rtk git status ✅
  • git diff → rewritten to rtk git diff ✅
  • docker ps → rewritten to rtk docker ps ✅
  • make test-parallel → rewritten to rtk make test-parallel ✅
  • playwright test → rewritten to rtk playwright test ✅
  • sudo npm run build → rewritten to sudo rtk npm run build ✅ (transparent prefix)

Suggested Improvement

Add a runtime check in rtk init --show that verifies the plugin file contains export default. If missing, warn the user.

sacrtap avatar Jun 19 '26 15:06 sacrtap

Adding export default RtkOpenCodePlugin to hooks/opencode/rtk.ts should fix this. The file gets embedded at build time and re-written on each rtk init -g --opencode, so it covers both fresh installs and existing setups.

molloyzak13 avatar Jun 19 '26 17:06 molloyzak13

Adding export default RtkOpenCodePlugin to hooks/opencode/rtk.ts should fix this. The file gets embedded at build time and re-written on each rtk init -g --opencode, so it covers both fresh installs and existing setups.

molloyzak13 avatar Jun 19 '26 17:06 molloyzak13

wshm · Automated triage by AI

🔍 Automated Triage

🐛 Category bug
🟠 Priority high
🎯 Confidence 97%

Summary

Generated OpenCode plugin template missing export default — RTK is silently disabled for 100% of OpenCode users; PR #2566 already open with a fix.

This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

📁 Relevant files
  • src/init/opencode_plugin_template.ts

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

pszymkowiak avatar Jun 25 '26 14:06 pszymkowiak

Hey team, just tried this in my local and it does not seem to be working as expected. Is there another solution to this problem? thks.

santiago1393 avatar Jul 19 '26 17:07 santiago1393

Heads up on the suggested fix in this thread: adding a bare export default RtkOpenCodePlugin resolves this on OpenCode 1.17.x (V1 loader), but it is no longer sufficient on current OpenCode, and it now fails differently.

On OpenCode 1.18.34 the TUI/desktop/shared server uses the V2 loader. With a bare default export you get past the first schema check and straight into the next one:

PluginModule.LoadError: Plugin must export a default definition with an id and an effect or setup function.
  (cause: SchemaError(Missing key at ["default"]["effect"] / ["default"]["setup"]))

The V2 loader requires the default export to carry an id plus setup() (or effect()), and it ignores server() entirely — so a V1 plugin exported as the default loads without error and then silently does nothing. That silent case is what #4311 and the discussion in #3463 cover.

Verified working shape on 1.18.34 + rtk 0.51.0 (both halves kept, so the V1 opencode run CLI keeps working too):

import { spawnSync } from "node:child_process"
import type { Plugin } from "@opencode-ai/plugin"

function rewriteCommand(command: string): string | undefined {
  try {
    const result = spawnSync("rtk", ["rewrite", command], {
      encoding: "utf8", timeout: 2000, maxBuffer: 1024 * 1024,
    })
    const rewritten = (result.stdout ?? "").trim()
    return rewritten && rewritten !== command ? rewritten : undefined
  } catch { return undefined }
}

async function setup(ctx) {
  await ctx.tool.hook("execute.before", async (event) => {
    const tool = String(event?.tool ?? "").toLowerCase()
    if (tool !== "bash" && tool !== "shell") return
    const input = event?.input
    if (!input || typeof input !== "object") return
    const command = input.command
    if (typeof command !== "string" || !command) return
    const rewritten = rewriteCommand(command)
    if (rewritten) event.input = { ...input, command: rewritten }
  })
}

export const RtkOpenCodePlugin: Plugin = async ({ $ }) => { /* unchanged V1 path */ }

export default { id: "rtk", setup, server: RtkOpenCodePlugin }

A plain object works in place of Plugin.define(...) — ~/.config/opencode/plugins/ has no node_modules, and the generated file's only import is type-only, so there is no runtime @opencode/plugin to resolve.

Full working notes and the three porting gotchas are in #3463. Suggest closing this in favour of that one.

dylldev avatar Oct 04 '26 00:10 dylldev