From 1fb4b1c3027bbf32101517854c08efb4b57aba0e Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Wed, 25 Nov 2026 15:33:50 +0100 Subject: [PATCH] dev: tools/dev.sh builds and serves through the Crafter.Build Flatpak The script stopped at "crafter-build: command not found": crafter-build is a shell alias for the Flatpak, and a /bin/sh script does not inherit aliases. It now calls the Flatpak itself unless a crafter-build command is on PATH. The server it builds links the SDK's libc++ and libunwind, so on the host it does not start (libunwind.so.1 not found). It now runs inside the Flatpak, like CI's crafter-run, with the temp dir granted so it can write the order ledger; without the grant every checkout answers 500. bin/ held builds from the old and the current toolchain, and the exactly-one rule refused to pick. Every Crafter.Build update adds a variant directory, so the script now takes the newest and says which, rather than failing after each update. CI keeps its strict rule. Stopping it also stops its tail of the server log, which was left running whenever the script ended without a Ctrl-C. The help text names /shop/donation and no longer mentions the RF form. Co-Authored-By: Claude Opus 5.5 --- tools/dev.sh | 72 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/tools/dev.sh b/tools/dev.sh index e057ae0..f9a6e1a 100755 --- a/tools/dev.sh +++ b/tools/dev.sh @@ -47,45 +47,70 @@ done cleanup() { [ -n "${SRV_PID:-}" ] && kill "$SRV_PID" 2>/dev/null || true [ -n "${CADDY_PID:-}" ] && kill "$CADDY_PID" 2>/dev/null || true + [ -n "${TAIL_PID:-}" ] && kill "$TAIL_PID" 2>/dev/null || true rm -rf "$WORK" } trap cleanup EXIT INT TERM -# Exactly one match or fail loudly. A variant directory name embeds a config -# hash, so two matches means the tree holds artifacts from two different -# configurations and picking either would be a coin flip — this has caused real -# confusion (testing a stale binary and believing the result). -onedir() { - _m=$(find bin -maxdepth 1 -type d -name "$1" 2>/dev/null | sort) - _n=$(printf '%s\n' "$_m" | grep -c . || true) - if [ "$_n" -ne 1 ]; then - echo "dev: expected exactly one $1 directory under bin/, found $_n" >&2 - [ "$_n" -gt 1 ] && printf '%s\n' "$_m" >&2 - echo "dev: run 'rm -rf bin' and try again" >&2 +# crafter-build is the Crafter.Build Flatpak. An interactive shell reaches it +# through an alias, which a script does not inherit, so call the Flatpak +# itself unless a crafter-build command is on PATH (CI installs a wrapper). +CRAFTER_REF="${CRAFTER_REF:-net.catcrafts.CrafterBuild}" +crafter_build() { + if command -v crafter-build >/dev/null 2>&1; then + crafter-build "$@" + else + flatpak run --die-with-parent "$CRAFTER_REF" "$@" + fi +} + +# What it builds links the SDK's libc++ and libunwind (RUNPATH /app/lib), so the +# server runs inside the Flatpak as well, the way CI's crafter-run does: on the +# host it does not even start (libunwind.so.1 not found). $WORK is under /tmp, +# which the sandbox does not share, hence the grant. Without it the server +# cannot write the order ledger and every checkout answers 500. +in_sdk() { + _cmd=$1; shift + flatpak run --die-with-parent --filesystem="$WORK" --command="$_cmd" \ + "$CRAFTER_REF" "$@" +} + +# A product's build directory. Its name embeds a config hash, and every +# Crafter.Build update brings a new one, so locally bin/ gains a variant per +# toolchain release while nothing removes the old ones. CI starts from a clean +# bin/ and keeps its exactly-one rule; here, take the variant whose artifact was +# written last, which right after a build is the build just made. When there is +# a choice, say which one won, so a stale pick cannot go unnoticed. +variant() { + _pick=$(ls -t bin/$1/$2 2>/dev/null | head -n 1) + if [ -z "$_pick" ]; then + echo "dev: no bin/$1/$2 found; run without --no-build first" >&2 exit 1 fi - printf '%s' "$_m" + _n=$(ls -d bin/$1 | wc -l) + [ "$_n" -gt 1 ] && echo "dev: using ${_pick%/*} (newest of $_n builds)" >&2 + printf '%s' "${_pick%/*}" } if [ "$BUILD" = 1 ]; then echo "dev: building the server product..." - crafter-build --product=server >"$WORK/build-server.log" 2>&1 \ + crafter_build --product=server >"$WORK/build-server.log" 2>&1 \ || { echo "dev: server build failed:" >&2; tail -20 "$WORK/build-server.log" >&2; exit 1; } - SRV=$(onedir 'Catcrafts.Server-*') + SRV=$(variant 'Catcrafts.Server-*' catcrafts-server) # sitemap.xml and feed.xml are generated from the same route table and Post # model the pages use, and the wasm build copies them into the bundle — so # they have to exist before it runs. - "$SRV/catcrafts-server" --sitemap > sitemap.xml - "$SRV/catcrafts-server" --feed > feed.xml + in_sdk "$PWD/$SRV/catcrafts-server" --sitemap > sitemap.xml + in_sdk "$PWD/$SRV/catcrafts-server" --feed > feed.xml echo "dev: building the wasm bundle..." - crafter-build >"$WORK/build-web.log" 2>&1 \ + crafter_build >"$WORK/build-web.log" 2>&1 \ || { echo "dev: wasm build failed:" >&2; tail -20 "$WORK/build-web.log" >&2; exit 1; } fi -SRV=$(onedir 'Catcrafts.Server-*') -WEB=$(onedir 'Catcrafts.Net-*') +[ -n "${SRV:-}" ] || SRV=$(variant 'Catcrafts.Server-*' catcrafts-server) +WEB=$(variant 'Catcrafts.Net-*' catcrafts.wasm) # Makes the static shell survive being served at a deep URL. Run unconditionally, # not just after a build: --no-build may be pointing at a bundle someone produced @@ -216,7 +241,7 @@ JSON echo "dev: seeded SAMPLE financials (DEV_FINANCIALS=1) — figures are invented" fi -"$SRV/catcrafts-server" --serve "$BACKEND_PORT" \ +in_sdk "$PWD/$SRV/catcrafts-server" --serve "$BACKEND_PORT" \ --orders="$WORK/orders.jsonl" --rail="$RAIL" \ --redirect-base="http://localhost:$PORT" >"$WORK/server.log" 2>&1 & SRV_PID=$! @@ -242,7 +267,7 @@ cat < donation (fake payment rail) + /shop/donation donate any amount to reach the payment page /projects the Crafter suite /posts fediverse posts /demos demo list; /demos/raytracer loads the wasm @@ -267,12 +292,13 @@ cat <