File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
lib/ruby_lsp/ruby_lsp_rails Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,9 @@ def create_client
30
30
31
31
class InitializationError < StandardError ; end
32
32
class IncompleteMessageError < StandardError ; end
33
+ class EmptyMessageError < StandardError ; end
34
+
35
+ MAX_RETRIES = 5
33
36
34
37
extend T ::Sig
35
38
@@ -59,7 +62,16 @@ def initialize
59
62
@stdout . binmode # for Windows compatibility
60
63
61
64
$stderr. puts ( "Ruby LSP Rails booting server" )
62
- read_response
65
+ count = 0
66
+
67
+ begin
68
+ count += 1
69
+ read_response
70
+ rescue EmptyMessageError
71
+ $stderr. puts ( "Ruby LSP Rails is retrying initialize (#{ count } )" )
72
+ retry if count < MAX_RETRIES
73
+ end
74
+
63
75
$stderr. puts ( "Finished booting Ruby LSP Rails server" )
64
76
65
77
unless ENV [ "RAILS_ENV" ] == "test"
@@ -134,7 +146,10 @@ def read_response
134
146
headers = @stdout . gets ( "\r \n \r \n " )
135
147
raise IncompleteMessageError unless headers
136
148
137
- raw_response = @stdout . read ( headers [ /Content-Length: (\d +)/i , 1 ] . to_i )
149
+ content_length = headers [ /Content-Length: (\d +)/i , 1 ] . to_i
150
+ raise EmptyMessageError if content_length . zero?
151
+
152
+ raw_response = @stdout . read ( content_length )
138
153
response = JSON . parse ( T . must ( raw_response ) , symbolize_names : true )
139
154
140
155
if response [ :error ]
Original file line number Diff line number Diff line change @@ -74,6 +74,22 @@ class RunnerClientTest < ActiveSupport::TestCase
74
74
ensure
75
75
FileUtils . mv ( "bin/rails_backup" , "bin/rails" )
76
76
end
77
+
78
+ test "is resilient to extra output being printed during boot" do
79
+ content = File . read ( "test/dummy/config/application.rb" )
80
+ FileUtils . mv ( "test/dummy/config/application.rb" , "test/dummy/config/application.rb.bak" )
81
+ junk = %{\n puts "1\r \n \r \n hello"}
82
+ File . write ( "test/dummy/config/application.rb" , content + junk )
83
+
84
+ capture_subprocess_io do
85
+ client = RunnerClient . create_client
86
+
87
+ response = T . must ( client . model ( "User" ) )
88
+ assert ( response . key? ( :columns ) )
89
+ end
90
+ ensure
91
+ FileUtils . mv ( "test/dummy/config/application.rb.bak" , "test/dummy/config/application.rb" )
92
+ end
77
93
end
78
94
end
79
95
end
You can’t perform that action at this time.
0 commit comments