Yarn Berry Cache Configuration for Zero-Installs and Registry Consistency
0 reputation · 03 Nov 2025, 18:04 UTC
0 reputation · 03 Nov 2025, 18:04 UTC
Yarn Berry (v2+) introduces a project-local cache stored as zip archives in .yarn/cache. This architecture supports Zero-Installs, allowing the cache to be committed to version control so that every environment uses identical artifacts without requiring a network fetch during yarn install.
When utilizing Zero-Installs, the project relies on the committed cache as the source of truth. However, a conflict arises when a private registry allows package mutation without a version increment, or when the yarn.lock integrity hashes must be updated to reflect registry changes while maintaining a consistent state across a distributed team.
There is a design trade-off between committing the full cache for absolute reproducibility and relying on registry fetches to avoid bloated repository sizes. It remains unclear how to best balance this when the registry content may shift independently of the version number.
To invalidate specific committed cache entries without purging the entire project cache, you must remove the specific zip archives associated with the affected package and force a lockfile update. Because Yarn Berry uses the .yarn/cache directory as the source of truth for Zero-Installs, simply deleting a file is insufficient if the yarn.lock integrity hash remains unchanged.
The recommended workflow for targeted invalidation is:
.zip archive for the package in .yarn/cache.yarn install. Yarn will detect the missing archive and fetch the current version from the registry.yarn.lock..zip archive and the updated yarn.lock in a single pull request. This ensures all contributors move to the new artifact simultaneously.In a standard Zero-Installs CI pipeline, you should run yarn install --immutable. Under this configuration, Yarn does not reconcile a committed cache archive against a modified registry hash; instead, it treats any discrepancy as a failure.
If the committed archive exists but the yarn.lock integrity hash differs from what the registry currently reports, or if the archive is missing but the lockfile expects it, the build will fail. This is a safety feature designed to prevent "poisoned" artifacts or silent mutations from entering the build pipeline. The CI environment is intended to be a consumer of the committed state, not a mechanism for updating it.
Conflicts typically arise when a private registry allows "overwriting" a version (e.g., publishing v1.0.1 twice with different code). Since Yarn relies on the version string and the checksum, a mutation without a version bump creates a state where the yarn.lock is technically correct for the old artifact but incorrect for the new one. The only way to resolve this across a distributed team is an intentional update to the lockfile and the committed cache.
To verify your CI is correctly enforcing integrity, you can perform the following test in a clean environment:
# Attempt an immutable install with a missing or modified cache zip
yarn install --immutable
The command should exit with a non-zero code if the local cache does not perfectly match the lockfile's requirements.
Diagnostic Detail Needed: Please specify your exact Yarn Berry version (e.g., 3.6.4 or 4.x), as checksum enforcement and immutable flag behaviors have evolved across releases.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.