yarnConfigHook: add yarn.lock consistency check (#387316)

This commit is contained in:
Yureka
2025-03-05 21:16:12 +01:00
committed by GitHub
parent f2bea47583
commit c1c82774ae
2 changed files with 46 additions and 0 deletions
@@ -13,6 +13,7 @@
nodejs-slim,
prefetch-yarn-deps,
fixup-yarn-lock,
diffutils,
yarn,
makeSetupHook,
cacert,
@@ -169,6 +170,11 @@ in
yarn
fixup-yarn-lock
];
substitutions = {
# Specify `diff` by abspath to ensure that the user's build
# inputs do not cause us to find the wrong binaries.
diff = "${diffutils}/bin/diff";
};
meta = {
description = "Install nodejs dependencies from an offline yarn cache produced by fetchYarnDeps";
};
@@ -10,6 +10,46 @@ yarnConfigHook() {
echo yarnConfigHook: No yarnOfflineCache or offlineCache were defined\! >&2
exit 2
fi
local -r cacheLockfile="$offlineCache/yarn.lock"
local -r srcLockfile="$PWD/yarn.lock"
echo "Validating consistency between $srcLockfile and $cacheLockfile"
if ! @diff@ "$srcLockfile" "$cacheLockfile"; then
# If the diff failed, first double-check that the file exists, so we can
# give a friendlier error msg.
if ! [ -e "$srcLockfile" ]; then
echo
echo "ERROR: Missing yarn.lock from src. Expected to find it at: $srcLockfile"
echo "Hint: You can copy a vendored yarn.lock file via postPatch."
echo
exit 1
fi
if ! [ -e "$cacheLockfile" ]; then
echo
echo "ERROR: Missing lockfile from cache. Expected to find it at: $cacheLockfile"
echo
exit 1
fi
echo
echo "ERROR: fetchYarnDeps hash is out of date"
echo
echo "The yarn.lock in src is not the same as the in $offlineCache."
echo
echo "To fix the issue:"
echo '1. Use `lib.fakeHash` as the fetchYarnDeps hash value'
echo "2. Build the derivation and wait for it to fail with a hash mismatch"
echo "3. Copy the 'got: sha256-' value back into the fetchYarnDeps hash field"
echo
exit 1
fi
yarn config --offline set yarn-offline-mirror "$offlineCache"
fixup-yarn-lock yarn.lock
yarn install \