setup-ruby icon indicating copy to clipboard operation
setup-ruby copied to clipboard

Cleanup cache after `bundle install`

Open palexvs opened this issue 1 year ago • 4 comments

Hi, I noticed that the cache size is double what all gems in my project weigh. It seems like it's due to ${BUNDLE_PATH}/ruby/*/cache/*.gem files. But I don't think we need them after bundle install is finished. Or am I missing something?

I was thinking about adding a config param like prune-bundle-cache: true that would run rm -rf ${BUNDLE_PATH}/ruby/*/cache after bundle install

Rails already does it https://github.com/rails/rails/blob/9e9f80778abb38b8d884d456594d6b6fa683c773/railties/lib/rails/generators/rails/app/templates/Dockerfile.tt#L57

RUN bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

WDYT?

palexvs avatar Sep 25 '24 05:09 palexvs

Is there no flags or env var to tell Bundler to not save those .gem files? If you don't know could you ask by filing or searching Bundler issues?

I think setting an env var to tell Bundler to not keep those files would be a good fix. Manually removing these files feels quite wrong and fragile.

I do see these cache files as well after bundle install locally, e.g. in prism:

$ ls vendor/bundle/ruby/3.3.0/cache
ast-2.4.2.gem                          nokogiri-1.16.6-x86_64-linux.gem  rbs-3.5.2.gem
benchmark-ips-2.13.0.gem               onigmo-0.1.0.gem                  rdoc-6.7.0.gem
bundler-2.3.26.gem                     parser-3.3.0.5.gem                reline-0.5.9.gem
debug-1.9.2.gem                        parser-3.3.2.0.gem                ruby-lsp-0.17.9.gem
ffi-1.16.3.gem                         parser-3.3.3.0.gem                ruby_memcheck-3.0.0.gem
ffi-1.17.0.gem                         power_assert-2.0.3.gem            ruby_parser-3.21.0.gem
ffi-1.17.0-x86_64-linux-gnu.gem        racc-1.7.3.gem                    sexp_processor-4.17.1.gem
io-console-0.7.2.gem                   racc-1.8.0.gem                    sorbet-runtime-0.5.11495.gem
irb-1.14.0.gem                         rake-13.1.0.gem                   stringio-3.1.1.gem
language_server-protocol-3.17.0.3.gem  rake-13.2.1.gem                   test-unit-3.6.1.gem
mini_portile2-2.8.7.gem                rake-compiler-1.2.6.gem           test-unit-3.6.2.gem
nokogiri-1.16.5-x86_64-linux.gem       rake-compiler-1.2.7.gem
nokogiri-1.16.6.gem                    rbs-3.4.3.gem

"${BUNDLE_PATH}"/ruby/*/cache is incomplete BTW, it only removes it for CRuby but not JRuby/TruffleRuby, i.e.:

$ ls vendor/bundle 
jruby  ruby  truffleruby

eregon avatar Sep 25 '24 09:09 eregon

@eregon here is a ticket for Bunder https://github.com/rubygems/rubygems/issues/7163. It was created a year ago with no progress

How about the after-install-run block (equal to the widely used run) where I could put my custom cleanup?

It would help me with another edge case: reducing a gem size (wkhtmltopdf_binary_gem) by removing binary files for unused platforms. Due to this gem + bundler cache, the whole ruby install cache weight 1Gb instead of 100Mb

palexvs avatar Sep 25 '24 19:09 palexvs

What do you mean by after-install-run?

eregon avatar Sep 25 '24 19:09 eregon

here an example:

      - name: Set up Ruby
        if: needs.should-execute-tests.outputs.status == 'true'
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
        after-install-run: |
          rm -rf ....../ruby/*/cache
          rm ....../ruby/*/gems/wkhtmltopdf-binary-*/bin/*debian*

or maybe name it post-bundle-install-run

palexvs avatar Sep 25 '24 19:09 palexvs