Caching Yarn workspaces on CircleCI
Yarn workspaces make it easy to share packages within the same monorepo but I found that something wasn’t quite working with CircleCI’s caching — the correct cache was found and used but it didn’t seem to be enough because yarn install
continued for 30 seconds.
Yarn workspaces use a single yarn.lock
at the root of the project along with a node_modules
directory. However, even though the individual workspace node_modules
directories may look empty you’ll probably see that they contain a .bin
directory if you list hidden files — and this was the clue to fix my caching issue.
To ensure the cache is properly populated you must specify that each workspace’s node_modules
directory is included in the cache along with the main one:
- save_cache:
key: yarn-{{ checksum "yarn.lock" }}
paths:
- node_modules
- workspace-a/node_modules
- workspace-b/node_modules