Skip to content

Commit 929422d

Browse files
committed
Fix --port option not being passed to LSP socket server
The --port CLI option was parsed correctly but never forwarded to Server.start_socket, causing the server to always bind to an OS-assigned random port.
1 parent ae070ab commit 929422d

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

lib/typeprof/cli/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def run_lsp
125125
if @lsp_options[:stdio]
126126
TypeProf::LSP::Server.start_stdio(@core_options)
127127
else
128-
TypeProf::LSP::Server.start_socket(@core_options)
128+
TypeProf::LSP::Server.start_socket(@core_options, @lsp_options[:port])
129129
end
130130
rescue Exception
131131
puts $!.detailed_message(highlight: false).gsub(/^/, "---")

lib/typeprof/lsp/server.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def self.start_stdio(core_options)
2121
new(core_options, reader, writer).run
2222
end
2323

24-
def self.start_socket(core_options)
25-
Socket.tcp_server_sockets("localhost", nil) do |servs|
24+
def self.start_socket(core_options, port = 0)
25+
Socket.tcp_server_sockets("localhost", port) do |servs|
2626
serv = servs[0].local_address
2727
$stdout << JSON.generate({
2828
host: serv.ip_address,

test/cli_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ def test_e2e_no_show_stats
186186
assert_not_include(result, "TypeProf Evaluation Statistics")
187187
end
188188

189+
def test_lsp_port_option
190+
# Find an available port from OS
191+
tmp_server = TCPServer.new("localhost", 0)
192+
port = tmp_server.addr[1]
193+
tmp_server.close
194+
195+
read_pipe, write_pipe = IO.pipe
196+
original_stdout = $stdout
197+
$stdout = write_pipe
198+
199+
cli = TypeProf::CLI::CLI.new(["--lsp", "--port", port.to_s])
200+
th = Thread.new { cli.run }
201+
202+
IO.select([read_pipe], nil, nil, 5)
203+
output = read_pipe.read_nonblock(4096)
204+
json = JSON.parse(output, symbolize_names: true)
205+
206+
assert_equal(port, json[:port])
207+
ensure
208+
th&.kill
209+
th&.join(1)
210+
$stdout = original_stdout
211+
write_pipe&.close unless write_pipe&.closed?
212+
read_pipe&.close unless read_pipe&.closed?
213+
end
214+
189215
def test_lsp_options_with_lsp_mode
190216
assert_nothing_raised { TypeProf::CLI::CLI.new(["--lsp", "--stdio"]) }
191217
end

0 commit comments

Comments
 (0)