[Bug] Setting cache folder in node_modules will break (yarn 3rc2)
- [ ] I'd be willing to implement a fix
Update, see comment below, this happens only if some versions differs in some monorepo packages.
YARN_CACHE_FOLDER=./node_modules/.yarn-cache && yarn install does not work when there's no initial ./node_modules/.yarn-state.yml present... (nodeLinker: node_modules)
I know it can look weird to do, but sometimes the only way to ensure caching on providers like Vercel.... till they support yarn 2+ and different cache uploads.
To Reproduce
Clone https://github.com/belgattitude/nextjs-monorepo-example/pull/48 and checkout branch 'fix-deps':
$ git clone https://github.com/belgattitude/nextjs-monorepo-example.git && \
cd nextjs-monorepo-example && \
git checkout -b fix-deps
Try this
$ YARN_CACHE_FOLDER=./node_modules/.yarn-cache yarn install

BTW YARN_CACHE_FOLDER is totally working if not set in node_modules/.subdir. ie hello/yarnache will work.
Weirdly it will work if an initial yarn install have been made and node_module is present,
- ie this will work
yarn install && YARN_CACHE_FOLDER=./node_modules/.yarn-cache yarn install - whereas this won't:
yarn install && rm -f ./node_modules/.yarn-state.yml && YARN_CACHE_FOLDER=./node_modules/.yarn-cache yarn install
Environment:
- Yarn version [3.0.0-rc2]
- OS: Ubuntu
- Node version [12.x, 14.x, ...]
- In a monorepo with nodeLinked: node_modules: https://github.com/belgattitude/nextjs-monorepo-example
Update: it actually works when all dependencies in the workspace have the same versions... (one node_modules folder)
I've ensured all deps are the same here: https://github.com/belgattitude/nextjs-monorepo-example/pull/49 and it works.
@belgattitude You can disable local cache for node_modules installs via enableGlobalCache: true
@larixer
You can disable local cache for node_modules installs via enableGlobalCache: true
The attempt here is to enable cache on vercel and probably other providers that does not support yarn 2 cache strategy (they fallback caching node_modules).
As discussed with vercel support, their recommended way is to set the cacheFolder into node_modules (same with pnpm store). ie: YARN_CACHE_FOLDER='node_modules/.yarn/cache' && yarn install. (they added yarn 2 support on their internal roadmap but I'm not sure it will come anytime soon)
Without cache the install is quite longer.
I know the solution is tricky but I was wondering if it's something yarn would consider (it actually almost works, depending if the initial node_modules is present or not)
I've updated a repro based on 3.0.0rc5 (very simple with a failing github action)
- https://github.com/belgattitude/nextjs-monorepo-example/pull/92/files
- https://github.com/belgattitude/nextjs-monorepo-example/pull/92/checks?check_run_id=2801812207
PS: see also https://github.com/vercel/vercel/discussions/4223#discussioncomment-652701
Found a workaround for vercel as they cache the build directory for nextjs by default.
In the vercel dashboard we can override the install command with YARN_CACHE_FOLDER=./.next/cache/yarn-cache yarn install and it seems to work

So I'll close this issue. Thx
@belgattitude when running with above command i always get build cache not found , wonder what might be the reason 🧐
@timakhalaya
tested on a monorepo and it works with 3 rc5 & rc6... are you on a monorepo too?

yep on the mono as well, for some strange reason it just flushes the cache sometimes 🧐 It works on some runs... on the other it's just not there.
i also get fsevents@patch:fsevents@npm%3A2.3.2#builtin<compat/fsevents>::version=2.3.2&hash=11e9ea The platform linux is incompatible with this module, link skipped. fsevents@patch:fsevents@npm%3A1.2.13#builtin<compat/fsevents>::version=1.2.13&hash=11e9ea The platform linux is incompatible with this module, link skipped.
that alone takes 20 secs just in waiting ... 🤔🧐
@belgattitude
same projects different deploys , same commands
22:52:19.043 | Installing build runtime...
-- | --
22:52:21.083 | Build runtime installed: 2.040s
22:52:23.446 | Looking up build cache...
22:52:23.607 | Build cache found. Downloading...
22:52:25.803 | Build cache downloaded [133.96 MB]: 2195.852ms
22:52:26.886 | Running "install" command: `ls -a && YARN_CACHE_FOLDER=./.next/cache/yarn-cache yarn install`...
22:52:26.892 | .
22:52:26.892 | ..
22:52:26.893 | babel.config.js
22:52:26.893 | components
22:52:26.893 | domain.config.js
22:52:26.893 | .env.secret
22:52:26.893 | hooks
22:52:26.893 | ignore-build-step.sh
22:52:26.893 | lib
22:52:26.893 | .next
22:52:26.893 | next.config.js
22:52:26.893 | package.json
22:52:26.893 | pages
22:52:26.893 | postcss.config.js
22:52:26.893 | public
22:52:26.893 | README.md
22:52:26.893 | vercel.json
and the other one the same repo and etc
22:53:12.954 | Cloning completed: 3.173s
-- | --
22:53:12.954 | Running "sh ./ignore-build-step.sh"
22:53:13.148 | Analyzing source code...
22:53:14.235 | Installing build runtime...
22:53:16.365 | Build runtime installed: 2.129s
22:53:18.737 | Looking up build cache...
22:53:18.825 | Build Cache not found
22:53:19.920 | Running "install" command: `ls -a && YARN_CACHE_FOLDER=./.next/cache/yarn-cache yarn install`...
22:53:19.926 | .
22:53:19.926 | ..
22:53:19.926 | babel.config.js
22:53:19.926 | components
22:53:19.927 | domain.config.js
22:53:19.927 | .env.secret
22:53:19.927 | hooks
22:53:19.927 | ignore-build-step.sh
22:53:19.927 | lib
22:53:19.927 | next.config.js
22:53:19.927 | package.json
22:53:19.927 | pages
22:53:19.927 | postcss.config.js
22:53:19.927 | public
22:53:19.927 | README.md
22:53:19.927 | vercel.json
@timakhalaya
It's weird, I rechecked and on my side it works...
Maybe check how I do here: https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docs/deploy/deploy-vercel.md
The monorepo example is there https://github.com/belgattitude/nextjs-monorepo-example