opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Bun.$ call in a tool fails when the same code works when called with bun directly

Open nagyv opened this issue 1 month ago • 1 comments

Question

I'm trying to write a custom tool that simply wraps a CLI (the goal is to give access to OpenCode to this CLI, but not to Bash in general).

The CLI in this case is the GitLab CLI, glab, but my question is likely independent of it.

My code currently is:

import { tool } from "@opencode-ai/plugin"

export default tool({
    description: "Wrapper for the `glab` CLI. Call `glab` with this tool.",
    args: {
        command: tool.schema.string().default("help").describe("The glab command to call. Example: `mr view -F json 32` "),
    },
    async execute(args) {
        const parts = args.command.split(/\s+/)
        let output
        try {
            output = await Bun.$`glab ${{ raw: args.command }}`.text();
        } catch (err) {
            const error = [];
            error.push(`Failed with code ${err.exitCode}`);
            error.push(err.stdout.toString());
            error.push(err.stderr.toString());
            return error.join("\n");
        }
        return output.toString().trim()
    },
})

Running the equivalend code through Bash+Bun directly works. When I run it as a tool call in OpenCode it returns a ShellError: Failed with exit code 1.

I'm trying to debug it for a while, but it's really hard to debug a tool call that happens in OpenCode. Any ideas or pointers?

nagyv avatar Dec 10 '25 21:12 nagyv

Can you show the error? I don't have glab installed but if I have the llm call the tool then I do:

opencode export

and read previous session json, i see this:

  {
          "id": "prt_b11408bc00019kuDWjLDRMkIIN",
          "sessionID": "ses_4eebf7d09ffevbkNx5dHD1TsfL",
          "messageID": "msg_b1140830e001gwUiiyHRMKJ2a2",
          "type": "tool",
          "callID": "toolu_01FVQ6zdat3kf5rG28x1GF5a",
          "tool": "glab",
          "state": {
            "status": "completed",
            "input": {
              "command": "help"
            },
            "output": "Failed with code 1\n\nbun: command not found: glab\n",
            "title": "",
            "metadata": {},
            "time": {
              "start": 1765521001409,
              "end": 1765521001409
            }
          }
        },

Which is expected.

rekram1-node avatar Dec 12 '25 06:12 rekram1-node