Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/solargraph/workspace/gemspecs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def resolve_require require
return auto_required_gemspecs_from_bundler if require == 'bundler/require'

# Determine gem name based on the require path
file = "lib/#{require}.rb"
spec_with_path = Gem::Specification.find_by_path(file)
spec_with_path = Gem::Specification.find_by_path(require)

all_gemspecs = all_gemspecs_from_bundle

Expand Down Expand Up @@ -73,6 +72,7 @@ def resolve_require require

# look ourselves just in case this is hanging out somewhere
# that find_by_path doesn't index
file = "lib/#{require}.rb"
gemspec = all_gemspecs.find do |spec|
spec = to_gem_specification(spec) unless spec.respond_to?(:files)

Expand Down
25 changes: 25 additions & 0 deletions spec/workspace/gemspecs_resolve_require_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ def configure_bundler_spec stub_value
end
end

context 'with a require path that does not textually match the gem name' do
# e.g. activesupport ships as 'active_support' - neither
# require.tr('/', '-') nor require.split('/').first can guess
# 'activesupport' from 'active_support', so this can only be
# resolved via Gem::Specification.find_by_path, and only if the
# require path itself (not "lib/#{require}.rb") is passed to it
let(:require) { 'active_support' }
let(:mismatched_spec) { instance_double(Gem::Specification, name: 'activesupport', files: []) }

before do
allow(Gem::Specification).to receive(:find_by_path).and_call_original
allow(Gem::Specification).to receive(:find_by_path).with(require).and_return(mismatched_spec)
allow(gemspecs).to receive(:all_gemspecs_from_bundle).and_return([mismatched_spec])
end

it 'resolves to the right known gem' do
expect(specs.map(&:name)).to eq(['activesupport'])
end

it 'passes the require path directly to find_by_path, not prefixed with lib/' do
specs
expect(Gem::Specification).to have_received(:find_by_path).with(require)
end
end

context 'with Bundler.require' do
let(:require) { 'bundler/require' }

Expand Down
Loading