From a94279beedd8ef3eff5a14c901827993ecd428cd Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Mon, 2 Oct 2023 19:37:29 -0400 Subject: [PATCH 1/9] 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") From 5349617204ca69ca31b38a1f76c11e26749d09db Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Tue, 3 Oct 2023 10:15:50 -0400 Subject: [PATCH 2/9] Ufuk feedback --- lib/ruby_lsp/requests/support/dependency_detector.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index d0697f325..1ba0b8689 100644 --- a/lib/ruby_lsp/requests/support/dependency_detector.rb +++ b/lib/ruby_lsp/requests/support/dependency_detector.rb @@ -80,9 +80,7 @@ def dependencies sig { returns(T::Array[String]) } def gemspec_dependencies - Dir.glob("{,*}.gemspec").flat_map do |path| - Bundler.load_gemspec(path).dependencies.map(&:name) - end + Array(Bundler.locked_gems.sources.find { Bundler::Source::Gemspec === _1 }&.gemspec&.dependencies&.map(&:name)) end end end From 8090987175080a35b3edfcb31bf922b8c3fb5e0e Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Tue, 3 Oct 2023 10:18:38 -0400 Subject: [PATCH 3/9] Pass regexp to any --- lib/ruby_lsp/requests/support/dependency_detector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index 1ba0b8689..a63289669 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) - dependencies.grep(gem_pattern).any? + dependencies.any?(gem_pattern) end sig { returns(T::Boolean) } From 8e6bf39fc388511f294b17ed1e2d1c5d414d9131 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:25:50 -0400 Subject: [PATCH 4/9] Update lib/ruby_lsp/requests/support/dependency_detector.rb Co-authored-by: Ufuk Kayserilioglu --- lib/ruby_lsp/requests/support/dependency_detector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index a63289669..17241967a 100644 --- a/lib/ruby_lsp/requests/support/dependency_detector.rb +++ b/lib/ruby_lsp/requests/support/dependency_detector.rb @@ -80,7 +80,7 @@ def dependencies sig { returns(T::Array[String]) } def gemspec_dependencies - Array(Bundler.locked_gems.sources.find { Bundler::Source::Gemspec === _1 }&.gemspec&.dependencies&.map(&:name)) + Array(Bundler.locked_gems.sources.find { Bundler::Source::Gemspec === _1 }&.gemspec&.dependencies).map(&:name) end end end From 6250b49126c161e7e83a8b10145069e271a29524 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:38:05 -0400 Subject: [PATCH 5/9] Update lib/ruby_lsp/requests/support/dependency_detector.rb Co-authored-by: Vinicius Stock --- lib/ruby_lsp/requests/support/dependency_detector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index 17241967a..34ec970bc 100644 --- a/lib/ruby_lsp/requests/support/dependency_detector.rb +++ b/lib/ruby_lsp/requests/support/dependency_detector.rb @@ -66,7 +66,7 @@ def detect_typechecker sig { returns(T::Array[String]) } def dependencies - # NOTE: If changing this behaviour, it's likely that the extension will also need changed. + # NOTE: If changing this behaviour, it's likely that the VS Code extension will also need changed. @dependencies ||= T.let( begin Bundler.with_original_env { Bundler.default_gemfile } From 8af680deef46dff2335e1dc902ed4a8eaa9b22a3 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Tue, 3 Oct 2023 16:45:55 -0400 Subject: [PATCH 6/9] Ensure multiple gemspecs are handled --- lib/ruby_lsp/requests/support/dependency_detector.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index 34ec970bc..f8855e6a0 100644 --- a/lib/ruby_lsp/requests/support/dependency_detector.rb +++ b/lib/ruby_lsp/requests/support/dependency_detector.rb @@ -80,7 +80,9 @@ def dependencies sig { returns(T::Array[String]) } def gemspec_dependencies - Array(Bundler.locked_gems.sources.find { Bundler::Source::Gemspec === _1 }&.gemspec&.dependencies).map(&:name) + Bundler.locked_gems.sources + .select { _1.is_a?(Bundler::Source::Gemspec) } + .flat_map { _1.gemspec&.dependencies&.map(&:name) } end end end From fa9cc9694143ea65e315d74ab61952cffbd2e836 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Wed, 4 Oct 2023 09:41:56 -0400 Subject: [PATCH 7/9] Grep by class name --- lib/ruby_lsp/requests/support/dependency_detector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_lsp/requests/support/dependency_detector.rb b/lib/ruby_lsp/requests/support/dependency_detector.rb index f8855e6a0..5b0d320ed 100644 --- a/lib/ruby_lsp/requests/support/dependency_detector.rb +++ b/lib/ruby_lsp/requests/support/dependency_detector.rb @@ -81,7 +81,7 @@ def dependencies sig { returns(T::Array[String]) } def gemspec_dependencies Bundler.locked_gems.sources - .select { _1.is_a?(Bundler::Source::Gemspec) } + .grep(Bundler::Source::Gemspec) .flat_map { _1.gemspec&.dependencies&.map(&:name) } end end From 90904da71fad3148e03c03a8e5f1988e6a5dc8fb Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Wed, 4 Oct 2023 09:56:24 -0400 Subject: [PATCH 8/9] Prevent state leak --- test/requests/support/dependency_detector_test.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/requests/support/dependency_detector_test.rb b/test/requests/support/dependency_detector_test.rb index 4823a47d1..f8d7929f9 100644 --- a/test/requests/support/dependency_detector_test.rb +++ b/test/requests/support/dependency_detector_test.rb @@ -5,6 +5,10 @@ module RubyLsp class DependencyDetectorTest < Minitest::Test + def setup + Singleton.__init__(RubyLsp::DependencyDetector) + end + def test_detects_no_test_library_when_there_are_no_dependencies stub_dependencies({}) @@ -48,7 +52,6 @@ def test_direct_dependency_returns_false_outside_of_bundle private def stub_dependencies(dependencies) - Singleton.__init__(RubyLsp::DependencyDetector) Bundler.locked_gems.stubs(dependencies: dependencies) end end From 216877ec4f1705d4aabd827c345b2852ae200208 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Wed, 4 Oct 2023 10:02:57 -0400 Subject: [PATCH 9/9] Use teardown instead --- test/requests/support/dependency_detector_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/requests/support/dependency_detector_test.rb b/test/requests/support/dependency_detector_test.rb index f8d7929f9..13568a5b8 100644 --- a/test/requests/support/dependency_detector_test.rb +++ b/test/requests/support/dependency_detector_test.rb @@ -5,7 +5,7 @@ module RubyLsp class DependencyDetectorTest < Minitest::Test - def setup + def teardown Singleton.__init__(RubyLsp::DependencyDetector) end @@ -52,6 +52,7 @@ def test_direct_dependency_returns_false_outside_of_bundle private def stub_dependencies(dependencies) + Singleton.__init__(RubyLsp::DependencyDetector) Bundler.locked_gems.stubs(dependencies: dependencies) end end