fix(freebsd): preserve and isolate cross-target Cargo caches

This commit is contained in:
FredApps committed 2026-11-19 03:42:06 +01:00
1 parent 1db5954800
commit fa34e3106d
3 files changed
+29 -3

No files matched your search

+3
View File
@@ -275,6 +275,7 @@ jobs:
timeout-minutes: 10
uses: storytold/craft-common-actions/actions/freebsd-cargo-cache@main
with:
target: ${{ matrix.arch == 'aarch64' && 'aarch64-unknown-freebsd' || '' }}
configuration: ${{ toJSON(inputs) }}
source-hash: ${{ hashFiles('**/Cargo.lock', '**/Cargo.toml', '**/build.rs', '.cargo/**', 'rust-toolchain*', 'packaging/**', '.github/workflows/freebsd.yml') }}
# Host ENOSPC can kill the runner before it uploads the guest build log.
@@ -368,6 +369,7 @@ jobs:
uses: storytold/craft-common-actions/actions/freebsd-cargo-cache@main
with:
phase: package
target: ${{ matrix.arch == 'aarch64' && 'aarch64-unknown-freebsd' || '' }}
configuration: ${{ toJSON(inputs) }}
source-hash: ${{ hashFiles('**/Cargo.lock', '**/Cargo.toml', '**/build.rs', '.cargo/**', 'rust-toolchain*', 'packaging/**', '.github/workflows/freebsd.yml') }}
- name: Build and validate the FreeBSD package
@@ -407,6 +409,7 @@ jobs:
uses: storytold/craft-common-actions/actions/freebsd-cargo-cache@main
with:
phase: save
target: ${{ matrix.arch == 'aarch64' && 'aarch64-unknown-freebsd' || '' }}
configuration: ${{ toJSON(inputs) }}
source-hash: ${{ hashFiles('**/Cargo.lock', '**/Cargo.toml', '**/build.rs', '.cargo/**', 'rust-toolchain*', 'packaging/**', '.github/workflows/freebsd.yml') }}
key: ${{ steps.cargo-cache.outputs.key }}
+8 -1
View File
@@ -39,6 +39,7 @@ runs:
CACHE_CONFIGURATION: ${{ inputs.configuration }}
CACHE_SOURCE_HASH: ${{ inputs.source-hash }}
CACHE_PROFILE: ${{ inputs.profile }}
CACHE_TARGET: ${{ inputs.target }}
run: |
set -euo pipefail
# Read the actual guest, including native-library versions. Host Rust and
@@ -60,7 +61,13 @@ runs:
# that touches a manifest with nothing to restore, so the fallback
# prefix below deliberately omits it and lets Cargo invalidate the
# stale parts. See issues #33 and #39.
compatible=$(printf '%s\n' "$guest" "$CACHE_CONFIGURATION" "$CACHE_PROFILE" | sha256sum | cut -d ' ' -f 1)
# Keep native cache keys stable; never reuse host-only or another target's objects.
case "$CACHE_TARGET" in *[!a-z0-9_-]*) echo 'invalid cache target' >&2; exit 1 ;; esac
profile_identity=$CACHE_PROFILE
if [ -n "$CACHE_TARGET" ]; then
profile_identity="$CACHE_PROFILE|target=$CACHE_TARGET"
fi
compatible=$(printf '%s\n' "$guest" "$CACHE_CONFIGURATION" "$profile_identity" | sha256sum | cut -d ' ' -f 1)
fingerprint=$(printf '%s\n' "$compatible" "$CACHE_SOURCE_HASH" | sha256sum | cut -d ' ' -f 1)
fallback_prefix="freebsd-cargo-v3-$compatible-"
prefix="$fallback_prefix$fingerprint-"
+18 -2
View File
@@ -102,7 +102,7 @@ sys.exit(subprocess.run([os.environ['REAL_RSYNC'], *args]).returncode)
{"FIXTURE_PACKAGES": "gtk3-2\nrust-1\n"}, {"FIXTURE_FREEBSD": "15.0-RELEASE"},
{"CACHE_CONFIGURATION": '{"features":"","fonts":"abc"}'},
{"CACHE_CONFIGURATION": '{"features":"heif","fonts":"def"}'},
{"CACHE_SOURCE_HASH": "different-lock-or-build-script"}, {"CACHE_PROFILE": "release"}):
{"CACHE_SOURCE_HASH": "different-lock-or-build-script"}, {"CACHE_PROFILE": "release"}, {"CACHE_TARGET": "aarch64-unknown-freebsd"}):
with self.subTest(env=env):
self.assertNotEqual(first["prefix"], self.identity(**env)["prefix"])
updated = self.identity(GITHUB_SHA="commit2")
@@ -122,10 +122,24 @@ sys.exit(subprocess.run([os.environ['REAL_RSYNC'], *args]).returncode)
for env in ({"FIXTURE_COMPILER": "rustc 1.98.1"}, {"FIXTURE_ARCH": "aarch64"},
{"FIXTURE_PACKAGES": "gtk3-2\nrust-1\n"}, {"FIXTURE_FREEBSD": "15.0-RELEASE"},
{"CACHE_CONFIGURATION": '{"features":"heif","fonts":"def"}'},
{"CACHE_PROFILE": "release"}):
{"CACHE_PROFILE": "release"}, {"CACHE_TARGET": "aarch64-unknown-freebsd"}):
with self.subTest(env=env):
self.assertNotEqual(first["fallback-prefix"], self.identity(**env)["fallback-prefix"])
def test_cross_target_keys_cannot_restore_native_or_other_targets(self):
native = self.identity()
arm = self.identity(CACHE_TARGET="aarch64-unknown-freebsd")
other = self.identity(CACHE_TARGET="riscv64-unknown-freebsd")
self.assertEqual(len({item["fallback-prefix"] for item in (native, arm, other)}), 3)
identity_step = next(step for step in ACTION["runs"]["steps"] if step.get("id") == "identity")
self.assertEqual(identity_step["env"]["CACHE_TARGET"], "${{ inputs.target }}")
# Empty target retains the exact pre-change native compatibility bytes.
legacy = IDENTITY.replace('"$profile_identity" | sha256sum', '"$CACHE_PROFILE" | sha256sum')
result = self.run_script(legacy)
self.assertEqual(result.returncode, 0, result.stderr)
outputs = dict(line.split("=", 1) for line in (self.root / "outputs").read_text().splitlines())
self.assertEqual(outputs, native)
def test_the_key_carries_the_day_before_the_revision(self):
key = self.identity()["key"]
prefix = self.identity()["prefix"]
@@ -371,6 +385,8 @@ class WorkflowCacheIntegrationTests(unittest.TestCase):
self.assertLess(steps.index(build), steps.index(save))
self.assertNotIn("always()", save["if"])
for cache_step in cache_steps:
self.assertEqual(cache_step["with"]["target"],
"${{ matrix.arch == 'aarch64' && 'aarch64-unknown-freebsd' || '' }}")
self.assertEqual(cache_step.get("continue-on-error"), "true")
self.assertEqual(cache_step.get("timeout-minutes"), "10")
self.assertIn("steps.cargo-cache.outputs.key != ''", save["if"])