Skip to content

fix: wait for dependencies before loading custom tools and plugins#12227

Merged
thdxr merged 1 commit intodevfrom
fix/tool-dependency-race-condition
Feb 5, 2026
Merged

fix: wait for dependencies before loading custom tools and plugins#12227
thdxr merged 1 commit intodevfrom
fix/tool-dependency-race-condition

Conversation

@thdxr
Copy link
Contributor

@thdxr thdxr commented Feb 5, 2026

Summary

Fixes a race condition where custom tools with external dependencies in their package.json would crash on import because dependencies weren't installed yet.

Problem

When a custom tool file had an import like:

import { say } from 'cowsay'

at the top level, and the tool directory had a package.json with cowsay as a dependency, the tool would crash with "Cannot find package 'cowsay'" because dependencies weren't installed before the module was imported.

Solution

  1. Config.state() now collects dependency installation promises in a deps array using iife()
  2. Added Config.waitForDependencies() to await all installations before proceeding
  3. ToolRegistry calls waitForDependencies() before loading custom tools (if tools exist)
  4. Plugin.list() calls waitForDependencies() before loading plugins (if plugins exist)

Changes

  • packages/opencode/src/config/config.ts: Track deps array and add wait function
  • packages/opencode/src/tool/registry.ts: Wait for deps before loading tools
  • packages/opencode/src/plugin/index.ts: Wait for deps before loading plugins
  • packages/opencode/test/tool/registry.test.ts: Added test verifying tools with external deps (cowsay) load successfully

Testing

Added a test that creates a tool with cowsay as a dependency and imports it at the top level. The test verifies the tool loads without crashing.

Fixes a race condition where custom tools with external dependencies in their
package.json would crash on import because dependencies weren't installed yet.

Changes:
- Config.state() now collects dependency installation promises in a deps array
- Added Config.waitForDependencies() to await all installations
- ToolRegistry calls waitForDependencies() before loading custom tools
- Plugin.list() calls waitForDependencies() before loading plugins
- Added test to verify tools with external deps (cowsay) load successfully
@thdxr thdxr enabled auto-merge (squash) February 5, 2026 03:23
@thdxr thdxr merged commit 556adad into dev Feb 5, 2026
6 checks passed
@thdxr thdxr deleted the fix/tool-dependency-race-condition branch February 5, 2026 03:25
01luyicheng pushed a commit to 01luyicheng/opencode that referenced this pull request Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant