Skip to content

Commit

Permalink
Specifically handle the case where bin/rails is not found
Browse files Browse the repository at this point in the history
When the file is not found, it is likely that the LSP is not being run in
the root of a Rails project. Having a more specific error message will
help the user understand what is going on.
  • Loading branch information
st0012 committed Mar 8, 2024
1 parent efb3e77 commit 529f22e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ class << self

sig { returns(RunnerClient) }
def create_client
new
if File.exist?("bin/rails")
new
else
warn(<<~MSG)
Ruby LSP Rails failed to locate bin/rails in the current directory: #{Dir.pwd}"
MSG
warn("Server dependent features will not be available")
NullClient.new
end
rescue Errno::ENOENT, StandardError => e # rubocop:disable Lint/ShadowedException
warn("Ruby LSP Rails failed to initialize server: #{e.message}\n#{e.backtrace&.join("\n")}")
warn("Server dependent features will not be available")
Expand Down
23 changes: 22 additions & 1 deletion test/ruby_lsp_rails/runner_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,31 @@ class RunnerClientTest < ActiveSupport::TestCase
assert_nil @client.model("ApplicationRecord") # ApplicationRecord is abstract
end

test "falls back to null client when bin/rails is not found" do
FileUtils.mv("bin/rails", "bin/rails_backup")

assert_output("", %r{Ruby LSP Rails failed to locate bin/rails in the current directory}) do
client = RunnerClient.create_client

assert_instance_of(NullClient, client)
assert_nil(client.model("User"))
assert_predicate(client, :stopped?)
end
ensure
FileUtils.mv("bin/rails_backup", "bin/rails")
end

test "failing to spawn server creates a null client" do
FileUtils.mv("bin/rails", "bin/rails_backup")
File.open("bin/rails", "w") do |f|
f.write("foo")
end
File.chmod(0o755, "bin/rails")

assert_output("", %r{No such file or directory - bin/rails}) do
assert_output(
"",
%r{Ruby LSP Rails failed to initialize server: bin/rails: line 1: foo:( command)? not found},
) do
client = RunnerClient.create_client

assert_instance_of(NullClient, client)
Expand Down

0 comments on commit 529f22e

Please sign in to comment.