Skip to content

Commit a691c13

Browse files
authored
Merge pull request #40 from Shopify/vs/do_not_fail_on_first_activation
Do not fail on first activation
2 parents 0c3ea34 + e3d7936 commit a691c13

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-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 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ 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+
app_uri_path = "#{project_root}/test/dummy/tmp/app_uri.txt"
28+
FileUtils.rm(app_uri_path)
29+
30+
# If the RailsClient singleton was initialized in a different test successfully, then there would be no chance
31+
# for this assertion to pass. We need to reset the singleton instance in order to force `initialize` to be
32+
# executed again
33+
Singleton.send(:__init__, RailsClient)
34+
35+
assert_raises(RailsClient::NeedsRestartError) do
36+
RailsClient.instance
37+
end
38+
ensure
39+
File.write(T.must(app_uri_path), "http://localhost:3000")
40+
end
2441
end
2542
end
2643
end

0 commit comments

Comments
 (0)