Skip to content

Commit 1c58ac9

Browse files
authored
Merge pull request #1056 from numtide/opencode
opencode: apply packaging fixes from PR #4644
2 parents ff0e37e + 7192643 commit 1c58ac9

File tree

3 files changed

+100
-72
lines changed

3 files changed

+100
-72
lines changed

packages/opencode/bundle.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--- a/packages/opencode/src/cli/cmd/tui/thread.ts
2+
+++ b/packages/opencode/src/cli/cmd/tui/thread.ts
3+
@@ -58,16 +58,14 @@ export const TuiThreadCommand = cmd({
4+
// Resolve relative paths against PWD to preserve behavior when using --cwd flag
5+
const baseCwd = process.env.PWD ?? process.cwd()
6+
const cwd = args.project ? path.resolve(baseCwd, args.project) : process.cwd()
7+
- const defaultWorker = new URL("./worker.ts", import.meta.url)
8+
- // Nix build creates a bundled worker next to the binary; prefer it when present.
9+
+ const localWorker = new URL("./worker.ts", import.meta.url)
10+
+ const distWorker = new URL("./cli/cmd/tui/worker.js", import.meta.url)
11+
const execDir = path.dirname(process.execPath)
12+
- const bundledWorker = path.join(execDir, "opencode-worker.js")
13+
- const hasBundledWorker = await Bun.file(bundledWorker).exists()
14+
- const workerPath = (() => {
15+
+ const workerPath = await iife(async () => {
16+
if (typeof OPENCODE_WORKER_PATH !== "undefined") return OPENCODE_WORKER_PATH
17+
- if (hasBundledWorker) return bundledWorker
18+
- return defaultWorker
19+
- })()
20+
+ if (await Bun.file(distWorker).exists()) return distWorker
21+
+ return localWorker
22+
+ })
23+
try {
24+
process.chdir(cwd)
25+
} catch (e) {

packages/opencode/package.nix

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
stdenvNoCC,
44
bun,
55
fetchFromGitHub,
6+
fetchpatch,
67
fzf,
78
makeBinaryWrapper,
89
models-dev,
@@ -93,6 +94,29 @@ stdenvNoCC.mkDerivation (finalAttrs: {
9394
patches = [
9495
# NOTE: Relax Bun version check to be a warning instead of an error
9596
./relax-bun-version-check.patch
97+
98+
# NOTE: Packaging improvements from PR #4644
99+
# Add bundle.ts for bundling with bun runtime
100+
(fetchpatch {
101+
url = "https://github.com/sst/opencode/commit/0b0cccaad07a05015ce6cc9c166452e9216a98cd.patch";
102+
includes = [ "nix/bundle.ts" ];
103+
hash = "sha256-+BXH+nEbiQu3IUKpKxbvKISkndBbyXCKcGqEQCoTsDM=";
104+
})
105+
# Add patch-wasm.ts script for more robust wasm path rewriting
106+
(fetchpatch {
107+
url = "https://github.com/sst/opencode/commit/5a1af8917ee213b5c9015283c5158534e5f259d9.patch";
108+
includes = [ "nix/scripts/patch-wasm.ts" ];
109+
hash = "sha256-GW5TSJ8MpNy1d2t5vZJPjNwVFjhBquxhIk2c2ki7ijA=";
110+
})
111+
# Update canonicalize-node-modules to skip missing targets
112+
(fetchpatch {
113+
url = "https://github.com/sst/opencode/commit/d289c9cb77b0f4d17be029a8802faae9df246f8e.patch";
114+
includes = [ "nix/scripts/canonicalize-node-modules.ts" ];
115+
hash = "sha256-yx+viE+BvGI+sDITRUqRHCRszY8aYyNBsGR1ZoP431k=";
116+
})
117+
118+
# NOTE: Update thread.ts to use new bundled worker paths from PR #4644
119+
./fix-thread-worker-path.patch
96120
];
97121

98122
dontConfigure = true;
@@ -108,55 +132,66 @@ stdenvNoCC.mkDerivation (finalAttrs: {
108132
cp -r ${finalAttrs.node_modules}/node_modules .
109133
cp -r ${finalAttrs.node_modules}/packages .
110134
111-
cd packages/opencode
135+
(
136+
cd packages/opencode
112137
113-
# Fix symlinks to workspace packages
114-
chmod -R u+w ./node_modules
115-
mkdir -p ./node_modules/@opencode-ai
116-
rm -f ./node_modules/@opencode-ai/{script,sdk,plugin}
117-
ln -s $(pwd)/../../packages/script ./node_modules/@opencode-ai/script
118-
ln -s $(pwd)/../../packages/sdk/js ./node_modules/@opencode-ai/sdk
119-
ln -s $(pwd)/../../packages/plugin ./node_modules/@opencode-ai/plugin
138+
# Fix symlinks to workspace packages
139+
chmod -R u+w ./node_modules
140+
mkdir -p ./node_modules/@opencode-ai
141+
rm -f ./node_modules/@opencode-ai/{script,sdk,plugin}
142+
ln -s $(pwd)/../../packages/script ./node_modules/@opencode-ai/script
143+
ln -s $(pwd)/../../packages/sdk/js ./node_modules/@opencode-ai/sdk
144+
ln -s $(pwd)/../../packages/plugin ./node_modules/@opencode-ai/plugin
120145
121-
# Bundle the application with version defines
122-
cp ${./bundle.ts} ./bundle.ts
123-
chmod +x ./bundle.ts
124-
bun run ./bundle.ts
146+
# Use upstream bundle.ts from the patched source
147+
cp ../../nix/bundle.ts ./bundle.ts
148+
chmod +x ./bundle.ts
149+
bun run ./bundle.ts
150+
)
125151
126152
runHook postBuild
127153
'';
128154

129155
installPhase = ''
130156
runHook preInstall
131157
158+
cd packages/opencode
159+
if [ ! -d dist ]; then
160+
echo "ERROR: dist directory missing after bundle step"
161+
exit 1
162+
fi
163+
132164
mkdir -p $out/lib/opencode
133-
# Copy the bundled dist directory
134165
cp -r dist $out/lib/opencode/
135-
136-
# Fix WASM paths in worker.ts - use absolute paths to the installed location
137-
# Main wasm is tree-sitter-<hash>.wasm, language wasms are tree-sitter-<lang>-<hash>.wasm
138-
main_wasm=$(find "$out/lib/opencode/dist" -maxdepth 1 -name 'tree-sitter-[a-z0-9]*.wasm' -print -quit)
139-
140-
substituteInPlace $out/lib/opencode/dist/worker.ts \
141-
--replace-fail 'module2.exports = "../../../tree-sitter-' 'module2.exports = "'"$out"'/lib/opencode/dist/tree-sitter-' \
142-
--replace-fail 'new URL("tree-sitter.wasm", import.meta.url).href' "\"$main_wasm\""
143-
144-
# Copy only the native modules we need (marked as external in bundle.ts)
145-
mkdir -p $out/lib/opencode/node_modules/.bun
146-
mkdir -p $out/lib/opencode/node_modules/@opentui
147-
148-
# Copy @opentui/core platform-specific packages
149-
for pkg in ../../node_modules/.bun/@opentui+core-*; do
150-
if [ -d "$pkg" ]; then
151-
cp -r "$pkg" $out/lib/opencode/node_modules/.bun/$(basename "$pkg")
166+
chmod -R u+w $out/lib/opencode/dist
167+
168+
# Select bundled worker assets deterministically (sorted find output)
169+
worker_file=$(find "$out/lib/opencode/dist" -type f \( -path '*/tui/worker.*' -o -name 'worker.*' \) | sort | head -n1)
170+
parser_worker_file=$(find "$out/lib/opencode/dist" -type f -name 'parser.worker.*' | sort | head -n1)
171+
if [ -z "$worker_file" ]; then
172+
echo "ERROR: bundled worker not found"
173+
exit 1
174+
fi
175+
176+
main_wasm=$(printf '%s\n' "$out"/lib/opencode/dist/tree-sitter-*.wasm | sort | head -n1)
177+
wasm_list=$(find "$out/lib/opencode/dist" -maxdepth 1 -name 'tree-sitter-*.wasm' -print)
178+
for patch_file in "$worker_file" "$parser_worker_file"; do
179+
[ -z "$patch_file" ] && continue
180+
[ ! -f "$patch_file" ] && continue
181+
if [ -n "$wasm_list" ] && grep -q 'tree-sitter' "$patch_file"; then
182+
# Rewrite wasm references to absolute store paths to avoid runtime resolve failures.
183+
bun --bun ../../nix/scripts/patch-wasm.ts "$patch_file" "$main_wasm" $wasm_list
152184
fi
153185
done
154186
187+
mkdir -p $out/lib/opencode/node_modules
188+
cp -r ../../node_modules/.bun $out/lib/opencode/node_modules/
189+
mkdir -p $out/lib/opencode/node_modules/@opentui
155190
156191
mkdir -p $out/bin
157192
makeWrapper ${bun}/bin/bun $out/bin/opencode \
158193
--add-flags "run" \
159-
--add-flags "$out/lib/opencode/dist/index.js" \
194+
--add-flags "$out/lib/opencode/dist/src/index.js" \
160195
--prefix PATH : ${
161196
lib.makeBinPath [
162197
fzf
@@ -170,14 +205,19 @@ stdenvNoCC.mkDerivation (finalAttrs: {
170205

171206
postInstall = ''
172207
# Add symlinks for platform-specific native modules
173-
for pkg in $out/lib/opencode/node_modules/.bun/@opentui+core-*; do
208+
pkgs=(
209+
$out/lib/opencode/node_modules/.bun/@opentui+core-*
210+
$out/lib/opencode/node_modules/.bun/@opentui+solid-*
211+
$out/lib/opencode/node_modules/.bun/@opentui+core@*
212+
$out/lib/opencode/node_modules/.bun/@opentui+solid@*
213+
)
214+
for pkg in "''${pkgs[@]}"; do
174215
if [ -d "$pkg" ]; then
175-
pkgName=$(basename "$pkg" | sed 's/@opentui+\(core-[^@]*\)@.*/\1/')
216+
pkgName=$(basename "$pkg" | sed 's/@opentui+\([^@]*\)@.*/\1/')
176217
ln -sf ../.bun/$(basename "$pkg")/node_modules/@opentui/$pkgName \
177-
$out/lib/opencode/node_modules/@opentui/$pkgName
218+
$out/lib/opencode/node_modules/@opentui/$pkgName
178219
fi
179220
done
180-
181221
'';
182222

183223
meta = {

0 commit comments

Comments
 (0)