Skip to content

Commit 297b2ce

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

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: 13 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,19 @@ 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+
unless File.exist?(app_uri_path)
30+
raise NeedsRestartError, <<~MESSAGE
31+
The The Ruby LSP Rails extension needs to be initialized. Please restart the Rails server and the Ruby LSP
32+
to get Rails features in the editor
33+
MESSAGE
34+
end
35+
36+
base_uri = File.read(app_uri_path).chomp
2637
@uri = T.let("#{base_uri}/ruby_lsp_rails", String)
2738
end
2839

test/lib/rails_client_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ 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+
puts "*" * 50
28+
puts project_root
29+
puts ENV["BUNDLE_GEMFILE"]
30+
puts "*" * 50
31+
File.expects(:exist?).with("#{project_root}/test/dummy/tmp/app_uri.txt").returns(false).once
32+
33+
assert_raises(RailsClient::NeedsRestartError) do
34+
RailsClient.instance
35+
end
36+
end
2437
end
2538
end
2639
end

0 commit comments

Comments
 (0)