mxcl/homebrew — pull request #21184 by nachbar (2013-07-13 02:57:38+00:00)
Title: Fix error in brew doctor and brew prune that causes them to throw an exception in the face of a bad symlink

I reported this problem at https://github.com/mxcl/homebrew/issues/20712

This pull request fixes that problem.

The root of the problem is that path.resolved_path_exists? may throw an exception in brew doctor and brew prune, if a null character is included in the symlink (and perhaps under other conditions).

The stack trace is included in the issue I reported (link above). It shows that the exception is caused by doctor.rb line 191, in check_for_broken_symlinks, when it calls resolved_path_exists?

The simplest and most complete solution would seem to be to handle that by trapping the exception and considering an exception thrown by resolved_path_exists? as evidence that the path is bad and needs to be pruned.

In the absense of this fix, both brew doctor and brew prune throw an exception that results in the user seeing a stack trace (along with a request to report the bug), and prevents either routine from finishing. The user is given no clue as to where to start looking for the bad symlink.

I don't know how to run any tests in this project, but I have tested that this patch allows brew doctor to run. Once I had fixed brew doctor (so that it no longer crashed) I got this result:

Warning: Broken symlinks were found. Remove them with `brew prune`:
/usr/local/bin/pkg-config

That symlink looked like this, using ls -l :
lrwxr-xr-x 1 nachbar admin 43 Jun 23 11:39 mongotop -> ../Cellar/mongodb/2.4.4-x86_64/bin/mongotop
lrwxr-xr-x 1 nachbar admin 40 Jun 18 21:25 pkg-config ->
lrwxr-xr-x 1 nachbar admin 31 Jun 18 21:19 rbenv -> ../Cellar/rbenv/0.4.0/bin/rbenv

Running brew prune resulted in the same error, and needed the same fix.