Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Yarn Berry global cache #201

Merged

Conversation

blimmer
Copy link
Contributor

@blimmer blimmer commented Dec 7, 2023

In Yarn Berry v4+, the default is to cache dependencies into a global cache (~/.yarn/berry/cache), as you can see here:

 docker run -it --entrypoint bash node:20
root@2d1b6b79999d:/# corepack enable
root@2d1b6b79999d:/# mkdir test-berry && cd test-berry
root@2d1b6b79999d:/test-berry# yarn init -y
yarn init v1.22.21
warning The yes flag has been set. This will automatically answer yes to all questions, which may have security implications.
success Saved package.json
Done in 0.01s.
root@2d1b6b79999d:/test-berry# yarn set version berry
➤ YN0000: Done in 0s 13ms
root@2d1b6b79999d:/test-berry# yarn --version
4.0.2
root@2d1b6b79999d:/test-berry# yarn add semver
➤ YN0000: · Yarn 4.0.2
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + semver@npm:7.5.4, lru-cache@npm:6.0.0, yallist@npm:4.0.0
➤ YN0000: └ Completed in 0s 593ms
➤ YN0000: ┌ Fetch step
➤ YN0013: │ 3 packages were added to the project (+ 130.97 KiB).
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 769ms
root@2d1b6b79999d:/test-berry# ls -al ~/.yarn/berry/cache
total 144
drwxr-xr-x 2 root root   4096 Dec  7 15:57 .
drwxr-xr-x 5 root root   4096 Dec  7 15:57 ..
-rw-r--r-- 1 root root  16449 Dec  7 15:57 lru-cache-npm-6.0.0-b4c8668fe1-10c0.zip
-rw-r--r-- 1 root root 101983 Dec  7 15:57 semver-npm-7.5.4-c4ad957fcd-10c0.zip
-rw-r--r-- 1 root root  15682 Dec  7 15:57 yallist-npm-4.0.0-b493d9e907-10c0.zip
root@2d1b6b79999d:/test-berry# ls .yarn/cache
ls: cannot access '.yarn/cache': No such file or directory
root@2d1b6b79999d:/test-berry#

To make this work transparently for all Yarn Berry users, we should cache both the global and project-level caches.

@blimmer blimmer requested a review from a team as a code owner December 7, 2023 15:58
@@ -176,7 +176,8 @@ steps:
- save_cache:
key: node-deps-{{ arch }}-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>><<^parameters.cache-only-lockfile>>{{ checksum "/tmp/node-project-package.json" }}-<</parameters.cache-only-lockfile>>{{ checksum "/tmp/node-project-lockfile" }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When updating versions of yarn, it writes the packageManager key to package.json. So, when updating between Yarn 3 and 4, it should trigger a cache invalidation based on this existing key.

There is a corner-case where someone updated to Yarn 4 with an older version of this orb where they wouldn't get the global cache. However, it didn't feel important enough to update this key to invalidate all Yarn Berry caches with this change.

@@ -176,7 +176,8 @@ steps:
- save_cache:
key: node-deps-{{ arch }}-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>><<^parameters.cache-only-lockfile>>{{ checksum "/tmp/node-project-package.json" }}-<</parameters.cache-only-lockfile>>{{ checksum "/tmp/node-project-lockfile" }}
paths:
- <<parameters.app-dir>>/.yarn/cache
- ~/.yarn/berry/cache # global cache, the default in Yarn Berry v4+
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the documentation on this is incorrect right now: https://yarnpkg.com/configuration/yarnrc#globalFolder. See yarnpkg/berry#6003 for a PR to update the docs to be correct.

Copy link
Contributor

@EricRibeiro EricRibeiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution and all the extra context provided in PR! It's very much appreciated 🙏

@EricRibeiro EricRibeiro merged commit 19792d1 into CircleCI-Public:master Jan 4, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants