Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unused gems in my Gemfile

How to find unused gems in my Gemfile, so that I can cleanup references which are no longer used.

like image 539
Joe Avatar asked Jan 15 '13 08:01

Joe


People also ask

Should you check in Gemfile lock?

Pretty much everyone agrees you should check that into git. Including your Gemfile. lock in version control is standard practice if you are writing an application.

How do I remove gem from Gemfile lock?

You can run just bundle or bundle install to install gems based on your Gemfile. That will remove the instance of mygem from your Gemfile. lock file.

How do you install gems you added to your Gemfile?

run the command bundle install in your shell, once you have your Gemfile created. This command will look your Gemfile and install the relevant Gems on the indicated versions. The Gemfiles are installed because in your Gemfile you are pointing out the source where the gems can be downloaded from.

Where is Gemfile stored?

A Gemfile is a file we created which is used for describing gem dependencies for Ruby programs. The Gemfile is located in the root of the project directory.


3 Answers

Use linux' file access time to see what's actually being used.

This requires:

  1. project's gems isolated in an rvm gemset
  2. gems installed on a partition mounted with the atime (strictatime on ubuntu 12.04) option:

    sudo mount -o remount,strictatime /

  3. 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.

like image 169
fakeleft Avatar answered Sep 20 '22 06:09

fakeleft


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.

like image 40
L.R. Avatar answered Sep 24 '22 06:09

L.R.


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.

like image 23
mahemoff Avatar answered Sep 23 '22 06:09

mahemoff