[PR #12227] fix: wait for dependencies before loading custom tools and plugins #14125

Closed
opened 2026-02-16 18:18:56 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12227

State: closed
Merged: Yes


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.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12227 **State:** closed **Merged:** Yes --- ## 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: ```typescript 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.
yindo added the pull-request label 2026-02-16 18:18:56 -05:00
yindo closed this issue 2026-02-16 18:18:56 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14125