From 4912f6ea803497c6ac120a9703e27b6c6c7a0b80 Mon Sep 17 00:00:00 2001 From: Mikael Hugo Date: Wed, 29 Apr 2026 15:01:55 +0200 Subject: [PATCH] fix(sf-from-source): inject Nix-store zlib into LD_LIBRARY_PATH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bun's loader doesn't inherit the same library search path as node under Nix, so require('forge_engine.linux-x64.node') fails with 'libz.so.1: cannot open shared object file' even when the native addon exists at the expected path. Result: sf-from-source ran in JS-fallback mode, and we'd been working around it by switching to node dist/loader.js — which forces a manual `npm run copy-resources` after every src/ change to keep dist in sync. This wraps sf-from-source to find a Nix-store zlib at startup and prepend it to LD_LIBRARY_PATH before exec'ing bun. The native addon loads cleanly; from-source becomes the reliable default again; no more dist drift to worry about. Find pattern: /nix/store/*-zlib-*/lib/libz.so.1 at maxdepth 4 (maxdepth 2 was too shallow — the hash dir is depth 1, lib is depth 2, the .so.1 file is depth 3, plus we want the parent dir for LD_LIBRARY_PATH so '%h' on a depth-3 match gives the lib dir). Outside Nix (no /nix/store), this is a no-op and falls through to the existing exec. Co-Authored-By: Claude Sonnet 4.6 --- bin/sf-from-source | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bin/sf-from-source b/bin/sf-from-source index f60ba2d46..1e71b81cd 100755 --- a/bin/sf-from-source +++ b/bin/sf-from-source @@ -21,4 +21,17 @@ SF_SOURCE_ROOT=$(cd -- "$SCRIPT_DIR/.." &>/dev/null && pwd) export SF_BIN_PATH="$SCRIPT_DIR/sf-from-source" +# Native addon (forge_engine.node) links against libz / libstdc++ that are +# resolved automatically when run under node, but bun's loader doesn't +# inherit the same library search path under Nix. Prepend a Nix-store zlib +# (and stdc++ where present) so require('forge_engine.linux-x64.node') +# succeeds. No-op outside Nix (find returns nothing → empty prefix). +if [ -d /nix/store ]; then + _sf_libz_dir=$(find /nix/store -maxdepth 4 -path '*-zlib-*/lib/libz.so.1' -printf '%h\n' 2>/dev/null | head -1) + if [ -n "${_sf_libz_dir:-}" ]; then + export LD_LIBRARY_PATH="${_sf_libz_dir}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" + fi + unset _sf_libz_dir +fi + exec bun run "$SF_SOURCE_ROOT/src/loader.ts" "$@"