Skip to content

Commit

Permalink
Try timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 26, 2024
1 parent 9d5301e commit f191a20
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
11 changes: 6 additions & 5 deletions exe/ruby-lsp-launcher
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ locked_bundler_version_file = File.join(".ruby-lsp", "locked_bundler_version")
# `$LOAD_PATH` and `Gem.loaded_specs`. Windows doesn't support forking, so we need a separate path to support it
pid = if Gem.win_platform?
Process.spawn(
ENV.to_hash,
Gem.ruby,
File.expand_path("../lib/ruby_lsp/scripts/compose_bundle_windows.rb", __dir__),
raw_initialize,
)
else
fork do
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
Gem::Specification.find_by_name("ruby-lsp").activate
require_relative "../lib/ruby_lsp/scripts/compose_bundle"
compose(raw_initialize)
end
Expand Down Expand Up @@ -61,11 +62,11 @@ rescue StandardError => e
# features in a degraded mode. We simply save the error so that we can report to the user that certain gems might be
# missing, but we respect the LSP life cycle
setup_error = e
$stderr.puts("Failed to set up composed Bundle\n#{e.full_message}")

# If we failed to set up the bundle, the minimum we need is to have our own dependencies activated so that we can
# require the gems the LSP depends on. If even that fails, then there's no way we can continue to run the language
# server
Gem::Specification.find_by_name("ruby-lsp").activate
# If Bundler.setup fails, we need to restore the original $LOAD_PATH so that we can still require the Ruby LSP server
# in degraded mode
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
end

error_path = File.join(".ruby-lsp", "install_error")
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_lsp/scripts/compose_bundle_windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("../../../lib", __dir__))
Gem::Specification.find_by_name("ruby-lsp").activate
require_relative "compose_bundle"

# When this is invoked on Windows, we pass the raw initialize as an argument to this script. On other platforms, we
Expand Down
40 changes: 21 additions & 19 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ def test_launch_mode_with_no_gemfile_and_bundle_path
Bundler.with_unbundled_env do
system("bundle config --local path #{File.join("vendor", "bundle")}")
assert_path_exists(File.join(dir, ".bundle", "config"))
end

Bundler.with_unbundled_env do
launch(dir)
end
end
Expand All @@ -151,16 +148,7 @@ def test_launch_mode_with_no_gemfile_and_bundle_path
private

def launch(workspace_path)
load_path = $-I.map do |p|
"-I#{File.expand_path(p)}"
end.join(" ")

stdin, stdout, stderr, wait_thr = T.unsafe(Open3).popen3(
ENV.to_hash,
Gem.ruby,
load_path,
File.join(@root, "exe", "ruby-lsp-launcher"),
)
stdin, stdout, stderr, wait_thr = Open3.popen3(Gem.ruby, File.join(@root, "exe", "ruby-lsp-launcher"))
stdin.sync = true
stdin.binmode
stdout.sync = true
Expand All @@ -180,14 +168,28 @@ def launch(workspace_path)
send_message(stdin, { id: 2, method: "shutdown" })
send_message(stdin, { method: "exit" })

begin
Process.wait(wait_thr.pid)
rescue Errno::ECHILD
nil
# Wait until the process exits
wait_thr.join

# If the child process failed, it is really difficult to diagnose what's happening unless we read what was printed
# to stderr
unless T.unsafe(wait_thr.value).success?
require "timeout"

Timeout.timeout(5) do
flunk("Process failed\n#{stderr.read}")
end
end

# temporary
if T.unsafe(wait_thr.value).success?
require "timeout"

Timeout.timeout(5) do
puts stderr.read
end
end

wait_thr.join
refute_predicate(wait_thr, :alive?)
assert_path_exists(File.join(workspace_path, ".ruby-lsp", "bundle_gemfile"))
assert_path_exists(File.join(workspace_path, ".ruby-lsp", "Gemfile"))
assert_path_exists(File.join(workspace_path, ".ruby-lsp", "Gemfile.lock"))
Expand Down

0 comments on commit f191a20

Please sign in to comment.