Skip to content

Commit

Permalink
Make sure stdout is clean during all request processing
Browse files Browse the repository at this point in the history
RuboCop is still too chatty and breaks the protocol by printing to
stdout despite our best attempts. Today I was having problems with the
formatting on Tapioca and realized it was because of the:
```
An error occurred while Layout/BlockEndNewline cop was inspecting /Users/ufuk/src/github.com/Shopify/tapioca/lib/tapioca/static/rbs_converter.rb:166:8.
To see the complete backtrace run rubocop -d.
```
problem (which is in per-1.34 versions of RuboCop). I am not exactly
sure why this was causing a problem, since that specific message should
be going to stderr, but the LSP communication was still broken because
of it.

Regardless, I realized that we never want any request to print to
stdout, willingly or by mistake, so I decided to resolve that at the
handler layer.
  • Loading branch information
paracycle committed Aug 31, 2022
1 parent e5c3fd0 commit b7bffbe
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/ruby_lsp/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def execute(request)
error = T.let(nil, T.nilable(StandardError))

request_time = Benchmark.realtime do
response = T.must(@handlers[request[:method]]).action.call(request)
response = capture_stdout do
T.must(@handlers[request[:method]]).action.call(request)
end
rescue StandardError => e
error = e
end
Expand Down Expand Up @@ -131,6 +133,18 @@ def finalize_request(result, request)

private

sig do
type_parameters(:T).params(block: T.proc.returns(T.type_parameter(:T))).returns(T.type_parameter(:T))
end
def capture_stdout(&block)
orig_stdout = $stdout
$stdout = StringIO.new

block.call
ensure
$stdout = orig_stdout
end

sig { returns(Thread) }
def new_worker
Thread.new do
Expand Down

0 comments on commit b7bffbe

Please sign in to comment.