Skip to content

Commit 8707c9c

Browse files
committed
Fail gracefully if app_uri file doesn't exist
1 parent 637b911 commit 8707c9c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/ruby-lsp-rails.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# frozen_string_literal: true
33

44
require "sorbet-runtime"
5+
require "pathname"
6+
57
require "ruby_lsp_rails/version"
68
require "ruby_lsp_rails/railtie"
79

lib/ruby_lsp/ruby_lsp_rails/rails_client.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module RubyLsp
88
module Rails
99
class RailsClient
1010
class ServerNotRunningError < StandardError; end
11+
class NeedsRestartError < StandardError; end
1112

1213
extend T::Sig
1314
include Singleton
@@ -20,9 +21,23 @@ class ServerNotRunningError < StandardError; end
2021

2122
sig { void }
2223
def initialize
23-
@root = T.let(Dir.exist?("test/dummy") ? File.join(Dir.pwd, "test", "dummy") : Dir.pwd, String)
24-
base_uri = File.read("#{@root}/tmp/app_uri.txt").chomp
24+
project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
25+
dummy_path = File.join(project_root, "test", "dummy")
26+
@root = T.let(Dir.exist?(dummy_path) ? dummy_path : project_root.to_s, String)
27+
app_uri_path = "#{@root}/tmp/app_uri.txt"
2528

29+
puts "*" * 50
30+
puts app_uri_path
31+
puts File.exist?(app_uri_path)
32+
puts "*" * 50
33+
unless File.exist?(app_uri_path)
34+
raise NeedsRestartError, <<~MESSAGE
35+
The The Ruby LSP Rails extension needs to be initialized. Please restart the Rails server and the Ruby LSP
36+
to get Rails features in the editor
37+
MESSAGE
38+
end
39+
40+
base_uri = File.read(app_uri_path).chomp
2641
@uri = T.let("#{base_uri}/ruby_lsp_rails", String)
2742
end
2843

test/lib/rails_client_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class RailsClientTest < ActiveSupport::TestCase
2121
stub_http_request("200", expected_response.to_json)
2222
assert_equal(expected_response, RailsClient.instance.model("User"))
2323
end
24+
25+
test "raises during instantiation if app_uri file doesn't exist" do
26+
project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
27+
File.expects(:exist?).with("#{project_root}/test/dummy/tmp/app_uri.txt").returns(false).once
28+
29+
assert_raises(RailsClient::NeedsRestartError) do
30+
RailsClient.instance
31+
end
32+
end
2433
end
2534
end
2635
end

0 commit comments

Comments
 (0)