fix(cli-run): skip unresolved opencode bin path injection

This commit is contained in:
YeonGyu-Kim 2026-02-18 15:49:44 +09:00
parent 9c5d80af1d
commit 3f16057a4b
2 changed files with 29 additions and 2 deletions

View File

@ -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<string, string | undefined> = {
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")
})
})

View File

@ -1,5 +1,6 @@
import { delimiter, dirname } from "node:path"
import { createRequire } from "node:module"
import { existsSync } from "node:fs"
type EnvLike = Record<string, string | undefined>
@ -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) : []