fix(tui): worker path resolution in dev mode (#3778)

Signed-off-by: Christian Stewart <christian@cjs.zip>
Co-authored-by: Sebastian Herrlinger <hasta84@gmail.com>
This commit is contained in:
Christian Stewart
2025-11-04 08:38:11 -08:00
committed by GitHub
parent 6f0028644e
commit 09bb819064
2 changed files with 24 additions and 5 deletions

View File

@@ -41,7 +41,9 @@ for (const [os, arch] of targets) {
const opentui = `@opentui/core-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}` const opentui = `@opentui/core-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}`
await $`mkdir -p ../../node_modules/${opentui}` 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` 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" : ""}` 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 $`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` 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({ await Bun.build({
conditions: ["browser"], conditions: ["browser"],
tsconfig: "./tsconfig.json", tsconfig: "./tsconfig.json",
@@ -61,10 +67,11 @@ for (const [os, arch] of targets) {
execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`], execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`],
windows: {}, windows: {},
}, },
entrypoints: ["./src/index.ts", parserWorker, "./src/cli/cmd/tui/worker.ts"], entrypoints: ["./src/index.ts", parserWorker, workerPath],
define: { define: {
OPENCODE_VERSION: `'${Script.version}'`, OPENCODE_VERSION: `'${Script.version}'`,
OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker), OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker),
OPENCODE_WORKER_PATH: workerPath,
OPENCODE_CHANNEL: `'${Script.channel}'`, OPENCODE_CHANNEL: `'${Script.channel}'`,
}, },
}) })

View File

@@ -8,6 +8,10 @@ import { bootstrap } from "@/cli/bootstrap"
import path from "path" import path from "path"
import { UI } from "@/cli/ui" import { UI } from "@/cli/ui"
declare global {
const OPENCODE_WORKER_PATH: string
}
export const TuiThreadCommand = cmd({ export const TuiThreadCommand = cmd({
command: "$0 [project]", command: "$0 [project]",
describe: "start opencode tui", describe: "start opencode tui",
@@ -58,13 +62,21 @@ export const TuiThreadCommand = cmd({
return piped ? piped + "\n" + args.prompt : args.prompt 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 { try {
process.chdir(cwd) process.chdir(cwd)
} catch (e) { } catch (e) {
UI.error("Failed to change directory to " + cwd) UI.error("Failed to change directory to " + cwd)
return return
} }
await bootstrap(cwd, async () => { await bootstrap(cwd, async () => {
upgrade() upgrade()
@@ -88,7 +100,7 @@ export const TuiThreadCommand = cmd({
return undefined return undefined
})() })()
const worker = new Worker("./src/cli/cmd/tui/worker.ts", { const worker = new Worker(workerPath, {
env: Object.fromEntries( env: Object.fromEntries(
Object.entries(process.env).filter( Object.entries(process.env).filter(
(entry): entry is [string, string] => entry[1] !== undefined, (entry): entry is [string, string] => entry[1] !== undefined,