Reputation: 15349
How to find unused gems in my Gemfile, so that I can cleanup references which are no longer used.
Upvotes: 45
Views: 16698
Reputation: 1092
Run your tests and then:
gem stale
Which does the following:
The stale command lists the latest access time for all the files in your installed gems.
You can use this command to discover gems and gem versions you are no longer using.
Upvotes: 19
Reputation: 2900
Use linux' file access time to see what's actually being used.
This requires:
gems installed on a partition mounted with the atime (strictatime on ubuntu 12.04) option:
sudo mount -o remount,strictatime /
complete test coverage (i.e. we'll be relying on test runs to update file access times)
Note the time and run your tests. Then from your gemdir, do:
ls --time-style long-iso -ltud1 $PWD/*/lib/** | grep "21:44" | sed s/.*gems.// | sed s/.lib.*// | sort -u
Change the 21:44 to whatever time you ran the tests at.
Upvotes: 49
Reputation: 16012
There is the bundle clean --force
command to remove gems outside the Gemfile.lock
definitions.
See bundle-clean.
Upvotes: 5
Reputation: 46489
Any gem should be considered for removal if all tests pass in its absence.
Assuming you have good test coverage - particularly high-level functional tests - you could write a script to selectively remove one gem at a time. ie run all your tests N times, where N is the number of gems in your Gemfile and each test has one missing gem. That will help weed out gems not pulling their weight.
Upvotes: 16
Reputation: 1932
You can use the gem_bench gem to analyze your Gemfile and identify which gems do not need to be required at boot time. From there it just requires a bit of analysis to determine which gems can be removed completely.
To generate a list of gems that can be removed from boot time:
gem 'gem_bench', :group => :console
to your Gemfile.bundle install
bundle exec rails console
with the following command:a = GemBench.check({verbose: true})
Upvotes: 14
Reputation: 13077
I doubt if there is an automated way to find unused gems in the Gemfile.
For someone who has built the application over time, it should be easy to manually identify gems that were discarded along the way for some reason or the other.
For a legacy application inherited from someone else, it is a much difficult task to manually identify unused gems. If there is comprehensive test coverage for the system, it would help in removing unused gems by trial and error, while ensuring that the tests pass at each change in the Gemfile.
Upvotes: 9