Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include non-development gemspec dependencies in DependencyDetector check #1031

Merged
merged 10 commits into from
Oct 4, 2023
14 changes: 10 additions & 4 deletions lib/ruby_lsp/requests/support/dependency_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def detect_test_library

sig { params(gem_pattern: Regexp).returns(T::Boolean) }
def direct_dependency?(gem_pattern)
dependency_keys.grep(gem_pattern).any?
dependencies.any?(gem_pattern)
end

sig { returns(T::Boolean) }
Expand All @@ -65,16 +65,22 @@ def detect_typechecker
end

sig { returns(T::Array[String]) }
def dependency_keys
@dependency_keys ||= T.let(
def dependencies
# NOTE: If changing this behaviour, it's likely that the extension will also need changed.
andyw8 marked this conversation as resolved.
Show resolved Hide resolved
@dependencies ||= T.let(
begin
Bundler.with_original_env { Bundler.default_gemfile }
Bundler.locked_gems.dependencies.keys
Bundler.locked_gems.dependencies.keys + gemspec_dependencies
rescue Bundler::GemfileNotFound
[]
end,
T.nilable(T::Array[String]),
)
end

sig { returns(T::Array[String]) }
def gemspec_dependencies
Array(Bundler.locked_gems.sources.find { Bundler::Source::Gemspec === _1 }&.gemspec&.dependencies).map(&:name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundler supports more than one gemspec in the same project, so I think this needs to be something like

Bundler.locked_gems.sources.each_with_object([]) do |source, dependencies|
  next unless Bundler::Source::Gemspec === source

  dependencies.concat(source.gemspec&.dependencies.map(&:name))
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Updated in 8af680d

Copy link
Member

@paracycle paracycle Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for each_with_object here:

Bundler.locked_gems.sources.flat_map do |source|
  next unless Bundler::Source::Gemspec === source

  Array(source.gemspec&.dependencies).map(&:name)
end.compact

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, even better by doing the filtering first and then doing a flat map:

Bundler.locked_gems.sources.grep(Bundler::Source::Gemspec).flat_map { |source| Array(source.gemspec&.dependencies) }.map(&:name)

Copy link
Contributor Author

@andyw8 andyw8 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's almost what I had in 8af680d, but grepping by the class name is nice, I've updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly, don't think any of those options are more readable than using each_with_object - in addition to them allocating more arrays. But whatever you all decide is fine.

end
end
end
4 changes: 4 additions & 0 deletions test/requests/support/dependency_detector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def test_detects_test_unit
assert_equal("test-unit", DependencyDetector.instance.detected_test_library)
end

def test_detects_dependencies_in_gemspecs
assert(DependencyDetector.instance.direct_dependency?(/^yarp$/))
end

def test_detects_rails_if_both_rails_and_minitest_are_present
stub_dependencies("minitest" => "1.2.3", "rails" => "1.2.3")

Expand Down
Loading