diff --git a/packages/opencode/script/build.ts b/packages/opencode/script/build.ts index 1d3a3fac..4ce8bfba 100755 --- a/packages/opencode/script/build.ts +++ b/packages/opencode/script/build.ts @@ -41,7 +41,9 @@ for (const [os, arch] of targets) { const opentui = `@opentui/core-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}` await $`mkdir -p ../../node_modules/${opentui}` - await $`npm pack ${opentui}@${pkg.dependencies["@opentui/core"]}`.cwd(path.join(dir, "../../node_modules")) + await $`npm pack ${opentui}@${pkg.dependencies["@opentui/core"]}`.cwd( + path.join(dir, "../../node_modules"), + ) await $`tar -xf ../../node_modules/${opentui.replace("@opentui/", "opentui-")}-*.tgz -C ../../node_modules/${opentui} --strip-components=1` const watcher = `@parcel/watcher-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}${os === "linux" ? "-glibc" : ""}` @@ -49,7 +51,11 @@ for (const [os, arch] of targets) { await $`npm pack ${watcher}`.cwd(path.join(dir, "../../node_modules")).quiet() await $`tar -xf ../../node_modules/${watcher.replace("@parcel/", "parcel-")}-*.tgz -C ../../node_modules/${watcher} --strip-components=1` - const parserWorker = fs.realpathSync(path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js")) + const parserWorker = fs.realpathSync( + path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"), + ) + const workerPath = "./src/cli/cmd/tui/worker.ts" + await Bun.build({ conditions: ["browser"], tsconfig: "./tsconfig.json", @@ -61,10 +67,11 @@ for (const [os, arch] of targets) { execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`], windows: {}, }, - entrypoints: ["./src/index.ts", parserWorker, "./src/cli/cmd/tui/worker.ts"], + entrypoints: ["./src/index.ts", parserWorker, workerPath], define: { OPENCODE_VERSION: `'${Script.version}'`, OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker), + OPENCODE_WORKER_PATH: workerPath, OPENCODE_CHANNEL: `'${Script.channel}'`, }, }) diff --git a/packages/opencode/src/cli/cmd/tui/thread.ts b/packages/opencode/src/cli/cmd/tui/thread.ts index 0ceb87ea..b05bb582 100644 --- a/packages/opencode/src/cli/cmd/tui/thread.ts +++ b/packages/opencode/src/cli/cmd/tui/thread.ts @@ -8,6 +8,10 @@ import { bootstrap } from "@/cli/bootstrap" import path from "path" import { UI } from "@/cli/ui" +declare global { + const OPENCODE_WORKER_PATH: string +} + export const TuiThreadCommand = cmd({ command: "$0 [project]", describe: "start opencode tui", @@ -58,13 +62,21 @@ export const TuiThreadCommand = cmd({ return piped ? piped + "\n" + args.prompt : args.prompt })() - const cwd = args.project ? path.resolve(args.project) : process.cwd() + // Resolve relative paths against PWD to preserve behavior when using --cwd flag + const baseCwd = process.env.PWD ?? process.cwd() + const cwd = args.project ? path.resolve(baseCwd, args.project) : process.cwd() + let workerPath: string | URL = new URL("./worker.ts", import.meta.url) + + if (typeof OPENCODE_WORKER_PATH !== "undefined") { + workerPath = OPENCODE_WORKER_PATH + } try { process.chdir(cwd) } catch (e) { UI.error("Failed to change directory to " + cwd) return } + await bootstrap(cwd, async () => { upgrade() @@ -88,7 +100,7 @@ export const TuiThreadCommand = cmd({ return undefined })() - const worker = new Worker("./src/cli/cmd/tui/worker.ts", { + const worker = new Worker(workerPath, { env: Object.fromEntries( Object.entries(process.env).filter( (entry): entry is [string, string] => entry[1] !== undefined,