Skip to content

Change how we warn about server not running #88

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

Merged
merged 1 commit into from
Jun 23, 2023
Merged
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
1 change: 0 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Extension < ::RubyLsp::Extension

sig { override.void }
def activate
# Must be the last statement in activate since it raises to display a notification for the user
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer relevant since we don't raise.

RubyLsp::Rails::RailsClient.instance.check_if_server_is_running!
end

Expand Down
34 changes: 15 additions & 19 deletions lib/ruby_lsp/ruby_lsp_rails/rails_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
module RubyLsp
module Rails
class RailsClient
class ServerNotRunningError < StandardError; end
class NeedsRestartError < StandardError; end
class ServerAddressUnknown < StandardError; end

extend T::Sig
include Singleton
Expand All @@ -31,21 +30,16 @@ def initialize
@root = T.let(Dir.exist?(dummy_path) ? dummy_path : project_root.to_s, String)
app_uri_path = "#{@root}/tmp/app_uri.txt"

unless File.exist?(app_uri_path)
raise NeedsRestartError, <<~MESSAGE
The Ruby LSP Rails extension needs to be initialized. Please restart the Rails server and the Ruby LSP
to get Rails features in the editor
MESSAGE
end

url = File.read(app_uri_path).chomp
if File.exist?(app_uri_path)
url = File.read(app_uri_path).chomp

scheme, rest = url.split("://")
uri, port = T.must(rest).split(":")
scheme, rest = url.split("://")
uri, port = T.must(rest).split(":")

@ssl = T.let(scheme == "https", T::Boolean)
@uri = T.let(T.must(uri), String)
@port = T.let(T.must(port).to_i, Integer)
@ssl = T.let(scheme == "https", T::Boolean)
@uri = T.let(T.must(uri), T.nilable(String))
@port = T.let(T.must(port).to_i, Integer)
end
end

sig { params(name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
Expand All @@ -54,15 +48,15 @@ def model(name)
return unless response.code == "200"

JSON.parse(response.body.chomp, symbolize_names: true)
rescue Errno::ECONNREFUSED
raise ServerNotRunningError, SERVER_NOT_RUNNING_MESSAGE
rescue Errno::ECONNREFUSED, ServerAddressUnknown
nil
end

sig { void }
def check_if_server_is_running!
request("activate", 0.2)
rescue Errno::ECONNREFUSED
raise ServerNotRunningError, SERVER_NOT_RUNNING_MESSAGE
rescue Errno::ECONNREFUSED, ServerAddressUnknown
warn(SERVER_NOT_RUNNING_MESSAGE)
rescue Net::ReadTimeout
# If the server is running, but the initial request is taking too long, we don't want to block the
# initialization of the Ruby LSP
Expand All @@ -72,6 +66,8 @@ def check_if_server_is_running!

sig { params(path: String, timeout: T.nilable(Float)).returns(Net::HTTPResponse) }
def request(path, timeout = nil)
raise ServerAddressUnknown unless @uri

http = Net::HTTP.new(@uri, @port)
http.use_ssl = @ssl
http.read_timeout = timeout if timeout
Expand Down
19 changes: 1 addition & 18 deletions test/ruby_lsp_rails/rails_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ class RailsClientTest < ActiveSupport::TestCase
assert_equal(expected_response, RailsClient.instance.model("User"))
end

test "raises during instantiation if app_uri file doesn't exist" do
Copy link
Contributor Author

@andyw8 andyw8 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer relevant since we don't do anything during instantiation.

project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
app_uri_path = "#{project_root}/test/dummy/tmp/app_uri.txt"
FileUtils.rm(app_uri_path)

# If the RailsClient singleton was initialized in a different test successfully, then there would be no chance
# for this assertion to pass. We need to reset the singleton instance in order to force `initialize` to be
# executed again
Singleton.send(:__init__, RailsClient)

assert_raises(RailsClient::NeedsRestartError) do
RailsClient.instance
end
ensure
File.write(T.must(app_uri_path), "http://localhost:3000")
end

test "instantiation finds the right directory when bundle gemfile points to .ruby-lsp" do
previous_bundle_gemfile = ENV["BUNDLE_GEMFILE"]
project_root = Pathname.new(previous_bundle_gemfile).dirname
Expand All @@ -57,7 +40,7 @@ class RailsClientTest < ActiveSupport::TestCase
test "check_if_server_is_running! raises if no server is found" do
Net::HTTP.any_instance.expects(:get).raises(Errno::ECONNREFUSED)

assert_raises(RailsClient::ServerNotRunningError) do
assert_output("", RailsClient::SERVER_NOT_RUNNING_MESSAGE + "\n") do
RailsClient.instance.check_if_server_is_running!
end
end
Expand Down
2 changes: 0 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
require "ruby_lsp/internal"
require "ruby_lsp/ruby_lsp_rails/extension"

$VERBOSE = nil unless ENV["VERBOSE"] || ENV["CI"]
Copy link
Contributor Author

@andyw8 andyw8 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to reduce noise in test outout, but it was preventing us from asserting against stderr.


module ActiveSupport
class TestCase
include SyntaxTree::DSL
Expand Down