Skip to content

fix: working directory incorrectly set to opencode package directory when running via bun fallback #22

@terisuke

Description

@terisuke

Bug Description

When OpenCode is launched from a user's project directory and falls back to running via bun (no prebuilt binary found), the working directory gets incorrectly set to the OpenCode package directory instead of the user's project directory.

Root Cause

In packages/opencode/bin/opencode, the bun() function spawns bun with cwd: dir, where dir is the OpenCode package directory (resolved from scriptDir). This is necessary so that ./src/index.ts resolves correctly. However, it means process.cwd() in the spawned process returns the OpenCode package path, not the user's original working directory.

All CLI commands call bootstrap(process.cwd(), ...), which then passes the wrong directory to Instance.provide().

Expected Behavior

When a user runs opencode from /Users/user/my-project, the instance should be initialized with /Users/user/my-project as the working directory, regardless of whether the bun fallback path is used.

Fix

  1. Capture the original CWD in bin/opencode before spawning bun and pass it as OPENCODE_ORIGINAL_CWD env var
  2. Resolve the env var centrally in bootstrap() so all commands benefit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions