Kurt Stephens

Nerd Up!

RubyGems : Gem::SourceIndex does not honor GEM_PATH ordering

Kurt on Wed, 2007-10-17 14:37.

Gem::SourceIndex does not honor GEM_PATH ordering.

See: http://rubyforge.org/tracker/index.php?func=detail&aid=14816&group_id=12...

Gem::SourceIndex#load_gems_in calls #add_spec for all gems found in #spec_dirs, in the order of Gem.path, however #add_spec
overwrites previous gem_specs in the @gems Hash:


    # Add a gem specification to the source index.
    def add_spec(gem_spec)
      @gems[gem_spec.full_name] = gem_spec
    end

Thus: if a gem xyz-1.2.3 exists in multiple gems install directories along Gem.path, the last one found in the Gem.path
is saved, not the first one found; very unexpected.

I think this would fix it:


    # Add a gem specification to the source index.
    def add_spec(gem_spec, overwrite = false)
      @gems[gem_spec.full_name] = nil if overwrite
      @gems[gem_spec.full_name] ||= gem_spec
    end

If other callers to #add_spec need to overwrite, they could call #add_spec(gem_spec, true) or some variant.

This was causing problems with gems that were corrupted further in the Gem.path (e.g.: /usr/lib/ruby by an errant superuser),
but were correctly installed earlier in Gem.path (e.g.: correct installation in $HOME/gems).

links: Kurt's blog | add new comment | 439 reads