diff --git a/lib/ruby_lsp/global_state.rb b/lib/ruby_lsp/global_state.rb index 3b3bbac98..d6ef81140 100644 --- a/lib/ruby_lsp/global_state.rb +++ b/lib/ruby_lsp/global_state.rb @@ -69,7 +69,7 @@ def apply_options(options) @formatter = detect_formatter(direct_dependencies, all_dependencies) if @formatter == "auto" specified_linters = options.dig(:initializationOptions, :linters) - @linters = specified_linters || detect_linters(direct_dependencies) + @linters = specified_linters || detect_linters(direct_dependencies, all_dependencies) @test_library = detect_test_library(direct_dependencies) @has_type_checker = detect_typechecker(direct_dependencies) @@ -127,10 +127,14 @@ def detect_formatter(direct_dependencies, all_dependencies) # Try to detect if there are linters in the project's dependencies. For auto-detection, we always only consider a # single linter. To have multiple linters running, the user must configure them manually - sig { params(dependencies: T::Array[String]).returns(T::Array[String]) } - def detect_linters(dependencies) + sig { params(dependencies: T::Array[String], all_dependencies: T::Array[String]).returns(T::Array[String]) } + def detect_linters(dependencies, all_dependencies) linters = [] - linters << "rubocop" if dependencies.any?(/^rubocop/) + + if dependencies.any?(/^rubocop/) || (all_dependencies.include?("rubocop") && dot_rubocop_yml_present) + linters << "rubocop" + end + linters end diff --git a/test/global_state_test.rb b/test/global_state_test.rb index 9fcc57de6..15ac32fb8 100644 --- a/test/global_state_test.rb +++ b/test/global_state_test.rb @@ -176,6 +176,18 @@ def test_specifying_empty_linters assert_empty(state.instance_variable_get(:@linters)) end + def test_linter_auto_detection_with_rubocop_as_transitive_dependency + state = GlobalState.new + + stub_direct_dependencies("gem_with_config" => "1.2.3") + stub_all_dependencies("gem_with_config", "rubocop") + state.stubs(:dot_rubocop_yml_present).returns(true) + + state.apply_options({}) + + assert_includes(state.instance_variable_get(:@linters), "rubocop") + end + def test_apply_options_sets_experimental_features state = GlobalState.new refute_predicate(state, :experimental_features)