From 3f16057a4b5b31e9a2a22413ea393f1386b380ce Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Wed, 18 Feb 2026 15:49:44 +0900 Subject: [PATCH] fix(cli-run): skip unresolved opencode bin path injection --- src/cli/run/opencode-bin-path.test.ts | 21 +++++++++++++++++++-- src/cli/run/opencode-bin-path.ts | 10 ++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/cli/run/opencode-bin-path.test.ts b/src/cli/run/opencode-bin-path.test.ts index ad3350da..20e3a24c 100644 --- a/src/cli/run/opencode-bin-path.test.ts +++ b/src/cli/run/opencode-bin-path.test.ts @@ -10,9 +10,10 @@ describe("prependResolvedOpencodeBinToPath", () => { PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin", } const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode" + const pathExists = () => true //#when - prependResolvedOpencodeBinToPath(env, resolver) + prependResolvedOpencodeBinToPath(env, resolver, pathExists) //#then expect(env.PATH).toBe( @@ -26,9 +27,10 @@ describe("prependResolvedOpencodeBinToPath", () => { PATH: "/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin", } const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode" + const pathExists = () => true //#when - prependResolvedOpencodeBinToPath(env, resolver) + prependResolvedOpencodeBinToPath(env, resolver, pathExists) //#then expect(env.PATH).toBe("/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin") @@ -49,4 +51,19 @@ describe("prependResolvedOpencodeBinToPath", () => { //#then expect(env.PATH).toBe("/Users/yeongyu/node_modules/.bin:/usr/bin") }) + + it("keeps PATH unchanged when resolved binary path does not exist", () => { + //#given + const env: Record = { + PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin", + } + const resolver = () => "/Users/yeongyu/node_modules/opencode-ai/bin/opencode" + const pathExists = () => false + + //#when + prependResolvedOpencodeBinToPath(env, resolver, pathExists) + + //#then + expect(env.PATH).toBe("/Users/yeongyu/node_modules/.bin:/usr/bin") + }) }) diff --git a/src/cli/run/opencode-bin-path.ts b/src/cli/run/opencode-bin-path.ts index 1a625cd9..bd7c6235 100644 --- a/src/cli/run/opencode-bin-path.ts +++ b/src/cli/run/opencode-bin-path.ts @@ -1,5 +1,6 @@ import { delimiter, dirname } from "node:path" import { createRequire } from "node:module" +import { existsSync } from "node:fs" type EnvLike = Record @@ -8,6 +9,7 @@ const resolveFromCurrentModule = createRequire(import.meta.url).resolve export function prependResolvedOpencodeBinToPath( env: EnvLike = process.env, resolve: (id: string) => string = resolveFromCurrentModule, + pathExists: (path: string) => boolean = existsSync, ): void { let resolvedPath: string try { @@ -16,7 +18,15 @@ export function prependResolvedOpencodeBinToPath( return } + if (!pathExists(resolvedPath)) { + return + } + const opencodeBinDir = dirname(resolvedPath) + if (!pathExists(opencodeBinDir)) { + return + } + const currentPath = env.PATH ?? "" const pathSegments = currentPath ? currentPath.split(delimiter) : []