fix(pty): graceful degradation when bun-pty native library unavailable#34
Merged
fix(pty): graceful degradation when bun-pty native library unavailable#34
Conversation
Auto-resolve BUN_PTY_LIB by probing .opencode/node_modules/ and other paths that bun-pty's resolveLibPath() misses. Load bun-pty dynamically so the plugin still works (without PTY tools) when the native library can't be found. Fixes #20 See: anomalyco/opencode#10556
There was a problem hiding this comment.
1 issue found across 11 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/tools/pty/pty-loader.ts">
<violation number="1" location="src/tools/pty/pty-loader.ts:90">
P2: `loadBunPty()` has a concurrency race: concurrent callers can get `null` while the first import is still in flight. Cache and return the in-flight Promise instead of returning `cachedModule` immediately once `loadAttempted` is set.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| * Returns null if bun-pty cannot be loaded (native library missing, etc.) | ||
| */ | ||
| export async function loadBunPty(): Promise<BunPtyModule | null> { | ||
| if (loadAttempted) return cachedModule; |
There was a problem hiding this comment.
P2: loadBunPty() has a concurrency race: concurrent callers can get null while the first import is still in flight. Cache and return the in-flight Promise instead of returning cachedModule immediately once loadAttempted is set.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/tools/pty/pty-loader.ts, line 90:
<comment>`loadBunPty()` has a concurrency race: concurrent callers can get `null` while the first import is still in flight. Cache and return the in-flight Promise instead of returning `cachedModule` immediately once `loadAttempted` is set.</comment>
<file context>
@@ -0,0 +1,123 @@
+ * Returns null if bun-pty cannot be loaded (native library missing, etc.)
+ */
+export async function loadBunPty(): Promise<BunPtyModule | null> {
+ if (loadAttempted) return cachedModule;
+ loadAttempted = true;
+
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BUN_PTY_LIBby probing.opencode/node_modules/and other paths that bun-pty'sresolveLibPath()missesFixes #20
Problem
When micode is installed as an OpenCode plugin,
bun-pty's native library (librust_pty.so/.dylib) ends up in.opencode/node_modules/bun-pty/...— a path not in bun-pty's hardcoded search list. This causes the plugin to crash on startup.Root cause is upstream: anomalyco/opencode#10556
Changes
New:
src/tools/pty/pty-loader.ts.opencode/node_modules/,.micode/node_modules/,require.resolve) and setsBUN_PTY_LIBenv var before importimport("bun-pty")in try/catch for graceful degradationModified:
src/tools/pty/manager.tsimport { spawn } from "bun-pty"(would crash at module load)init(spawnFn)— spawn function is injected after successful loadingavailablegetter for callers to check PTY statusModified:
src/index.tsloadBunPty()to dynamically load with path probingTests
manager.init(spawn)patternManual workaround
If auto-resolution doesn't cover a specific setup:
export BUN_PTY_LIB=.opencode/node_modules/bun-pty/rust-pty/target/release/librust_pty.dylib