Class: Steep::Server::TypeCheckWorker

Inherits:
BaseWorker show all
Includes:
ChangeBuffer
Defined in:
lib/steep/server/type_check_worker.rb

Defined Under Namespace

Classes: GotoJob

Constant Summary collapse

WorkspaceSymbolJob =
_ = Struct.new(:query, :id, keyword_init: true)
StatsJob =
_ = Struct.new(:id, keyword_init: true)
QueryDefinitionJob =
_ = Struct.new(:id, :name, keyword_init: true)
QueryDiagnosticsJob =
_ = Struct.new(:id, keyword_init: true)
StartTypeCheckJob =
_ = Struct.new(:guid, :changes, keyword_init: true)
TypeCheckCodeJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)
ValidateAppSignatureJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)
ValidateLibrarySignatureJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)
TypeCheckInlineCodeJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)

Constants inherited from BaseWorker

BaseWorker::LSP, BaseWorker::ShutdownJob

Instance Attribute Summary collapse

Attributes included from ChangeBuffer

#buffered_changes, #mutex

Attributes inherited from BaseWorker

#queue, #reader, #writer

Instance Method Summary collapse

Methods included from ChangeBuffer

#collect_changes, #load_files, #pop_buffer, #push_buffer, #reset_change

Methods inherited from BaseWorker

#run, #skip_job?, #skip_jobs_after_shutdown!, #skip_jobs_after_shutdown?

Constructor Details

#initialize(project:, reader:, writer:, assignment:, commandline_args:, io_socket: nil, buffered_changes: nil, service: nil) ⇒ TypeCheckWorker

Returns a new instance of TypeCheckWorker.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/steep/server/type_check_worker.rb', line 60

def initialize(project:, reader:, writer:, assignment:, commandline_args:, io_socket: nil, buffered_changes: nil, service: nil)
  super(project: project, reader: reader, writer: writer)

  @assignment = assignment
  @buffered_changes = buffered_changes || {}
  @mutex = Mutex.new()
  @queue = Queue.new
  @commandline_args = commandline_args
  @current_type_check_guid = nil
  @io_socket = io_socket
  @service = service if service
  @child_pids = []
  @need_to_warmup = true

  if io_socket
    Signal.trap "SIGCHLD" do
      while pid = Process.wait(-1, Process::WNOHANG)
        raise "Unexpected worker process exit: #{pid}" if @child_pids.include?(pid)
      end
    end
  end
end

Instance Attribute Details

#assignmentObject (readonly)

Returns the value of attribute assignment.



4
5
6
# File 'lib/steep/server/type_check_worker.rb', line 4

def assignment
  @assignment
end

#commandline_argsObject (readonly)

Returns the value of attribute commandline_args.



5
6
7
# File 'lib/steep/server/type_check_worker.rb', line 5

def commandline_args
  @commandline_args
end

#current_type_check_guidObject (readonly)

Returns the value of attribute current_type_check_guid.



6
7
8
# File 'lib/steep/server/type_check_worker.rb', line 6

def current_type_check_guid
  @current_type_check_guid
end

#io_socketObject (readonly)

Returns the value of attribute io_socket.



57
58
59
# File 'lib/steep/server/type_check_worker.rb', line 57

def io_socket
  @io_socket
end

#need_to_warmupObject (readonly)

Returns the value of attribute need_to_warmup.



58
59
60
# File 'lib/steep/server/type_check_worker.rb', line 58

def need_to_warmup
  @need_to_warmup
end

#projectObject (readonly)

Returns the value of attribute project.



4
5
6
# File 'lib/steep/server/type_check_worker.rb', line 4

def project
  @project
end

Instance Method Details

#enqueue_typecheck_jobs(params) ⇒ Object



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
# File 'lib/steep/server/type_check_worker.rb', line 167

def enqueue_typecheck_jobs(params)
  guid = params[:guid]

  @current_type_check_guid = guid

  pop_buffer() do |changes|
    Steep.logger.info { "Enqueueing StartTypeCheckJob for guid=#{guid}" }
    queue << StartTypeCheckJob.new(guid: guid, changes: changes)
  end

  targets = project.targets.each.with_object({}) do |target, hash| #$ Hash[String, Project::Target]
    hash[target.name.to_s] = target
  end

  priority_paths = Set.new(params[:priority_uris].map {|uri| Steep::PathHelper.to_pathname!(uri) })
  libraries = params[:library_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]
  signatures = params[:signature_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]
  codes = params[:code_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]
  inlines = params[:inline_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]

  priority_libs, non_priority_libs = libraries.partition {|_, path| priority_paths.include?(path) }
  priority_sigs, non_priority_sigs = signatures.partition {|_, path| priority_paths.include?(path) }
  priority_codes, non_priority_codes = codes.partition {|_, path| priority_paths.include?(path) }
  priority_inlines, non_priority_inlines = inlines.partition {|_, path| priority_paths.include?(path) }

  priority_codes.each do |target, path|
    Steep.logger.info { "Enqueueing TypeCheckCodeJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << TypeCheckCodeJob.new(guid: guid, path: path, target: target)
  end

  priority_sigs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateAppSignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateAppSignatureJob.new(guid: guid, path: path, target: target)
  end

  priority_libs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateLibrarySignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateLibrarySignatureJob.new(guid: guid, path: path, target: target)
  end

  priority_inlines.each do |target, path|
    Steep.logger.info { "Enqueueing TypeCheckInlineCodeJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << TypeCheckInlineCodeJob.new(guid: guid, path: path, target: target)
  end

  non_priority_codes.each do |target, path|
    Steep.logger.info { "Enqueueing TypeCheckCodeJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << TypeCheckCodeJob.new(guid: guid, path: path, target: target)
  end

  non_priority_sigs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateAppSignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateAppSignatureJob.new(guid: guid, path: path, target: target)
  end

  non_priority_libs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateLibrarySignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateLibrarySignatureJob.new(guid: guid, path: path, target: target)
  end

  non_priority_inlines.each do |target, path|
    Steep.logger.info { "Enqueueing TypeCheckInlineCodeJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << TypeCheckInlineCodeJob.new(guid: guid, path: path, target: target)
  end
end

#goto(job) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/steep/server/type_check_worker.rb', line 459

def goto(job)
  path = Steep::PathHelper.to_pathname(job.params[:textDocument][:uri]) or return []
  line = job.params[:position][:line] + 1
  column = job.params[:position][:character]

  goto_service = Services::GotoService.new(type_check: service, assignment: assignment)
  locations =
    case
    when job.definition?
      goto_service.definition(path: path, line: line, column: column)
    when job.implementation?
      goto_service.implementation(path: path, line: line, column: column)
    when job.type_definition?
      goto_service.type_definition(path: path, line: line, column: column)
    else
      raise
    end

  locations.map do |loc|
    path =
      case loc
      when RBS::Location
        Pathname(loc.buffer.name)
      else
        Pathname(loc.source_buffer.name)
      end

    path = project.absolute_path(path)

    {
      uri: Steep::PathHelper.to_uri(path).to_s,
      range: loc.as_lsp_range
    }
  end
end

#handle_job(job) ⇒ Object



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
# File 'lib/steep/server/type_check_worker.rb', line 233

def handle_job(job)
  case job
  when StartTypeCheckJob
    Steep.logger.info { "Processing StartTypeCheckJob for guid=#{job.guid}" }
    service.update(changes: job.changes)

  when ValidateAppSignatureJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing ValidateAppSignature for guid=#{job.guid}, path=#{job.path}" }

      formatter = Diagnostic::LSPFormatter.new({}, **{})

      diagnostics = service.validate_signature(path: project.relative_path(job.path), target: job.target)

      typecheck_progress(
        path: job.path,
        guid: job.guid,
        target: job.target,
        diagnostics: diagnostics.filter_map { formatter.format(_1) }
      )
    end

  when ValidateLibrarySignatureJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing ValidateLibrarySignature for guid=#{job.guid}, path=#{job.path}" }

      formatter = Diagnostic::LSPFormatter.new({}, **{})
      diagnostics = service.validate_signature(path: job.path, target: job.target)

      typecheck_progress(path: job.path, guid: job.guid, target: job.target, diagnostics: diagnostics.filter_map { formatter.format(_1) })
    end

  when TypeCheckCodeJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing TypeCheckCodeJob for guid=#{job.guid}, path=#{job.path}, target=#{job.target.name}" }
      group_target = project.group_for_source_path(job.path) || job.target
      formatter = Diagnostic::LSPFormatter.new(group_target.code_diagnostics_config)
      relative_path = project.relative_path(job.path)
      diagnostics = service.typecheck_source(path: relative_path, target: job.target)
      typecheck_progress(path: job.path, guid: job.guid, target: job.target, diagnostics: diagnostics&.filter_map { formatter.format(_1) })
    end

  when TypeCheckInlineCodeJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing TypeCheckInlineCodeJob for guid=#{job.guid}, path=#{job.path}, target=#{job.target.name}" }
      group_target = project.group_for_inline_source_path(job.path) || job.target
      formatter = Diagnostic::LSPFormatter.new(group_target.code_diagnostics_config)
      relative_path = project.relative_path(job.path)
      diagnostics = service.typecheck_source(path: relative_path, target: job.target) #: Array[Diagnostic::Ruby::Base | Diagnostic::Signature::Base] | nil
      signature_diagnostics = service.validate_signature(path: relative_path, target: job.target)
      if diagnostics
        diagnostics.concat(signature_diagnostics)
      else
        unless signature_diagnostics.empty?
          diagnostics = signature_diagnostics
        end
      end

      typecheck_progress(path: job.path, guid: job.guid, target: job.target, diagnostics: diagnostics&.filter_map { formatter.format(_1) })
    end

  when WorkspaceSymbolJob
    writer.write(
      id: job.id,
      result: workspace_symbol_result(job.query)
    )
  when StatsJob
    writer.write(
      id: job.id,
      result: stats_result().map(&:as_json)
    )
  when GotoJob
    writer.write(
      id: job.id,
      result: goto(job)
    )
  when QueryDefinitionJob
    writer.write(
      CustomMethods::Query__Definition.response(job.id, query_definition_result(job.name))
    )
  when QueryDiagnosticsJob
    writer.write(
      CustomMethods::Query__Diagnostics.response(job.id, query_diagnostics_result())
    )
  end
end

#handle_request(request) ⇒ Object



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
# File 'lib/steep/server/type_check_worker.rb', line 87

def handle_request(request)
  case request[:method]
  when "initialize"
    writer.write({ id: request[:id], result: nil})

  when "textDocument/didChange"
    collect_changes(request)

  when CustomMethods::FileLoad::METHOD
    input = request[:params][:content]
    load_files(input)

  when CustomMethods::FileReset::METHOD
    params = request[:params] #: CustomMethods::FileReset::params
    uri = params[:uri]
    text = params[:content]
    reset_change(uri: uri, text: text)

  when "workspace/symbol"
    query = request[:params][:query]
    queue << WorkspaceSymbolJob.new(id: request[:id], query: query)
  when CustomMethods::Stats::METHOD
    queue << StatsJob.new(id: request[:id])
  when CustomMethods::TypeCheck__Start::METHOD
    params = request[:params] #: CustomMethods::TypeCheck__Start::params
    enqueue_typecheck_jobs(params)
  when CustomMethods::Query__Definition::METHOD
    params = request[:params] #: CustomMethods::Query__Definition::params
    queue << QueryDefinitionJob.new(id: request[:id], name: params[:name])
  when CustomMethods::Query__Diagnostics::METHOD
    queue << QueryDiagnosticsJob.new(id: request[:id])
  when "textDocument/definition"
    queue << GotoJob.definition(id: request[:id], params: request[:params])
  when "textDocument/implementation"
    queue << GotoJob.implementation(id: request[:id], params: request[:params])
  when "textDocument/typeDefinition"
    queue << GotoJob.type_definition(id: request[:id], params: request[:params])
  when CustomMethods::Refork::METHOD
    io_socket or raise

    # Receive IOs before fork to avoid receiving them from multiple processes
    stdin = io_socket.recv_io
    stdout = io_socket.recv_io

    if need_to_warmup
      Process.warmup
      @need_to_warmup = false
    end

    if pid = fork
      stdin.close
      stdout.close
      @child_pids << pid
      writer.write(CustomMethods::Refork.response(request[:id], { pid: }))
    else
      io_socket.close

      reader.close
      writer.close

      reader = LanguageServer::Protocol::Transport::Io::Reader.new(stdin)
      writer = LanguageServer::Protocol::Transport::Io::Writer.new(stdout)
      Steep.logger.info("Reforked worker: #{Process.pid}, params: #{request[:params]}")
      index = request[:params][:index]
      assignment = Services::PathAssignment.new(max_index: request[:params][:max_index], index: index)

      worker = self.class.new(project: project, reader: reader, writer: writer, assignment: assignment, commandline_args: commandline_args, io_socket: nil, buffered_changes: buffered_changes, service: service)

      tags = Steep.logger.current_tags.dup
      if (index = tags.find_index("typecheck:typecheck@0"))
        tags[index] = "typecheck:typecheck@#{index}-reforked"
      end
      Steep.logger.push_tags(*tags)
      worker.run()

      raise "unreachable"
    end
  end
end

#query_definition_result(name_string) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/steep/server/type_check_worker.rb', line 407

def query_definition_result(name_string)
  name = Services::GotoService.parse_name(name_string)

  kind =
    case name
    when RBS::TypeName
      "type_name"
    when InstanceMethodName
      "instance_method"
    when SingletonMethodName
      "singleton_method"
    else
      "unknown"
    end #: CustomMethods::Query__Definition::kind

  locations = [] #: Array[CustomMethods::Query__Definition::location]

  if name
    goto_service = Services::GotoService.new(type_check: service, assignment: assignment)
    goto_service.query_definition(name).each do |loc|
      case loc
      when RBS::Location
        path = Pathname(loc.buffer.name)
        source = "rbs" #: CustomMethods::Query__Definition::source
        if path.extname == ".rb"
          source = "ruby" #: CustomMethods::Query__Definition::source
        end
        path = project.absolute_path(path)
        locations << {
          uri: Steep::PathHelper.to_uri(path).to_s,
          range: loc.as_lsp_range,
          source: source
        }
      else
        path = Pathname(loc.source_buffer.name)
        path = project.absolute_path(path)
        locations << {
          uri: Steep::PathHelper.to_uri(path).to_s,
          range: loc.as_lsp_range,
          source: "ruby"
        }
      end
    end
  end

  {
    name: name_string,
    kind: kind,
    locations: locations
  }
end

#query_diagnostics_resultObject

Returns the diagnostics of the files this worker has type checked so far

An array of Query__Diagnostics::entry, with the LSP-formatted diagnostics per file URI. Files that are loaded but not type checked yet are not included.



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
# File 'lib/steep/server/type_check_worker.rb', line 373

def query_diagnostics_result
  result = {} #: Hash[String, Array[untyped]]

  service.source_files.each_value do |file|
    next if file.typing.nil? && file.errors.nil?

    absolute_path = project.absolute_path(file.path)

    group_target =
      project.group_for_source_path(absolute_path) ||
      project.group_for_inline_source_path(absolute_path) ||
      project.target_for_source_path(absolute_path) ||
      project.target_for_inline_source_path(absolute_path)
    next unless group_target

    formatter = Diagnostic::LSPFormatter.new(group_target.code_diagnostics_config)
    uri = PathHelper.to_uri(absolute_path).to_s
    array = result[uri] ||= []
    array.concat(file.diagnostics.filter_map { formatter.format(_1) })
  end

  formatter = Diagnostic::LSPFormatter.new({}, **{})
  service.signature_validation_diagnostics.each_value do |path_diagnostics|
    path_diagnostics.each do |path, diagnostics|
      absolute_path = path.absolute? ? path : project.absolute_path(path)
      uri = PathHelper.to_uri(absolute_path).to_s
      array = result[uri] ||= []
      array.concat(diagnostics.filter_map { formatter.format(_1) })
    end
  end

  result.map { { uri: _1, diagnostics: _2 } }
end

#serviceObject



83
84
85
# File 'lib/steep/server/type_check_worker.rb', line 83

def service
  @service ||= Services::TypeCheckService.new(project: project)
end

#stats_resultObject



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/steep/server/type_check_worker.rb', line 354

def stats_result
  calculator = Services::StatsCalculator.new(service: service)

  project.targets.each.with_object([]) do |target, stats|
    service.source_files.each_value do |file|
      next unless target.possible_source_file?(file.path)
      absolute_path = project.absolute_path(file.path)
      next unless assignment =~ [target, absolute_path]

      stats << calculator.calc_stats(target, file: file)
    end
  end
end

#typecheck_progress(guid:, path:, target:, diagnostics:) ⇒ Object



320
321
322
# File 'lib/steep/server/type_check_worker.rb', line 320

def typecheck_progress(guid:, path:, target:, diagnostics:)
  writer.write(CustomMethods::TypeCheck__Progress.notification({ guid: guid, path: path.to_s, target: target.name.to_s, diagnostics: diagnostics }))
end

#workspace_symbol_result(query) ⇒ Object



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
# File 'lib/steep/server/type_check_worker.rb', line 324

def workspace_symbol_result(query)
  Steep.measure "Generating workspace symbol list for query=`#{query}`" do
    provider = Index::SignatureSymbolProvider.new(project: project, assignment: assignment)
    project.targets.each do |target|
      index = service.signature_services.fetch(target.name).latest_rbs_index
      provider.indexes[target] = index
    end

    symbols = provider.query_symbol(query)

    symbols.map do |symbol|
      LSP::Interface::SymbolInformation.new(
        name: symbol.name,
        kind: symbol.kind,
        location: symbol.location.yield_self do |location|
          path = Pathname(location.buffer.name)
          {
            uri: Steep::PathHelper.to_uri(project.absolute_path(path)),
            range: {
              start: { line: location.start_line - 1, character: location.start_column },
              end: { line: location.end_line - 1, character: location.end_column }
            }
          }
        end,
        container_name: symbol.container_name
      )
    end
  end
end