-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrunner_client.rb
More file actions
419 lines (360 loc) · 12.1 KB
/
runner_client.rb
File metadata and controls
419 lines (360 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# typed: strict
# frozen_string_literal: true
require "json"
require "open3"
module RubyLsp
module Rails
class RunnerClient
class << self
#: (Thread::Queue outgoing_queue, RubyLsp::GlobalState global_state) -> RunnerClient
def create_client(outgoing_queue, global_state)
if File.exist?("bin/rails")
new(outgoing_queue, global_state)
else
unless outgoing_queue.closed?
outgoing_queue << RubyLsp::Notification.window_log_message(
<<~MESSAGE.chomp,
Ruby LSP Rails failed to locate bin/rails in the current directory: #{Dir.pwd}
Server dependent features will not be available
MESSAGE
type: RubyLsp::Constant::MessageType::WARNING,
)
end
NullClient.new
end
rescue StandardError => e
unless outgoing_queue.closed?
outgoing_queue << RubyLsp::Notification.window_log_message(
<<~MESSAGE.chomp,
Ruby LSP Rails failed to initialize server: #{e.full_message}
Server dependent features will not be available
MESSAGE
type: Constant::MessageType::ERROR,
)
end
NullClient.new
end
end
class InitializationError < StandardError; end
class MessageError < StandardError; end
class EmptyMessageError < MessageError; end
#: String
attr_reader :rails_root
#: (Thread::Queue outgoing_queue, RubyLsp::GlobalState global_state) -> void
def initialize(outgoing_queue, global_state)
@outgoing_queue = outgoing_queue #: Thread::Queue
@mutex = Mutex.new #: Mutex
# Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
# set its own session ID
begin
Process.setpgrp
Process.setsid
rescue Errno::EPERM
# If we can't set the session ID, continue
rescue NotImplementedError
# setpgrp() may be unimplemented on some platform
# https://github.com/Shopify/ruby-lsp-rails/issues/348
end
log_message("Ruby LSP Rails booting server")
stdin, stdout, stderr, wait_thread = Bundler.with_original_env do
Open3.popen3(
{ "RUBY_LSP_RAILS_RUNNER" => "true" },
"bundle",
"exec",
"rails",
"runner",
"#{__dir__}/server.rb",
"start",
server_relevant_capabilities(global_state),
)
end
@stdin = stdin #: IO
@stdout = stdout #: IO
@stderr = stderr #: IO
@stdin.sync = true
@stdout.sync = true
@stderr.sync = true
@wait_thread = wait_thread #: Process::Waiter
# We set binmode for Windows compatibility
@stdin.binmode
@stdout.binmode
@stderr.binmode
initialize_response = read_response #: as !nil
@rails_root = initialize_response[:root] #: String
log_message("Finished booting Ruby LSP Rails server")
unless ENV["RAILS_ENV"] == "test"
at_exit do
if @wait_thread.alive?
sleep(0.5) # give the server a bit of time if we already issued a shutdown notification
force_kill
end
end
end
# Responsible for transmitting notifications coming from the server to the outgoing queue, so that we can do
# things such as showing progress notifications initiated by the server
@notifier_thread = Thread.new do
until @stderr.closed?
notification = read_notification
unless @outgoing_queue.closed? || !notification
@outgoing_queue << notification
end
end
rescue IOError
# The server was shutdown and stderr is already closed
end #: Thread
rescue StandardError
raise InitializationError, @stderr.read
end
#: (String server_addon_path) -> void
def register_server_addon(server_addon_path)
send_notification("server_addon/register", server_addon_path: server_addon_path)
rescue MessageError
log_message(
"Ruby LSP Rails failed to register server addon #{server_addon_path}",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
#: (String name) -> Hash[Symbol, untyped]?
def model(name)
make_request("model", name: name)
rescue MessageError
log_message(
"Ruby LSP Rails failed to get model information",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
#: (model_name: String, association_name: String) -> Hash[Symbol, untyped]?
def association_target(model_name:, association_name:)
make_request(
"association_target",
model_name: model_name,
association_name: association_name,
)
rescue MessageError
log_message(
"Ruby LSP Rails failed to get association location",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
#: (String name) -> Hash[Symbol, untyped]?
def route_location(name)
make_request("route_location", name: name)
rescue MessageError
log_message(
"Ruby LSP Rails failed to get route location",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
#: (controller: String, action: String) -> Hash[Symbol, untyped]?
def route(controller:, action:)
make_request("route_info", controller: controller, action: action)
rescue MessageError
log_message(
"Ruby LSP Rails failed to get route information",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
# Delegates a notification to a server add-on
#: (server_addon_name: String, request_name: String, **untyped params) -> void
def delegate_notification(server_addon_name:, request_name:, **params)
send_notification(
"server_addon/delegate",
request_name: request_name,
server_addon_name: server_addon_name,
**params,
)
end
#: -> String?
def pending_migrations_message
response = make_request("pending_migrations_message")
response[:pending_migrations_message] if response
rescue MessageError
log_message(
"Ruby LSP Rails failed when checking for pending migrations",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
#: -> Hash[Symbol, untyped]?
def run_migrations
make_request("run_migrations")
rescue MessageError
log_message(
"Ruby LSP Rails failed to run migrations",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
# Delegates a request to a server add-on
#: (server_addon_name: String, request_name: String, **untyped params) -> Hash[Symbol, untyped]?
def delegate_request(server_addon_name:, request_name:, **params)
make_request(
"server_addon/delegate",
server_addon_name: server_addon_name,
request_name: request_name,
**params,
)
rescue MessageError
nil
end
#: -> void
def trigger_reload
log_message("Reloading Rails application")
send_notification("reload")
rescue MessageError
log_message(
"Ruby LSP Rails failed to trigger reload",
type: RubyLsp::Constant::MessageType::ERROR,
)
nil
end
#: -> void
def shutdown
log_message("Ruby LSP Rails shutting down server")
send_message("shutdown")
sleep(0.5) # give the server a bit of time to shutdown
[@stdin, @stdout, @stderr].each(&:close)
rescue IOError
# The server connection may have died
force_kill
end
#: -> bool
def stopped?
[@stdin, @stdout, @stderr].all?(&:closed?) && !@wait_thread.alive?
end
#: -> bool
def connected?
true
end
def views_dir
File.join(@rails_root, "app/views")
end
private
#: (String request, **untyped params) -> Hash[Symbol, untyped]?
def make_request(request, **params)
send_message(request, **params)
read_response
end
# Notifications are like messages, but one-way, with no response sent back.
#: (String request, **untyped params) -> void
def send_notification(request, **params) = send_message(request, **params)
# @overridable
#: (String request, **untyped params) -> void
def send_message(request, **params)
message = { method: request }
message[:params] = params
json = message.to_json
@mutex.synchronize do
@stdin.write("Content-Length: #{json.bytesize}\r\n\r\n", json)
end
rescue Errno::EPIPE
# The server connection died
end
# @overridable
#: -> Hash[Symbol, untyped]?
def read_response
raw_response = @mutex.synchronize do
content_length = read_content_length
content_length = read_content_length unless content_length
raise EmptyMessageError unless content_length
@stdout.read(content_length)
end #: as !nil
response = JSON.parse(raw_response, symbolize_names: true)
if response[:error]
log_message(
"Ruby LSP Rails error: #{response[:error]}",
type: RubyLsp::Constant::MessageType::ERROR,
)
return
end
response.fetch(:result)
rescue Errno::EPIPE
# The server connection died
nil
end
#: -> void
def force_kill
# Windows does not support the `TERM` signal, so we're forced to use `KILL` here
Process.kill(
Signal.list["KILL"], #: as !nil
@wait_thread.pid,
)
end
#: (::String message, ?type: ::Integer) -> void
def log_message(message, type: RubyLsp::Constant::MessageType::LOG)
return if @outgoing_queue.closed?
@outgoing_queue << RubyLsp::Notification.window_log_message(message, type: type)
end
#: -> Integer?
def read_content_length
headers = @stdout.gets("\r\n\r\n")
return unless headers
length = headers[/Content-Length: (\d+)/i, 1]
return unless length
length.to_i
end
# Read a server notification from stderr. Only intended to be used by notifier thread
#: -> Hash[Symbol, untyped]?
def read_notification
headers = @stderr.gets("\r\n\r\n")
return unless headers
length = headers[/Content-Length: (\d+)/i, 1]
return unless length
raw_content = @stderr.read(length.to_i)
return unless raw_content
JSON.parse(raw_content, symbolize_names: true)
end
#: (GlobalState global_state) -> String
def server_relevant_capabilities(global_state)
{
supports_progress: global_state.client_capabilities.supports_progress,
}.to_json
end
end
class NullClient < RunnerClient
#: -> void
def initialize # rubocop:disable Lint/MissingSuper
end
# @override
#: -> void
def shutdown
# no-op
end
# @override
#: -> bool
def stopped?
true
end
# @override
#: -> String
def rails_root
Dir.pwd
end
#: -> bool
def connected?
false
end
private
#: (::String message, ?type: ::Integer) -> void
def log_message(message, type: RubyLsp::Constant::MessageType::LOG)
# no-op
end
# @override
#: (String request, **untyped params) -> void
def send_message(request, **params)
# no-op
end
# @override
#: -> Hash[Symbol, untyped]?
def read_response
# no-op
end
end
end
end