@@ -10,28 +10,34 @@ class RunnerClient
10
10
class << self
11
11
extend T ::Sig
12
12
13
- sig { params ( message_queue : Thread ::Queue ) . returns ( RunnerClient ) }
14
- def create_client ( message_queue )
13
+ sig { params ( outgoing_queue : Thread ::Queue ) . returns ( RunnerClient ) }
14
+ def create_client ( outgoing_queue )
15
15
if File . exist? ( "bin/rails" )
16
- new ( message_queue )
16
+ new ( outgoing_queue )
17
17
else
18
- message_queue << RubyLsp ::Notification . window_log_message (
18
+ unless outgoing_queue . closed?
19
+ outgoing_queue << RubyLsp ::Notification . window_log_message (
20
+ <<~MESSAGE . chomp ,
21
+ Ruby LSP Rails failed to locate bin/rails in the current directory: #{ Dir . pwd }
22
+ Server dependent features will not be available
23
+ MESSAGE
24
+ type : RubyLsp ::Constant ::MessageType ::WARNING ,
25
+ )
26
+ end
27
+
28
+ NullClient . new
29
+ end
30
+ rescue Errno ::ENOENT , StandardError => e # rubocop:disable Lint/ShadowedException
31
+ unless outgoing_queue . closed?
32
+ outgoing_queue << RubyLsp ::Notification . window_log_message (
19
33
<<~MESSAGE . chomp ,
20
- Ruby LSP Rails failed to locate bin/rails in the current directory : #{ Dir . pwd }
34
+ Ruby LSP Rails failed to initialize server : #{ e . full_message }
21
35
Server dependent features will not be available
22
36
MESSAGE
23
- type : RubyLsp :: Constant ::MessageType ::WARNING ,
37
+ type : Constant ::MessageType ::ERROR ,
24
38
)
25
- NullClient . new
26
39
end
27
- rescue Errno ::ENOENT , StandardError => e # rubocop:disable Lint/ShadowedException
28
- message_queue << RubyLsp ::Notification . window_log_message (
29
- <<~MESSAGE . chomp ,
30
- Ruby LSP Rails failed to initialize server: #{ e . full_message }
31
- Server dependent features will not be available
32
- MESSAGE
33
- type : Constant ::MessageType ::ERROR ,
34
- )
40
+
35
41
NullClient . new
36
42
end
37
43
end
@@ -47,9 +53,9 @@ class EmptyMessageError < StandardError; end
47
53
sig { returns ( String ) }
48
54
attr_reader :rails_root
49
55
50
- sig { params ( message_queue : Thread ::Queue ) . void }
51
- def initialize ( message_queue )
52
- @message_queue = T . let ( message_queue , Thread ::Queue )
56
+ sig { params ( outgoing_queue : Thread ::Queue ) . void }
57
+ def initialize ( outgoing_queue )
58
+ @outgoing_queue = T . let ( outgoing_queue , Thread ::Queue )
53
59
@mutex = T . let ( Mutex . new , Mutex )
54
60
# Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
55
61
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
@@ -78,19 +84,19 @@ def initialize(message_queue)
78
84
@stdout . binmode
79
85
@stderr . binmode
80
86
81
- @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails booting server" )
87
+ log_message ( "Ruby LSP Rails booting server" )
82
88
count = 0
83
89
84
90
begin
85
91
count += 1
86
92
initialize_response = T . must ( read_response )
87
93
@rails_root = T . let ( initialize_response [ :root ] , String )
88
94
rescue EmptyMessageError
89
- @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails is retrying initialize (#{ count } )" )
95
+ log_message ( "Ruby LSP Rails is retrying initialize (#{ count } )" )
90
96
retry if count < MAX_RETRIES
91
97
end
92
98
93
- @message_queue << RubyLsp :: Notification . window_log_message ( "Finished booting Ruby LSP Rails server" )
99
+ log_message ( "Finished booting Ruby LSP Rails server" )
94
100
95
101
unless ENV [ "RAILS_ENV" ] == "test"
96
102
at_exit do
@@ -108,7 +114,7 @@ def initialize(message_queue)
108
114
def register_server_addon ( server_addon_path )
109
115
send_notification ( "server_addon/register" , server_addon_path : server_addon_path )
110
116
rescue IncompleteMessageError
111
- @message_queue << RubyLsp :: Notification . window_log_message (
117
+ log_message (
112
118
"Ruby LSP Rails failed to register server addon #{ server_addon_path } " ,
113
119
type : RubyLsp ::Constant ::MessageType ::ERROR ,
114
120
)
@@ -119,7 +125,7 @@ def register_server_addon(server_addon_path)
119
125
def model ( name )
120
126
make_request ( "model" , name : name )
121
127
rescue IncompleteMessageError
122
- @message_queue << RubyLsp :: Notification . window_log_message (
128
+ log_message (
123
129
"Ruby LSP Rails failed to get model information: #{ @stderr . read } " ,
124
130
type : RubyLsp ::Constant ::MessageType ::ERROR ,
125
131
)
@@ -139,7 +145,7 @@ def association_target_location(model_name:, association_name:)
139
145
association_name : association_name ,
140
146
)
141
147
rescue => e
142
- @message_queue << RubyLsp :: Notification . window_log_message (
148
+ log_message (
143
149
"Ruby LSP Rails failed with #{ e . message } : #{ @stderr . read } " ,
144
150
type : RubyLsp ::Constant ::MessageType ::ERROR ,
145
151
)
@@ -150,7 +156,7 @@ def association_target_location(model_name:, association_name:)
150
156
def route_location ( name )
151
157
make_request ( "route_location" , name : name )
152
158
rescue IncompleteMessageError
153
- @message_queue << RubyLsp :: Notification . window_log_message (
159
+ log_message (
154
160
"Ruby LSP Rails failed to get route location: #{ @stderr . read } " ,
155
161
type : RubyLsp ::Constant ::MessageType ::ERROR ,
156
162
)
@@ -161,7 +167,7 @@ def route_location(name)
161
167
def route ( controller :, action :)
162
168
make_request ( "route_info" , controller : controller , action : action )
163
169
rescue IncompleteMessageError
164
- @message_queue << RubyLsp :: Notification . window_log_message (
170
+ log_message (
165
171
"Ruby LSP Rails failed to get route information: #{ @stderr . read } " ,
166
172
type : RubyLsp ::Constant ::MessageType ::ERROR ,
167
173
)
@@ -170,10 +176,10 @@ def route(controller:, action:)
170
176
171
177
sig { void }
172
178
def trigger_reload
173
- @message_queue << RubyLsp :: Notification . window_log_message ( "Reloading Rails application" )
179
+ log_message ( "Reloading Rails application" )
174
180
send_notification ( "reload" )
175
181
rescue IncompleteMessageError
176
- @message_queue << RubyLsp :: Notification . window_log_message (
182
+ log_message (
177
183
"Ruby LSP Rails failed to trigger reload" ,
178
184
type : RubyLsp ::Constant ::MessageType ::ERROR ,
179
185
)
@@ -182,7 +188,7 @@ def trigger_reload
182
188
183
189
sig { void }
184
190
def shutdown
185
- @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails shutting down server" )
191
+ log_message ( "Ruby LSP Rails shutting down server" )
186
192
send_message ( "shutdown" )
187
193
sleep ( 0.5 ) # give the server a bit of time to shutdown
188
194
[ @stdin , @stdout , @stderr ] . each ( &:close )
@@ -241,7 +247,7 @@ def read_response
241
247
response = JSON . parse ( T . must ( raw_response ) , symbolize_names : true )
242
248
243
249
if response [ :error ]
244
- @message_queue << RubyLsp :: Notification . window_log_message (
250
+ log_message (
245
251
"Ruby LSP Rails error: #{ response [ :error ] } " ,
246
252
type : RubyLsp ::Constant ::MessageType ::ERROR ,
247
253
)
@@ -259,6 +265,13 @@ def force_kill
259
265
# Windows does not support the `TERM` signal, so we're forced to use `KILL` here
260
266
Process . kill ( T . must ( Signal . list [ "KILL" ] ) , @wait_thread . pid )
261
267
end
268
+
269
+ sig { params ( message : ::String , type : ::Integer ) . void }
270
+ def log_message ( message , type : RubyLsp ::Constant ::MessageType ::LOG )
271
+ return if @outgoing_queue . closed?
272
+
273
+ @outgoing_queue << RubyLsp ::Notification . window_log_message ( message , type : type )
274
+ end
262
275
end
263
276
264
277
class NullClient < RunnerClient
0 commit comments