13

I must be missing something because last night I was astonished to find that googling for check gem dependencies and similar didn't reveal the answer for this.

I'm basically after a rough equivalent of rpm -V - a command that will go through some or all my installed gems and make sure that their dependencies are also installed. Since gem install by default installs any dependent gems, normally this is not necessary; however, if you gem uninstall a gem and tell it to proceed with the uninstall even though other gems depend on the one being uninstalled, then obviously you will end up with broken dependencies. The question is, how do you then list those broken dependencies without installing / uninstalling / updating any gems?

N.B. answers which involve Bundler are not much use to me, since I'm still stuck on Rails 2.x for various reasons.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Adam Spiers
  • 17,397
  • 5
  • 46
  • 65

4 Answers4

17

in the bash shell:

gem list --no-version > list
gem dependency --pipe > depends
grep -v -f list depends > failed.txt
rm list
rm depends

failed.txt will now have a list of all dependencies that are not installed.

philosodad
  • 1,808
  • 14
  • 24
  • Thanks, that's very useful! Although I think it still misses some unsatisfied dependencies, e.g. if gem A depends on version x of gem B, but only version y of gem B is installed, it will not reveal that gem A's dependency is unsatisfied, since it only checks that gem B is installed. – Adam Spiers Nov 13 '10 at 12:49
  • If the answer is useful, you might consider voting it up. The feature that you are looking for used to be called 'gem lock', but the `lock` command has been deprecated and the rubygem group recommends Bundler. My old version of gems still has the `lock` command, you might see if yours does as well. – philosodad Nov 13 '10 at 22:59
  • 3
    I would vote it up if StackOverflow let me :-( It requires 15 reputation, which seems ridiculous considering it's my own question. – Adam Spiers Nov 19 '10 at 16:29
5

I know you said you weren't interested in answers about Bundler, but…

Bundler will handle gem dependency resolution for you and is compatible with Rails 2.3. I've used Bundler with a number of Rails 2 apps and not had any problems with it.

There are instructions for installing Bundler on Rails 2.3 here: http://gembundler.com/rails23.html

georgebrock
  • 28,393
  • 13
  • 77
  • 72
  • 1
    Thanks - good to know. But assuming I had Bundler installed, how would I perform a dependency check on all my installed gems? Would I have to create a dummy Gemfile containing them all, and then run `bundle check` or similar? – Adam Spiers Nov 11 '10 at 11:24
  • I'm not suggestion using a dummy Gemfile, but entirely moving to Bundler. That way you not only solve your current problem, but also avoid this kind of thing happening in the future. – georgebrock Nov 12 '10 at 20:06
  • Thanks - I've had a look at Bundler and I think you're right. It's a shame that `gem` doesn't support checking for broken dependencies, but changing my workflow to Bundler means that I shouldn't have to care about broken dependencies any more, since `bundle install` will ensure that any broken dependencies which matter *in the context of a given project* are fixed. – Adam Spiers Nov 13 '10 at 12:56
3

Have you tried running gem update? That will run all the dependency tests for all of your gems. You can run this to install into a separate directory.

[edit] Also, what happens when you run gem check? gem dependency will list all gem dependencies. I'm pretty sure that if it doesn't tell you whether something is installed, you could pipe the output to a command like check to see if those gems are installed. [/edit]

philosodad
  • 1,808
  • 14
  • 24
  • Thanks but that would also have the unwanted side effect of changing gem versions, which would potentially break my Rails app. I was specifically after a read-only check of the local gems - I'll update the original question to make this clear. – Adam Spiers Nov 11 '10 at 11:26
  • true. but you can run this so that the updates are not installed in your gem folder. Also, you might try `gem outdated`. – philosodad Nov 11 '10 at 21:45
  • `gem check` doesn't output anything, but it doesn't claim to check dependencies anyway. Likewise `gem outdated` merely reports which gems have newer versions available, again ignoring any dependencies. – Adam Spiers Nov 13 '10 at 12:53
1

I definitely agree with switching to Bundler for applications. If you happen to be looking explicitly for a way to quickly identify unsatisfied gem dependencies for installed gems on a system (like I was), then you might give this script a try.

https://gist.github.com/1124953