Skip to content

Allow disabling the Sorbet detection via config #3468

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion jekyll/editors.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ configuration languages (JSON, Lua, ELisp, etc.).
"inlayHint": {
"implicitHashValue": true,
"implicitRescue": true
}
},
"typeCheckerIntegration": "defer"
},
"indexing": {
"excludedPatterns": ["path/to/excluded/file.rb"],
Expand Down
20 changes: 17 additions & 3 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def initialize
@linters = [] #: Array[String]
@test_library = "minitest" #: String
@has_type_checker = true #: bool
@type_checker_integration = ENV["RUBY_LSP_BYPASS_TYPECHECKER"] ? :bypass : :defer #: Symbol
@index = RubyIndexer::Index.new #: RubyIndexer::Index
@supported_formatters = {} #: Hash[String, Requests::Support::Formatter]
@type_inferrer = TypeInferrer.new(@index) #: TypeInferrer
Expand Down Expand Up @@ -144,6 +145,21 @@ def apply_options(options)
@test_library = detect_test_library(direct_dependencies)
notifications << Notification.window_log_message("Detected test library: #{@test_library}")

case (integration_setting = options.dig(:initializationOptions, :featuresConfiguration, :typeCheckerIntegration))
when "defer"
@type_checker_integration = :defer
when "bypass"
@type_checker_integration = :bypass
when nil
# no op
else
notifications << Notification.window_log_message(
"Invalid typeCheckerIntegration value '#{integration_setting}', must be " \
"'defer' or 'bypass'. Using default: #{integration_setting}",
type: Constant::MessageType::WARNING,
)
end

@has_type_checker = detect_typechecker(all_dependencies)
if @has_type_checker
notifications << Notification.window_log_message(
Expand Down Expand Up @@ -256,9 +272,7 @@ def detect_test_library(dependencies)

#: (Array[String] dependencies) -> bool
def detect_typechecker(dependencies)
return false if ENV["RUBY_LSP_BYPASS_TYPECHECKER"]

dependencies.any?(/^sorbet-static/)
@type_checker_integration == :defer && dependencies.any?(/^sorbet-static/)
rescue Bundler::GemfileNotFound
false
end
Expand Down
62 changes: 62 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,68 @@ def test_type_checker_is_detected_based_on_transitive_sorbet_static
assert_predicate(state, :has_type_checker)
end

def test_type_checker_integration_defaults_to_defer
state = GlobalState.new

Bundler.locked_gems.stubs(dependencies: {})
stub_all_dependencies("sorbet-static")
state.apply_options(
{
initializationOptions: { featuresConfiguration: { typeCheckerIntegration: "defer" } },
},
)

assert_predicate(state, :has_type_checker)
end

def test_type_checker_integration_can_be_set_to_bypass
state = GlobalState.new

Bundler.locked_gems.stubs(dependencies: {})
stub_all_dependencies("sorbet-static")
state.apply_options(
{
initializationOptions: { featuresConfiguration: { typeCheckerIntegration: "bypass" } },
},
)

refute_predicate(state, :has_type_checker)
end

def test_type_checker_integration_handles_misconfiguration_gracefully
state = GlobalState.new

Bundler.locked_gems.stubs(dependencies: {})
stub_all_dependencies("sorbet-static")
notifications = state.apply_options(
{
initializationOptions: { featuresConfiguration: { typeCheckerIntegration: "invalid" } },
},
)

log = notifications.find do |n|
params = n.params #: as untyped
n.method == "window/logMessage" && params.message.include?("typeCheckerIntegration")
end

refute_nil(log)
assert_predicate(state, :has_type_checker)
end

def test_environment_variable_sets_type_checker_integration
original_env_var = ENV["RUBY_LSP_BYPASS_TYPECHECKER"]
ENV["RUBY_LSP_BYPASS_TYPECHECKER"] = "1"
state = GlobalState.new

Bundler.locked_gems.stubs(dependencies: {})
stub_all_dependencies("sorbet-static")
state.apply_options({ initializationOptions: {} })

refute_predicate(state, :has_type_checker)
ensure
ENV["RUBY_LSP_BYPASS_TYPECHECKER"] = original_env_var
end

def test_addon_settings_are_stored
global_state = GlobalState.new

Expand Down