Skip to content

inherit_gem fails when gem is installed from git source #440

@jonrohan

Description

@jonrohan

Description

When using inherit_gem in .erb-lint.yml to inherit configuration from a gem that's installed from a git source (rather than from RubyGems), erb_lint fails with a Gem::MissingSpecError.

Environment

  • erb_lint version: 0.9.0
  • Ruby version: 3.4.7
  • Bundler: Using bundler standalone mode

Steps to Reproduce

  1. Install a gem from git in your Gemfile:
gem "primer_view_components", github: "primer/view_components", ref: "5a6201b882e2fa8e46f1da466c3a85f21cfcbb2f"
  1. Configure erb_lint to inherit from that gem in .erb-lint.yml:
inherit_gem:
  primer_view_components:
    - lib/primer/view_components/linters/accessibility.yml
  1. Run erb_lint:
bundle exec erb_lint app/views/some_file.html.erb

Expected Behavior

erb_lint should successfully load the inherited configuration from the git-based gem.

Actual Behavior

Unable to find gem primer_view_components; is the gem installed? 
Could not find 'primer_view_components' (>= 0) among 672 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/path/to/vendor/gems/3.4.7/ruby/3.4.0'

Root Cause
In

spec = Gem::Specification.find_by_name(gem_name)
, erb_lint uses Gem::Specification.find_by_name to locate gem paths:

def gem_config_path(gem, relative_config_path)
  gem_path = Gem::Specification.find_by_name(gem).gem_dir
  File.expand_path(relative_config_path, gem_path)
rescue Gem::LoadError
  raise Gem::LoadError, "Unable to find gem #{gem}; is the gem installed?"
end

However, Gem::Specification.find_by_name only works for gems installed from RubyGems. Git-sourced gems are installed to bundler/gems/ rather than gems/ and aren't discoverable via Gem::Specification.

Suggested Fix

Use Bundler's API to find gems, which works for both regular and git-sourced gems:

def gem_config_path(gem, relative_config_path)
  gem_path = if defined?(Bundler)
    spec = Bundler.load.specs.find { |s| s.name == gem }
    spec&.full_gem_path || Gem::Specification.find_by_name(gem).gem_dir
  else
    Gem::Specification.find_by_name(gem).gem_dir
  end
  
  File.expand_path(relative_config_path, gem_path)
rescue Gem::LoadError
  raise Gem::LoadError, "Unable to find gem #{gem}; is the gem installed?"
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions