Skip to content

Commit 2d23e54

Browse files
committed
Move server update after launching server
1 parent 64a03e9 commit 2d23e54

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

lib/ruby_lsp/server.rb

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ def run_initialized
372372

373373
perform_initial_indexing
374374
check_formatter_is_available
375+
update_server
375376
end
376377

377378
#: (Hash[Symbol, untyped] message) -> void
@@ -1417,8 +1418,36 @@ def compose_bundle(message)
14171418

14181419
# We compose the bundle in a thread so that the LSP continues to work while we're checking for its validity. Once
14191420
# we return the response back to the editor, then the restart is triggered
1421+
launch_bundle_compose("Recomposing the bundle ahead of restart") do |stderr, status|
1422+
if status&.exitstatus == 0
1423+
# Create a signal for the restart that it can skip composing the bundle and launch directly
1424+
FileUtils.touch(already_composed_path)
1425+
send_message(Result.new(id: id, response: { success: true }))
1426+
else
1427+
# This special error code makes the extension avoid restarting in case we already know that the composed
1428+
# bundle is not valid
1429+
send_message(
1430+
Error.new(id: id, code: BUNDLE_COMPOSE_FAILED_CODE, message: "Failed to compose bundle\n#{stderr}"),
1431+
)
1432+
end
1433+
end
1434+
end
1435+
1436+
#: -> void
1437+
def update_server
1438+
launch_bundle_compose("Trying to update server") do |stderr, status|
1439+
if status&.exitstatus == 0
1440+
send_log_message("Successfully updated the server")
1441+
else
1442+
send_log_message("Failed to update server\n#{stderr}", type: Constant::MessageType::ERROR)
1443+
end
1444+
end
1445+
end
1446+
1447+
#: (String) { (IO, Process::Status?) -> void } -> Thread
1448+
def launch_bundle_compose(log, &block)
14201449
Thread.new do
1421-
send_log_message("Recomposing the bundle ahead of restart")
1450+
send_log_message(log)
14221451

14231452
_stdout, stderr, status = Bundler.with_unbundled_env do
14241453
Open3.capture3(
@@ -1433,17 +1462,7 @@ def compose_bundle(message)
14331462
)
14341463
end
14351464

1436-
if status&.exitstatus == 0
1437-
# Create a signal for the restart that it can skip composing the bundle and launch directly
1438-
FileUtils.touch(already_composed_path)
1439-
send_message(Result.new(id: id, response: { success: true }))
1440-
else
1441-
# This special error code makes the extension avoid restarting in case we already know that the composed
1442-
# bundle is not valid
1443-
send_message(
1444-
Error.new(id: id, code: BUNDLE_COMPOSE_FAILED_CODE, message: "Failed to compose bundle\n#{stderr}"),
1445-
)
1446-
end
1465+
block.call(stderr, status)
14471466
end
14481467
end
14491468

lib/ruby_lsp/setup_bundler.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,31 +256,31 @@ def run_bundle_install(bundle_gemfile = @gemfile)
256256
#: (Hash[String, String] env, ?force_install: bool) -> Hash[String, String]
257257
def run_bundle_install_directly(env, force_install: false)
258258
RubyVM::YJIT.enable if defined?(RubyVM::YJIT.enable)
259+
return update(env) if File.exist?(@custom_dir + "needs_update")
259260

260261
# The ENV can only be merged after checking if an update is required because we depend on the original value of
261262
# ENV["BUNDLE_GEMFILE"], which gets overridden after the merge
262263
should_update = should_bundle_update?
263-
ENV #: as untyped
264-
.merge!(env)
264+
ENV.merge!(env)
265265

266-
unless should_update && !force_install
267-
Bundler::CLI::Install.new({ "no-cache" => true }).run
268-
correct_relative_remote_paths if @custom_lockfile.exist?
269-
return env
270-
end
266+
Bundler::CLI::Install.new({ "no-cache" => true }).run
267+
correct_relative_remote_paths if @custom_lockfile.exist?
268+
FileUtils.touch(@custom_dir + "needs_update") if should_update
269+
env
270+
end
271271

272+
#: (Hash[String, String]) -> Hash[String, String]
273+
def update(env)
272274
# Try to auto upgrade the gems we depend on, unless they are in the Gemfile as that would result in undesired
273275
# source control changes
274276
gems = ["ruby-lsp", "debug", "prism"].reject { |dep| @dependencies[dep] }
275277
gems << "ruby-lsp-rails" if @rails_app && !@dependencies["ruby-lsp-rails"]
276278

277279
Bundler::CLI::Update.new({ conservative: true }, gems).run
278280
correct_relative_remote_paths if @custom_lockfile.exist?
281+
File.delete(@custom_dir + "needs_update")
279282
@last_updated_path.write(Time.now.iso8601)
280283
env
281-
rescue Bundler::GemNotFound, Bundler::GitError
282-
# If a gem is not installed, skip the upgrade and try to install it with a single retry
283-
@retry ? env : run_bundle_install_directly(env, force_install: true)
284284
end
285285

286286
#: (Hash[String, String] env) -> Hash[String, String]

0 commit comments

Comments
 (0)