Skip to content

Commit

Permalink
Specifically handle the case where bin/rails is not found (#284)
Browse files Browse the repository at this point in the history
* Specifically handle the case where bin/rails is not found

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.

* Use $stderr instead of warn

* Fix for Ubuntu

---------

Co-authored-by: Andy Waite <[email protected]>
  • Loading branch information
st0012 and andyw8 authored Mar 8, 2024
1 parent 04fca2a commit 5d9f3c4
Show file tree
Hide file tree
Showing 2 changed files with 32 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
$stderr.puts(<<~MSG)
Ruby LSP Rails failed to locate bin/rails in the current directory: #{Dir.pwd}"
MSG
$stderr.puts("Server dependent features will not be available")
NullClient.new
end
rescue Errno::ENOENT, StandardError => e # rubocop:disable Lint/ShadowedException
$stderr.puts("Ruby LSP Rails failed to initialize server: #{e.message}\n#{e.backtrace&.join("\n")}")
$stderr.puts("Server dependent features will not be available")
Expand Down
24 changes: 23 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,32 @@ 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
# The error message is slightly different on Ubuntu, so we need to allow for that
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 5d9f3c4

Please sign in to comment.