Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default output device to stderr #451

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ class Server
VOID = Object.new

def initialize
$stdin.sync = true
$stdout.sync = true
$stdin.binmode
$stdout.binmode
# Grab references to the original pipes so that we can change the default output device further down
@stdin = $stdin
@stdout = $stdout
@stderr = $stderr
@stdin.sync = true
@stdout.sync = true
@stderr.sync = true
@stdin.binmode
@stdout.binmode
@stderr.binmode

# # Set the default output device to be $stderr. This means that using `puts` by itself will default to printing
# # to $stderr and only explicit `$stdout.puts` will go to $stdout. This reduces the chance that output coming
# # from the Rails app will be accidentally sent to the client
$> = $stderr
vinistock marked this conversation as resolved.
Show resolved Hide resolved

@running = true
end

Expand All @@ -25,18 +37,18 @@ def start
routes_reloader.execute_unless_loaded if routes_reloader&.respond_to?(:execute_unless_loaded)

initialize_result = { result: { message: "ok", root: ::Rails.root.to_s } }.to_json
$stdout.write("Content-Length: #{initialize_result.length}\r\n\r\n#{initialize_result}")
@stdout.write("Content-Length: #{initialize_result.length}\r\n\r\n#{initialize_result}")

while @running
headers = $stdin.gets("\r\n\r\n")
json = $stdin.read(headers[/Content-Length: (\d+)/i, 1].to_i)
headers = @stdin.gets("\r\n\r\n")
json = @stdin.read(headers[/Content-Length: (\d+)/i, 1].to_i)

request = JSON.parse(json, symbolize_names: true)
response = execute(request.fetch(:method), request[:params])
next if response == VOID

json_response = response.to_json
$stdout.write("Content-Length: #{json_response.length}\r\n\r\n#{json_response}")
@stdout.write("Content-Length: #{json_response.length}\r\n\r\n#{json_response}")
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,22 @@ def <(other)
assert_equal "GET", result[:verb]
assert_equal "/users(.:format)", result[:path]
end

test "prints in the Rails application or server are automatically redirected to stderr" do
server = RubyLsp::Rails::Server.new

server.instance_eval do
def resolve_route_info(requirements)
puts "Hello"
super
end
end

stdout, stderr = capture_subprocess_io do
server.execute("route_info", { controller: "UsersController", action: "index" })
end

assert_empty(stdout)
assert_equal("Hello\n", stderr)
end
end
Loading