Skip to content

Commit 86b0665

Browse files
committed
fix(ci): prevent stale cache from overwriting source files in Node builds
Root cause: The build-and-cache-binding action caches the entire crates/kreuzberg-node/ directory, including source files. When fallback restore keys matched old caches (e.g. v1), the old source files overwrote the checked-out code. Since cache-hit was false (partial match), the build step ran but compiled the stale restored source, failing with missing ExtractionResult fields. Three fixes applied: - Move cache version to beginning of cache key so fallback prefix matching respects version boundaries - Add git checkout step after partial cache restore to ensure source files always match the current commit - Bump cache version to v4 to force fresh builds
1 parent c305a72 commit 86b0665

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

.github/actions/build-and-cache-binding/action.yml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ runs:
183183
echo "=== Cache Key Computation ==="
184184
185185
# Primary cache key: exact match required for perfect cache hit
186-
CACHE_KEY="${BINDING_NAME}-${PLATFORM}-rust-${RUST_HASH}-binding-${BINDING_HASH}-deps-${DEPS_HASH}-${CACHE_VERSION}"
186+
# Version prefix ensures fallback keys respect version boundaries
187+
CACHE_KEY="${CACHE_VERSION}-${BINDING_NAME}-${PLATFORM}-rust-${RUST_HASH}-binding-${BINDING_HASH}-deps-${DEPS_HASH}"
187188
188189
echo " Binding: $BINDING_NAME"
189190
echo " Platform: $PLATFORM"
@@ -197,17 +198,17 @@ runs:
197198
echo "cache-key=$CACHE_KEY" >> "$GITHUB_OUTPUT"
198199
199200
# Fallback restore keys (partial matches for cache misses)
200-
# Try 1: Same binding/platform/rust, different binding/deps
201-
RESTORE_KEY_1="${BINDING_NAME}-${PLATFORM}-rust-${RUST_HASH}-"
202-
# Try 2: Same binding/platform, different rust
203-
RESTORE_KEY_2="${BINDING_NAME}-${PLATFORM}-"
204-
# Try 3: Same binding, different platform
205-
RESTORE_KEY_3="${BINDING_NAME}-"
201+
# Note: GitHub Actions cache uses PREFIX matching, so version must come
202+
# early in the key to prevent stale cross-version matches. We prefix all
203+
# keys with CACHE_VERSION to ensure version bumps invalidate old caches.
204+
# Try 1: Same version/binding/platform/rust, different binding/deps hashes
205+
RESTORE_KEY_1="${CACHE_VERSION}-${BINDING_NAME}-${PLATFORM}-rust-${RUST_HASH}-"
206+
# Try 2: Same version/binding/platform, different rust/binding/deps
207+
RESTORE_KEY_2="${CACHE_VERSION}-${BINDING_NAME}-${PLATFORM}-"
206208
207209
echo "fallback-keys<<EOF" >> "$GITHUB_OUTPUT"
208210
echo "$RESTORE_KEY_1" >> "$GITHUB_OUTPUT"
209211
echo "$RESTORE_KEY_2" >> "$GITHUB_OUTPUT"
210-
echo "$RESTORE_KEY_3" >> "$GITHUB_OUTPUT"
211212
echo "EOF" >> "$GITHUB_OUTPUT"
212213
213214
- name: Restore binding from cache
@@ -273,6 +274,29 @@ runs:
273274
exit 0
274275
fi
275276
277+
- name: Restore source files after partial cache restore
278+
if: env.CACHE_HIT == 'false'
279+
shell: bash
280+
run: |
281+
set -euo pipefail
282+
283+
echo "=== Restoring source files after partial cache restore ==="
284+
# When a fallback cache key matches, old cached files overwrite the
285+
# checked-out source tree. Restore source files from git to ensure
286+
# the build compiles current code, not stale cached source.
287+
CACHE_PATHS="${{ inputs.cache-paths }}"
288+
while IFS= read -r path; do
289+
if [[ -n "$path" ]]; then
290+
# Strip trailing slash for git checkout
291+
clean_path="${path%/}"
292+
if [[ -d "$clean_path" ]]; then
293+
echo "Restoring source files in: $clean_path"
294+
git checkout HEAD -- "$clean_path" 2>/dev/null || echo " (git checkout skipped for $clean_path)"
295+
fi
296+
fi
297+
done <<< "$CACHE_PATHS"
298+
echo "✓ Source files restored to match current commit"
299+
276300
- name: Build binding if cache missed
277301
id: build
278302
if: env.CACHE_HIT == 'false'

.github/workflows/ci-node.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ jobs:
198198
task-command: node:build:ci
199199
cache-paths: |
200200
crates/kreuzberg-node/
201-
cache-version: v3
201+
cache-version: v4
202202
rust-hash: "n/a"
203203
binding-files: |
204204
crates/kreuzberg-node/**

0 commit comments

Comments
 (0)