Skip to content

Default cwd should does not work for opencode desktop #4

@jessielaf

Description

@jessielaf

Problem

When opencode is launched as the macOS desktop app (via Finder, Dock, or Spotlight), the claude CLI subprocess spawned by this plugin runs with its working directory set to / instead of the project directory.

opencode itself shows the correct project in its UI (e.g. ~/projects/foo/foo), and models that don't go through this plugin (tested with non-claude-code providers in the same opencode session) operate against the correct project directory. Only the Claude CLI subprocess ends up at /.

File operations, .claude/ discovery, and CLAUDE.md lookup all resolve against / as a result.

Image Image

Reproduction

  1. Install the opencode desktop app on macOS
  2. Add @khalilgharbaoui/opencode-claude-code-plugin to opencode.json
  3. Launch opencode from Finder/Dock (not from a terminal)
  4. Open any project, e.g. ~/projects/foo
  5. Ask Claude to run pwd. It reports /.
  6. Switch the model to a non-claude-code provider in the same project. File operations resolve correctly against the project directory.

What I think is happening

GUI-launched apps on macOS inherit / as their working directory from launchd. The plugin's cwd option defaults to process.cwd(), which in this case is /. Launching opencode from a terminal (cd ~/project && opencode) avoids the issue because the shell sets cwd before the process starts, but that's not workable when using the desktop app.

Other providers in opencode seem to handle the project directory correctly somehow, so opencode clearly knows where the project is. I haven't dug into the plugin source to confirm exactly where the fallback happens, so I might be off on the details.

Workaround

Adding a per-project opencode.json with an explicit cwd works.

{
  "provider": {
    "claude-code": {
      "options": {
        "cwd": "/absolute/path/to/project"
      }
    }
  }
}

AI Suggestion

This section was drafted with help from an AI assistant and may be inaccurate, treat as a hypothesis rather than a verified fix.

According to the opencode plugin docs, the plugin context object passed to the plugin factory includes directory, worktree, and project.root, all of which point to the active project. The fix could be to read from that context when resolving cwd, instead of falling back to process.cwd().

Roughly something like.

cwd: options.cwd ?? ctx.directory ?? ctx.project?.root ?? process.cwd()

Environment

  • opencode desktop app (macOS)
  • @khalilgharbaoui/opencode-claude-code-plugin (latest)
  • Claude Code CLI at ~/.local/bin/claude

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions