Cache YARN properly?

I’m following all of the instructions from https://medium.com/@ekryski/using-yarn-with-circleci-and-heroku-31d0d247bd59#.uconuwl70.

I still see yarn install every time (first non-cached issue), and then I still see all the node modules being downloaded every time by yearn (second non-cached issue).

I expected:

  • Yarn would be cached due to my cache-directories setting.
  • Node modules would not be downloaded since yarn (locally) never downloads files twice.

Please advise, thanks.

3 Likes

There is an existing feature request to have Yarn preinstalled, please :heart: that post as that is how the product team gauges interest and prioritizes requests.

Regarding the issue of caching the Yarn packages, adding the following snippets to your circle.yml file will resolve that:

machine:
  pre:
    - mkdir ~/.yarn-cache

and

dependencies:
  cache_directories:
    - ~/.yarn-cache

Please keep in mind that this will increase your Restore Cache step, so you may want to do a couple tests to see which is faster.

To explain, we need to create the ~/.yarn-cache directory before the dependencies, as you can not tell CircleCi to cache a directory that does not exist at the time the instruction is parsed.

5 Likes

Worked like a charm, and thanks for the informative answer, you guys rock!

2 Likes

I updated the gist in the guide as well. Cheers!

1 Like

Hi,

I’ve tried following these instructions, but yarn does a full install every time I run a build. I can ssh onto the box during the build and confirm that the yarn cache is properly restored. However, running yarn install always does a fresh install the first time it is run as part of the build. If i run it a second time during the build it correctly reads from the cache. There is nothing unique about my setup afaik. Below is the relevant section of my circle.yml file.


machine:
  pre:
    - mkdir ~/.yarn-cache
  node:
    version: 5.0.0
  ruby:
    version: 2.3.1
dependencies:
  pre:
    - curl -o- -L https://raw.githubusercontent.com/yarnpkg/yarn/v0.15.1/scripts/install-latest.sh | bash
    - mkdir -p tmp/cache/assets/production
  cache_directories:
    # Global node_modules
    - "~/nvm/versions/node/v5.0.0/bin"
    - "~/nvm/versions/node/v5.0.0/lib/node_modules"
    # yarn cache
    - ~/.yarn-cache
    # Application sprockets compiled assets
    - "tmp/cache/assets/production"
  override:
    - yarn install
    - bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
    - NODE_ENV=production yarn run build

This is something that we have rolled out, please see our docs for details.

https://circleci.com/docs/1.0/yarn/

1 Like

Have others seen issues with yarn install failing due to caching issues?

This tends to happen when a package changes and often it only happens on one of our environments. recently it’s been https://circleci.com/gh/clarkbw/debugger.html, but not https://circleci.com/gh/jasonLaster/debugger.html.