'Allowing ruby buildpack on Heroku to use Yarn cache

I'm running a Ruby on Rails + Webpacker app on Heroku with Yarn v2 and hitting some issue with dependency caching. It seems that Yarn dependencies are reinstalled when it gets to asset:precompile stage, ignoring the cache.

The app is running with both node and ruby heroku buildpacks.

The output of node buildpack installation step:

-----> Installing binaries
       engines.node (package.json):  16.x
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 16.x...
       Downloading and installing node 16.15.0...
       Using default npm version: 8.5.5
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.18)
       Using yarn 3.2.0
       
-----> Restoring cache
       - yarn cache
       
-----> Installing dependencies
       Running 'yarn install' with yarn.lock
       ➤ YN0000: ┌ Resolution step
       ➤ YN0000: └ Completed in 0s 232ms
       ➤ YN0000: ┌ Fetch step
       ➤ YN0000: └ Completed in 0s 469ms
       ➤ YN0000: ┌ Link step
       ➤ YN0007: │ core-js@npm:3.22.5 must be built because it never has been before or the last one failed
       ➤ YN0007: │ core-js-pure@npm:3.22.4 must be built because it never has been before or the last one failed
       ➤ YN0000: └ Completed in 8s 631ms
       ➤ YN0000: Done in 9s 587ms
       
-----> Build
       
-----> Caching build
       - yarn cache
       
-----> Pruning devDependencies
       Skipping because the Yarn workspace plugin is not present. Add the plugin to your source code with 'yarn plugin import workspace-tools'.
       
-----> Build succeeded!

So node buildpack correctly restores dependencies in .yarn/cache, all good here. Pruning is skipped so should have no additional removal/reinstall of packages.

However when it gets to Ruby one:

-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       RAILS_ENV=staging environment is not defined in config/webpacker.yml, falling back to production environment
       ➤ YN0000: ┌ Resolution step
       ➤ YN0000: └ Completed in 0s 236ms
       ➤ YN0000: ┌ Fetch step
       ➤ YN0013: │ @ampproject/remapping@npm:2.2.0 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/code-frame@npm:7.16.7 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/compat-data@npm:7.17.10 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/core@npm:7.17.10 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/generator@npm:7.17.10 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/code-frame@npm:7.16.7 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/compat-data@npm:7.17.10 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @babel/core@npm:7.17.10 can't be found in the cache and will be fetched from the remote registry
...

Doesn't seem like it gets anything from .yarn/cache and everything gets reinstalled again.

Anyone got any ideas how to get around that, sans for checking in full .yarn/cache into git for offline caching?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source