|
| 1 | +// src/tools/pty/pty-loader.ts |
| 2 | +// Resolves bun-pty native library path and loads bun-pty with graceful degradation. |
| 3 | +// |
| 4 | +// bun-pty's resolveLibPath() checks BUN_PTY_LIB env var first, then hardcoded paths |
| 5 | +// relative to import.meta.url. When micode is installed as an OpenCode plugin, |
| 6 | +// the library ends up in .opencode/node_modules/bun-pty/... which isn't in the |
| 7 | +// hardcoded search paths. We fix this by probing likely locations and setting |
| 8 | +// BUN_PTY_LIB before the dynamic import. |
| 9 | +// |
| 10 | +// See: https://github.com/vtemian/micode/issues/20 |
| 11 | +// See: https://github.com/anomalyco/opencode/issues/10556 |
| 12 | + |
| 13 | +import { existsSync } from "node:fs"; |
| 14 | +import { dirname, join } from "node:path"; |
| 15 | + |
| 16 | +import { log } from "../../utils/logger"; |
| 17 | + |
| 18 | +type BunPtyModule = typeof import("bun-pty"); |
| 19 | + |
| 20 | +let cachedModule: BunPtyModule | null = null; |
| 21 | +let loadAttempted = false; |
| 22 | +let loadError: string | null = null; |
| 23 | + |
| 24 | +/** |
| 25 | + * Probe additional paths where the bun-pty native library might live, |
| 26 | + * beyond what bun-pty checks itself. Sets BUN_PTY_LIB if found. |
| 27 | + */ |
| 28 | +function probeBunPtyLib(): void { |
| 29 | + // If already set by user, respect it |
| 30 | + if (process.env.BUN_PTY_LIB) return; |
| 31 | + |
| 32 | + const platform = process.platform; |
| 33 | + const arch = process.arch; |
| 34 | + |
| 35 | + const filenames = |
| 36 | + platform === "darwin" |
| 37 | + ? arch === "arm64" |
| 38 | + ? ["librust_pty_arm64.dylib", "librust_pty.dylib"] |
| 39 | + : ["librust_pty.dylib"] |
| 40 | + : platform === "win32" |
| 41 | + ? ["rust_pty.dll"] |
| 42 | + : arch === "arm64" |
| 43 | + ? ["librust_pty_arm64.so", "librust_pty.so"] |
| 44 | + : ["librust_pty.so"]; |
| 45 | + |
| 46 | + const cwd = process.cwd(); |
| 47 | + |
| 48 | + // Paths that bun-pty does NOT check but where the lib may exist |
| 49 | + // when installed as an OpenCode plugin dependency |
| 50 | + const additionalBasePaths = [ |
| 51 | + // .opencode/node_modules/bun-pty/... (plugin installed via .opencode/package.json) |
| 52 | + join(cwd, ".opencode", "node_modules", "bun-pty", "rust-pty", "target", "release"), |
| 53 | + // .micode/node_modules/bun-pty/... (if micode has its own node_modules) |
| 54 | + join(cwd, ".micode", "node_modules", "bun-pty", "rust-pty", "target", "release"), |
| 55 | + ]; |
| 56 | + |
| 57 | + // Also try resolving from require.resolve if available |
| 58 | + try { |
| 59 | + const bunPtyMain = require.resolve("bun-pty"); |
| 60 | + if (bunPtyMain) { |
| 61 | + // require.resolve gives us something like .../node_modules/bun-pty/src/index.ts |
| 62 | + // Go up to the bun-pty package root |
| 63 | + const pkgDir = dirname(dirname(bunPtyMain)); |
| 64 | + additionalBasePaths.unshift(join(pkgDir, "rust-pty", "target", "release")); |
| 65 | + } |
| 66 | + } catch { |
| 67 | + // require.resolve may fail in some environments |
| 68 | + } |
| 69 | + |
| 70 | + for (const basePath of additionalBasePaths) { |
| 71 | + for (const filename of filenames) { |
| 72 | + const candidate = join(basePath, filename); |
| 73 | + if (existsSync(candidate)) { |
| 74 | + process.env.BUN_PTY_LIB = candidate; |
| 75 | + log.info("pty.loader", `Auto-resolved BUN_PTY_LIB=${candidate}`); |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Dynamically load bun-pty with graceful degradation. |
| 84 | + * Sets BUN_PTY_LIB env var before import to fix path resolution |
| 85 | + * in OpenCode plugin environments. |
| 86 | + * |
| 87 | + * Returns null if bun-pty cannot be loaded (native library missing, etc.) |
| 88 | + */ |
| 89 | +export async function loadBunPty(): Promise<BunPtyModule | null> { |
| 90 | + if (loadAttempted) return cachedModule; |
| 91 | + loadAttempted = true; |
| 92 | + |
| 93 | + // Probe and set BUN_PTY_LIB before importing |
| 94 | + probeBunPtyLib(); |
| 95 | + |
| 96 | + try { |
| 97 | + cachedModule = await import("bun-pty"); |
| 98 | + log.info("pty.loader", "bun-pty loaded successfully"); |
| 99 | + return cachedModule; |
| 100 | + } catch (error) { |
| 101 | + loadError = error instanceof Error ? error.message : String(error); |
| 102 | + // Extract just the first line for a cleaner warning |
| 103 | + const firstLine = loadError.split("\n")[0]; |
| 104 | + log.warn("pty.loader", `bun-pty unavailable: ${firstLine}`); |
| 105 | + log.warn("pty.loader", "PTY tools will be disabled. Set BUN_PTY_LIB env var to the native library path to fix."); |
| 106 | + cachedModule = null; |
| 107 | + return null; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Check if bun-pty is available (must call loadBunPty first). |
| 113 | + */ |
| 114 | +export function isBunPtyAvailable(): boolean { |
| 115 | + return cachedModule !== null; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Get the load error message, if any. |
| 120 | + */ |
| 121 | +export function getBunPtyLoadError(): string | null { |
| 122 | + return loadError; |
| 123 | +} |
0 commit comments