Ver Fonte

build: a dev container script that works from any checkout path; build.sh stops assuming one machine's layout

The build tree on this host had been made in a container that mounted the
repository at a fixed path, /data/smartbotic-database. After the checkout moved
that path existed only inside the container, so build/ pointed at a directory
the host did not have: a host `cmake --build build` failed and created the path
as an empty stray tree, and a host `cmake -B build` overwrote the cache with
host paths. Reported as confusing, which it was.

packaging/dev.sh builds and tests in the Debian 13 build image with the
checkout mounted at the SAME absolute path it has on the host, so build/ means
one thing inside and outside the container wherever the checkout lives:

  packaging/dev.sh build | test [ctest args] | e2e [name...|all] | run <cmd> | shell

It runs `build.sh --local --no-package`, so there is one list of configure
flags, not two. It runs as the invoking user, passes the commit in from the
host (a worktree's .git points outside the mount, so git cannot answer inside),
runs the e2e suites one after another in a single container because they use
fixed ports and /tmp directories, and mounts the host's grpcurl when there is
one - Debian does not package it and test_relations.sh needs it.

build.sh:

- DEB_REPO_DIR no longer defaults to /data/smartbotics-deb-repo. It is
  $DEB_REPO_DIR, else a smartbotics-deb-repo checkout next to this one, and it
  is resolved and checked BEFORE building. It used to fail only after a full
  package build.
- --local reconfigures a build tree that was configured for another location,
  or last written by another CMake version (the signature of a host run against
  a container-built tree, or the reverse), instead of dying on CMake's error.
- --repo stages only the packages the run produced. It passed the whole dist/
  directory, so every historical package was restaged on each publish and
  anything else sitting in dist/ went to the repository with it.
- --no-package builds without making .debs.

Verified: dev.sh build on this checkout detected the tree configured for
/data/smartbotic-database, reconfigured, and left no stray directory; ctest
23/23 and all 11 e2e suites pass through it. A copy of the source at an
unrelated path containing a space, with no git metadata, built and passed both
new e2e suites. A cache edited to claim another CMake version was reconfigured.
The default Docker package build was run with a throwaway revision and listed
only its own four packages.

Not changed, recorded in the roadmap: Dockerfile.build ends its build RUN with
`|| true`, which swallows a compile error until the next step.
fszontagh há 1 mês atrás
pai
commit
f35d8fd61b
4 ficheiros alterados com 397 adições e 22 exclusões
  1. 29 0
      CLAUDE.md
  2. 12 4
      docs/ROADMAP.md
  3. 132 18
      packaging/build.sh
  4. 224 0
      packaging/dev.sh

Diff do ficheiro suprimidas por serem muito extensas
+ 29 - 0
CLAUDE.md


+ 12 - 4
docs/ROADMAP.md

@@ -215,10 +215,18 @@ Two reports from smartbotic-vectorapi produced v2.11.4. Left open, with reasons:
 - **`getRelationsEnforced()` still answers `true` against a pre-2.11.0 server,**
   which enforces nothing. Left alone because it is the fail-toward-enforced read
   by contract; `tryGetRelationsEnforced()` reports no value there.
-- **The build image has no `grpcurl`,** so `tests/load_test/test_relations.sh`
-  stops at its third phase when run inside `smartbotic-db-build-base:debian13`.
-  It passes with a `grpcurl` binary mounted in. Add it to
-  `packaging/Dockerfile.base` on the next base rebuild.
+- **The build image has no `grpcurl`,** which `tests/load_test/test_relations.sh`
+  needs for its third phase. Debian does not package it, so it cannot simply be
+  added to the `apt-get install` list in `packaging/Dockerfile.base`.
+  `packaging/dev.sh` mounts the host's `grpcurl` into the container when there is
+  one; on a host without it that suite stops and says so. The proper fix is a
+  pinned, checksummed release binary in the base image.
+- **`packaging/Dockerfile.build` cannot fail at its build step.** The `RUN` ends
+  `cmake --build ... && ccache --show-stats || true`, and the `|| true` applies
+  to the whole chain, so a compile error is swallowed there and surfaces one step
+  later as "test_vector_storage: not found". Nothing wrong ships, but the error
+  is misleading. Found while reworking `build.sh`; not changed, since it is the
+  release build and wants its own verification.
 
 ### 5c. Shutdown latency after v2.11.2
 

+ 132 - 18
packaging/build.sh

@@ -16,7 +16,15 @@ PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
 
 BASE_IMAGE_NAME="smartbotic-db-build-base:debian13"
 BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
-DEB_REPO_DIR="${DEB_REPO_DIR:-/data/smartbotics-deb-repo}"
+BUILD_DIR="$PROJECT_DIR/build"
+
+# Where the smartbotics-deb-repo checkout lives. Only --repo / --sync need it.
+#
+# No absolute default: the old one (/data/smartbotics-deb-repo) was one
+# machine's layout, and when that checkout moved, a publish failed only AFTER
+# a full package build. Resolution order: $DEB_REPO_DIR, then a checkout next
+# to this one. Resolved and validated before anything is built.
+DEB_REPO_DIR="${DEB_REPO_DIR:-}"
 
 # Output directories — separate to prevent local/Docker overwriting each other
 #   dist/local/    — native build for current system
@@ -32,6 +40,7 @@ GIT_COMMIT="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || echo "
 LOCAL_MODE=0
 INSTALL_AFTER=0
 SKIP_TESTS=0
+NO_PACKAGE=0
 DEB_REVISION=1
 REBUILD_BASE=0
 NO_CACHE=0
@@ -60,6 +69,7 @@ Build modes:
 
 Options:
   --install            Install .debs locally after build (--local only)
+  --no-package         Stop after the build: no .debs (--local only)
   --skip-tests         Skip running tests
   --deb-revision N     Debian revision suffix (default: 1)
   --rebuild-base       Force rebuild Docker base image
@@ -70,7 +80,12 @@ Options:
 
 Environment variables:
   BUILD_JOBS           Parallel build jobs (default: nproc)
-  DEB_REPO_DIR         Path to smartbotics-deb-repo (default: /data/smartbotics-deb-repo)
+  DEB_REPO_DIR         Path to a smartbotics-deb-repo checkout, for --repo/--sync
+                       (default: ../smartbotics-deb-repo next to this checkout)
+
+If this host cannot build natively (missing yyjson/lmdb, a different protobuf),
+use packaging/dev.sh, which runs the same --local build inside the Debian 13
+build image.
 EOF
     exit 0
 }
@@ -83,6 +98,7 @@ while [[ $# -gt 0 ]]; do
         --local)        LOCAL_MODE=1; shift ;;
         --install)      INSTALL_AFTER=1; shift ;;
         --skip-tests)   SKIP_TESTS=1; shift ;;
+        --no-package)   NO_PACKAGE=1; shift ;;
         --deb-revision) DEB_REVISION="$2"; shift 2 ;;
         --rebuild-base) REBUILD_BASE=1; shift ;;
         --no-cache)     NO_CACHE=1; shift ;;
@@ -103,15 +119,47 @@ if [[ $INSTALL_AFTER -eq 1 && $LOCAL_MODE -eq 0 ]]; then
     exit 1
 fi
 
+if [[ $NO_PACKAGE -eq 1 && $LOCAL_MODE -eq 0 ]]; then
+    log_error "--no-package is only supported with --local"
+    exit 1
+fi
+
+if [[ $NO_PACKAGE -eq 1 && $INSTALL_AFTER -eq 1 ]]; then
+    log_error "--no-package and --install contradict each other"
+    exit 1
+fi
+
 if [[ $DO_SYNC -eq 1 && $DO_REPO -eq 0 ]]; then
     DO_REPO=1
 fi
 
+if [[ $DO_REPO -eq 1 && $NO_PACKAGE -eq 1 ]]; then
+    log_error "--repo/--sync need packages; drop --no-package"
+    exit 1
+fi
+
 if [[ $DO_REPO -eq 1 && -z "$SUITE" ]]; then
     log_error "--repo requires --suite NAME"
     exit 1
 fi
 
+# Find the apt repository tooling NOW, not after the build. A publish that
+# cannot possibly succeed should cost seconds, not a full package build.
+if [[ $DO_REPO -eq 1 ]]; then
+    if [[ -z "$DEB_REPO_DIR" ]]; then
+        DEB_REPO_DIR="$(dirname "$PROJECT_DIR")/smartbotics-deb-repo"
+    fi
+    for tool in add-packages.sh create-repo.sh sync-repo.sh; do
+        if [[ ! -x "$DEB_REPO_DIR/scripts/$tool" ]]; then
+            log_error "apt repository tooling not found: $DEB_REPO_DIR/scripts/$tool"
+            log_error "Set DEB_REPO_DIR to your smartbotics-deb-repo checkout, e.g."
+            log_error "  DEB_REPO_DIR=/path/to/smartbotics-deb-repo $0 --repo --suite $SUITE"
+            exit 1
+        fi
+    done
+    DEB_REPO_DIR="$(cd "$DEB_REPO_DIR" && pwd)"
+fi
+
 # ---------------------------------------------------------------------------
 # Set output directory based on mode
 # ---------------------------------------------------------------------------
@@ -120,7 +168,9 @@ if [[ $LOCAL_MODE -eq 1 ]]; then
 else
     OUTPUT_DIR="${DIST_BASE}/debian13"
 fi
-mkdir -p "$OUTPUT_DIR"
+if [[ $NO_PACKAGE -eq 0 ]]; then
+    mkdir -p "$OUTPUT_DIR"
+fi
 
 # ---------------------------------------------------------------------------
 # Banner
@@ -132,14 +182,63 @@ if [[ $LOCAL_MODE -eq 1 ]]; then
 else
     log_info "Mode: Docker (Debian 13)"
 fi
-log_info "Output: ${OUTPUT_DIR}"
+if [[ $NO_PACKAGE -eq 0 ]]; then
+    log_info "Output: ${OUTPUT_DIR}"
+fi
 
 # ---------------------------------------------------------------------------
 # Local build
 # ---------------------------------------------------------------------------
+# A CMake build tree belongs to the place and the toolchain that configured it.
+# It records absolute source and binary paths and refuses to be used from
+# anywhere else, and it caches every library path it found. Both go stale for
+# ordinary reasons:
+#
+#   - the checkout was moved or renamed, or the tree was built in a container
+#     that mounted the repository at a different path;
+#   - the tree was built in the build image (packaging/dev.sh) and someone then
+#     ran cmake against it on the host, or the other way round. The paths match
+#     but the cache now holds another system's libraries.
+#
+# CMake's own errors for these do not say what to do. Dropping the cache makes
+# the next configure start clean; nothing else needs removing, because every
+# compile command changes and Ninja rebuilds what is affected.
+#
+# The second case is detected by the CMake version that last wrote the cache.
+# Two environments sharing one tree almost never share a CMake version, and a
+# cache written by a different CMake is worth a clean configure regardless.
+forget_foreign_build_tree() {
+    local cache="$BUILD_DIR/CMakeCache.txt"
+    [[ -f "$cache" ]] || return 0
+
+    cached() { sed -n "s/^$1:[A-Z]*=//p" "$cache" | head -n1; }
+
+    local why=""
+    local configured_src configured_bin
+    configured_src="$(cached CMAKE_HOME_DIRECTORY)"
+    configured_bin="$(cached CMAKE_CACHEFILE_DIR)"
+    if [[ "$configured_src" != "$PROJECT_DIR" || "$configured_bin" != "$BUILD_DIR" ]]; then
+        why="it was configured for ${configured_src:-an unknown location}, not $PROJECT_DIR"
+    else
+        local wrote running
+        wrote="$(cached CMAKE_CACHE_MAJOR_VERSION).$(cached CMAKE_CACHE_MINOR_VERSION).$(cached CMAKE_CACHE_PATCH_VERSION)"
+        running="$(cmake --version | sed -n '1s/^cmake version //p')"
+        if [[ "$wrote" != "$running" ]]; then
+            why="it was last configured by CMake $wrote and this is CMake $running (a different system)"
+        fi
+    fi
+    [[ -n "$why" ]] || return 0
+
+    log_warn "build/ cannot be reused: $why."
+    log_warn "Reconfiguring from scratch; anything whose build command changed is rebuilt."
+    rm -f -- "$cache"
+}
+
 build_local() {
+    forget_foreign_build_tree
+
     log_info "Configuring CMake (Release)..."
-    cmake -B "$PROJECT_DIR/build" -G Ninja \
+    cmake -B "$BUILD_DIR" -G Ninja \
         -DCMAKE_BUILD_TYPE=Release \
         -DSMARTBOTIC_DB_BUILD_SERVICE=ON \
         -DSMARTBOTIC_DB_BUILD_CLIENT=ON \
@@ -149,18 +248,23 @@ build_local() {
         -S "$PROJECT_DIR"
 
     log_info "Building with ${BUILD_JOBS} jobs..."
-    cmake --build "$PROJECT_DIR/build" -j"$BUILD_JOBS"
+    cmake --build "$BUILD_DIR" -j"$BUILD_JOBS"
     log_success "Build completed"
 
     if [[ $SKIP_TESTS -eq 0 ]]; then
         log_info "Running tests..."
-        LD_LIBRARY_PATH="$PROJECT_DIR/build/client" \
-            "$PROJECT_DIR/build/tests/test_vector_storage"
+        LD_LIBRARY_PATH="$BUILD_DIR/client" \
+            "$BUILD_DIR/tests/test_vector_storage"
         log_success "Tests passed"
     else
         log_warn "Tests skipped (--skip-tests)"
     fi
 
+    if [[ $NO_PACKAGE -eq 1 ]]; then
+        log_info "Packaging skipped (--no-package)"
+        return 0
+    fi
+
     log_info "Creating .deb packages..."
     OUTPUT_DIR="$OUTPUT_DIR" BUILD_DEB_REVISION="$DEB_REVISION" "$PROJECT_DIR/packaging/deb/create-debs.sh"
     log_success "Packages created"
@@ -241,15 +345,30 @@ else
     build_docker
 fi
 
+if [[ $NO_PACKAGE -eq 1 ]]; then
+    echo ""
+    log_success "Build complete: $BUILD_DIR"
+    exit 0
+fi
+
 # ---------------------------------------------------------------------------
 # List created packages
 # ---------------------------------------------------------------------------
+# Only what THIS run produced. dist/ accumulates every version ever built, and
+# both the listing and the publish step used to take the whole directory - so a
+# publish re-staged every historical package, and anything else sitting in
+# dist/ went to the repository with it.
+shopt -s nullglob
+BUILT_DEBS=("$OUTPUT_DIR"/*_"${VERSION}-${DEB_REVISION}"_*.deb)
+shopt -u nullglob
+if [[ ${#BUILT_DEBS[@]} -eq 0 ]]; then
+    log_error "No ${VERSION}-${DEB_REVISION} .deb files found in $OUTPUT_DIR"
+    exit 1
+fi
+
 echo ""
 log_info "Created packages:"
-ls -lh "$OUTPUT_DIR"/*.deb 2>/dev/null || {
-    log_error "No .deb files found in $OUTPUT_DIR"
-    exit 1
-}
+ls -lh "${BUILT_DEBS[@]}"
 
 # ---------------------------------------------------------------------------
 # Repository operations
@@ -258,12 +377,7 @@ if [[ $DO_REPO -eq 1 ]]; then
     echo ""
     log_info "Adding packages to repository (suite: ${SUITE})..."
 
-    if [[ ! -x "$DEB_REPO_DIR/scripts/add-packages.sh" ]]; then
-        log_error "Repository tool not found: $DEB_REPO_DIR/scripts/add-packages.sh"
-        exit 1
-    fi
-
-    "$DEB_REPO_DIR/scripts/add-packages.sh" "$OUTPUT_DIR"
+    "$DEB_REPO_DIR/scripts/add-packages.sh" "${BUILT_DEBS[@]}"
     log_success "Packages added to staging"
 
     "$DEB_REPO_DIR/scripts/create-repo.sh" --suite "$SUITE"

+ 224 - 0
packaging/dev.sh

@@ -0,0 +1,224 @@
+#!/usr/bin/env bash
+#
+# packaging/dev.sh - build and test inside the Debian 13 build image
+#
+# For a host that cannot build this project natively: no yyjson or lmdb
+# development packages, or a protobuf/gRPC that does not match the release
+# target. It runs the same `build.sh --local` build the packages are made
+# from, in the same image, so what you test is what ships.
+#
+# The repository is mounted at the SAME absolute path it has on the host.
+# That is the whole design. A CMake build tree records the absolute paths it
+# was configured with; mounting the checkout somewhere else (a fixed /src, or
+# one machine's old checkout path) leaves build/ pointing at a directory that
+# does not exist outside the container, so every host-side tool that reads
+# the tree is confused by it, and a host `cmake --build build` goes off and
+# creates that phantom directory. With the paths equal, build/ means the same
+# thing inside and outside, wherever the checkout lives.
+#
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
+
+BASE_IMAGE_NAME="${BASE_IMAGE_NAME:-smartbotic-db-build-base:debian13}"
+DOCKER="${DOCKER:-docker}"
+BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
+
+log_info()    { printf '\033[1;34m[INFO]\033[0m  %s\n' "$*"; }
+log_success() { printf '\033[1;32m[OK]\033[0m    %s\n' "$*"; }
+log_warn()    { printf '\033[1;33m[WARN]\033[0m  %s\n' "$*"; }
+log_error()   { printf '\033[1;31m[ERROR]\033[0m %s\n' "$*" >&2; }
+
+usage() {
+    cat <<EOF
+Usage: packaging/dev.sh <command> [args]
+
+Commands:
+  build                Configure if needed and build into ./build (Release)
+  test [ctest args]    Run the unit suite, e.g. test -R relation
+  e2e                  List the client/server suites in tests/load_test
+  e2e <name>...        Run those suites, e.g. e2e test_relations test_indexes
+  e2e all              Run every tests/load_test/test_*.sh
+  run <cmd> [args]     Run one command in the container, from the repo root
+  shell                Interactive shell in the container
+
+Environment variables:
+  BUILD_JOBS           Parallel build jobs (default: nproc)
+  BASE_IMAGE_NAME      Build image (default: smartbotic-db-build-base:debian13;
+                       built from packaging/Dockerfile.base if missing)
+  DOCKER               Container CLI to use (default: docker)
+
+The container sees the repository at its real path:
+  $PROJECT_DIR
+EOF
+}
+
+ensure_image() {
+    if ! command -v "$DOCKER" &>/dev/null; then
+        log_error "$DOCKER is not installed or not in PATH"
+        exit 1
+    fi
+    if "$DOCKER" image inspect "$BASE_IMAGE_NAME" &>/dev/null; then
+        return 0
+    fi
+    log_info "Base image not found, building ${BASE_IMAGE_NAME}..."
+    "$DOCKER" buildx build \
+        -f "$PROJECT_DIR/packaging/Dockerfile.base" \
+        -t "$BASE_IMAGE_NAME" \
+        "$PROJECT_DIR"
+    log_success "Base image built: ${BASE_IMAGE_NAME}"
+}
+
+# Run "$@" in the build image, as the invoking user, from the repo root.
+in_container() {
+    local args=(run --rm)
+    if [[ -t 0 && -t 1 ]]; then
+        args+=(-it)
+    fi
+
+    # As the invoking user, so nothing in build/ ends up owned by root. That
+    # user has no home in the image; HOME=/tmp gives tools somewhere to write.
+    args+=(-u "$(id -u):$(id -g)" -e HOME=/tmp)
+
+    # Resolved on the host: inside the container git may be unable to answer
+    # (a worktree's .git is a pointer to a directory outside this mount), and
+    # CMake prefers this variable over asking git.
+    local commit
+    commit="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || true)"
+    if [[ -n "$commit" ]]; then
+        args+=(-e "BUILD_GIT_COMMIT=$commit")
+    fi
+    args+=(-e "BUILD_JOBS=$BUILD_JOBS")
+
+    args+=(-v "$PROJECT_DIR:$PROJECT_DIR" -w "$PROJECT_DIR")
+
+    # tests/load_test/test_relations.sh drives three RPCs with grpcurl, which
+    # Debian does not package, so the image has none. Borrow the host's when
+    # there is one; without it that suite stops and says so.
+    local host_grpcurl
+    host_grpcurl="$(command -v grpcurl 2>/dev/null || true)"
+    if [[ -n "$host_grpcurl" ]]; then
+        args+=(-v "$host_grpcurl:/usr/local/bin/grpcurl:ro")
+    fi
+
+    "$DOCKER" "${args[@]}" "$BASE_IMAGE_NAME" "$@"
+}
+
+require_build() {
+    if [[ ! -x "$PROJECT_DIR/build/service/smartbotic-database" ]]; then
+        log_error "No build found. Run: packaging/dev.sh build"
+        exit 1
+    fi
+}
+
+cmd_build() {
+    in_container packaging/build.sh --local --no-package --skip-tests
+}
+
+cmd_test() {
+    require_build
+    in_container bash -c 'cd build/tests && ctest --output-on-failure -j"$BUILD_JOBS" "$@"' ctest "$@"
+}
+
+cmd_e2e() {
+    local dir="$PROJECT_DIR/tests/load_test"
+    local names=()
+
+    if [[ $# -eq 0 ]]; then
+        echo "Suites in tests/load_test (run with: packaging/dev.sh e2e <name>...):"
+        local f
+        for f in "$dir"/test_*.sh; do
+            echo "  $(basename "$f" .sh)"
+        done
+        return 0
+    fi
+
+    if [[ "$1" == "all" ]]; then
+        local f
+        for f in "$dir"/test_*.sh; do
+            names+=("$(basename "$f" .sh)")
+        done
+    else
+        local n
+        for n in "$@"; do
+            n="${n%.sh}"
+            if [[ ! -f "$dir/$n.sh" ]]; then
+                log_error "No such suite: tests/load_test/$n.sh"
+                exit 1
+            fi
+            names+=("$n")
+        done
+    fi
+
+    require_build
+
+    # One container for the whole list: the suites use fixed ports and /tmp
+    # directories, which is safe exactly because they run one after another
+    # in a filesystem and network namespace nothing else shares.
+    in_container bash -c '
+        failed=0
+        for name in "$@"; do
+            log="/tmp/e2e-$name.log"
+            if bash "tests/load_test/$name.sh" > "$log" 2>&1; then
+                printf "  \033[1;32mPASS\033[0m  %s\n" "$name"
+            else
+                printf "  \033[1;31mFAIL\033[0m  %s\n" "$name"
+                tail -n 40 "$log" | sed "s/^/        /"
+                failed=$((failed + 1))
+            fi
+        done
+        if [ "$failed" -ne 0 ]; then
+            echo ""
+            echo "$failed of $# suite(s) failed"
+            exit 1
+        fi
+        echo ""
+        echo "all $# suite(s) passed"
+    ' e2e "${names[@]}"
+}
+
+if [[ $# -eq 0 ]]; then
+    usage
+    exit 1
+fi
+
+command="$1"
+shift
+
+case "$command" in
+    build)
+        ensure_image
+        cmd_build
+        ;;
+    test)
+        ensure_image
+        cmd_test "$@"
+        ;;
+    e2e)
+        if [[ $# -gt 0 ]]; then
+            ensure_image
+        fi
+        cmd_e2e "$@"
+        ;;
+    run)
+        if [[ $# -eq 0 ]]; then
+            log_error "run needs a command"
+            exit 1
+        fi
+        ensure_image
+        in_container "$@"
+        ;;
+    shell)
+        ensure_image
+        in_container bash
+        ;;
+    -h|--help|help)
+        usage
+        ;;
+    *)
+        log_error "Unknown command: $command"
+        usage
+        exit 1
+        ;;
+esac

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff