From a94279beedd8ef3eff5a14c901827993ecd428cd Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Mon, 2 Oct 2023 19:37:29 -0400 Subject: [PATCH] Include gemspec in dependency check --- .../requests/support/dependency_detector.rb | 16 ++++++++++++---- .../requests/support/dependency_detector_test.rb | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index 4cb070bce..d0697f325 100644 --- a/lib/ruby_lsp/requests/support/dependency_detector.rb +++ b/lib/ruby_lsp/requests/support/dependency_detector.rb @@ -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.grep(gem_pattern).any? end sig { returns(T::Boolean) } @@ -65,16 +65,24 @@ 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. + @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 + Dir.glob("{,*}.gemspec").flat_map do |path| + Bundler.load_gemspec(path).dependencies.map(&:name) + end + end end end diff --git a/test/requests/support/dependency_detector_test.rb b/test/requests/support/dependency_detector_test.rb index ad837c70d..f6f75940b 100644 --- a/test/requests/support/dependency_detector_test.rb +++ b/test/requests/support/dependency_detector_test.rb @@ -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")