Skip to content

Commit

Permalink
Remove Sorbet from server.rb (#339)
Browse files Browse the repository at this point in the history
* Remove Sorbet from `server.rb`

* Use Bundler.with_original_env
  • Loading branch information
andyw8 authored May 1, 2024
1 parent 402f5d6 commit cb8857a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/test/dummy/log/*.log
/test/dummy/storage/
/test/dummy/tmp/
gemfiles/*.lock
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Sorbet/StrictSigil:
Exclude:
- "**/*.rake"
- "test/**/*.rb"
- "lib/ruby_lsp/ruby_lsp_rails/server.rb" # we can't assume sorbet-runtime is available
10 changes: 4 additions & 6 deletions lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ def initialize
# If we can't set the session ID, continue
end

stdin, stdout, stderr, wait_thread = Open3.popen3(
"bin/rails",
"runner",
"#{__dir__}/server.rb",
"start",
)
stdin, stdout, stderr, wait_thread = Bundler.with_original_env do
Open3.popen3("bin/rails", "runner", "#{__dir__}/server.rb", "start")
end

@stdin = T.let(stdin, IO)
@stdout = T.let(stdout, IO)
@stderr = T.let(stderr, IO)
Expand Down
34 changes: 3 additions & 31 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
# typed: strict
# typed: false
# frozen_string_literal: true

require "sorbet-runtime"
require "json"

begin
T::Configuration.default_checked_level = :never
# Suppresses call validation errors
T::Configuration.call_validation_error_handler = ->(*) {}
# Suppresses errors caused by T.cast, T.let, T.must, etc.
T::Configuration.inline_type_error_handler = ->(*) {}
# Suppresses errors caused by incorrect parameter ordering
T::Configuration.sig_validation_error_handler = ->(*) {}
rescue
# Need this rescue so that if another gem has
# already set the checked level by the time we
# get to it, we don't fail outright.
nil
end

# NOTE: We should avoid printing to stderr since it causes problems. We never read the standard error pipe from the
# client, so it will become full and eventually hang or crash. Instead, return a response with an `error` key.

Expand All @@ -27,16 +11,12 @@ module Rails
class Server
VOID = Object.new

extend T::Sig

sig { void }
def initialize
$stdin.sync = true
$stdout.sync = true
@running = T.let(true, T::Boolean)
@running = true
end

sig { void }
def start
initialize_result = { result: { message: "ok" } }.to_json
$stdout.write("Content-Length: #{initialize_result.length}\r\n\r\n#{initialize_result}")
Expand All @@ -54,19 +34,13 @@ def start
end
end

sig do
params(
request: String,
params: T.nilable(T::Hash[Symbol, T.untyped]),
).returns(T.any(Object, T::Hash[Symbol, T.untyped]))
end
def execute(request, params)
case request
when "shutdown"
@running = false
VOID
when "model"
resolve_database_info_from_model(T.must(params).fetch(:name))
resolve_database_info_from_model(params.fetch(:name))
when "reload"
::Rails.application.reloader.reload!
VOID
Expand All @@ -79,7 +53,6 @@ def execute(request, params)

private

sig { params(model_name: String).returns(T::Hash[Symbol, T.untyped]) }
def resolve_database_info_from_model(model_name)
const = ActiveSupport::Inflector.safe_constantize(model_name)
unless active_record_model?(const)
Expand All @@ -105,7 +78,6 @@ def resolve_database_info_from_model(model_name)
{ error: e.full_message(highlight: false) }
end

sig { params(const: T.untyped).returns(T::Boolean) }
def active_record_model?(const)
!!(
const &&
Expand Down

0 comments on commit cb8857a

Please sign in to comment.