From e19934f22c39f166128fb7e7b1c47edf70281fb6 Mon Sep 17 00:00:00 2001 From: Alex Rocha Date: Wed, 27 Nov 2024 11:38:04 -0800 Subject: [PATCH] Cleanup orphaned RBIs --- lib/ruby_lsp/tapioca/addon.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/ruby_lsp/tapioca/addon.rb b/lib/ruby_lsp/tapioca/addon.rb index eef5fa80b..0dfc15020 100644 --- a/lib/ruby_lsp/tapioca/addon.rb +++ b/lib/ruby_lsp/tapioca/addon.rb @@ -47,7 +47,9 @@ def activate(global_state, outgoing_queue) @outgoing_queue << Notification.window_log_message("Activating Tapioca add-on v#{version}") @rails_runner_client.register_server_addon(File.expand_path("server_addon.rb", __dir__)) - generate_gem_rbis if git_repo? && lockfile_changed? + if git_repo? + lockfile_changed? ? generate_gem_rbis : cleanup_orphaned_rbis + end rescue IncompatibleApiError # The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking # changes @@ -154,6 +156,30 @@ def generate_gem_rbis diff: T.must(@lockfile_diff), ) end + + sig { void } + def cleanup_orphaned_rbis + untracked_files = %x(git ls-files --others --exclude-standard sorbet/rbi/gems/).lines.map(&:strip) + deleted_files = %x(git ls-files --deleted sorbet/rbi/gems/).lines.map(&:strip) + + untracked_files.each do |file| + File.delete(file) + + T.must(@outgoing_queue) << Notification.window_log_message( + "Deleted untracked RBI: #{file}", + type: Constant::MessageType::INFO, + ) + end + + deleted_files.each do |file| + %x(git checkout -- #{file}) + + T.must(@outgoing_queue) << Notification.window_log_message( + "Restored deleted RBI: #{file}", + type: Constant::MessageType::INFO, + ) + end + end end end end