Skip to content

Commit

Permalink
Improve orphaned gem RBI cleanup logic
Browse files Browse the repository at this point in the history
Previous implementation was not capturing all orphaned RBI files in some
 edge cases. By cleaning up orphaned gem RBIs before generating new ones
 we ensure that we are capturing all orphaned RBIs.

Co-authored-by: Vinicius Stock <vinistock@users.noreply.github.com>
  • Loading branch information
alexcrocha and vinistock committed Feb 3, 2025
1 parent 47e8b88 commit 301b4aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_lsp/tapioca/run_gem_rbi_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def initialize(project_path)
def run
return log_message("Not a git repository") unless git_repo?

cleanup_orphaned_rbis

if lockfile_changed?
generate_gem_rbis
else
cleanup_orphaned_rbis
end
end

Expand Down
49 changes: 47 additions & 2 deletions spec/tapioca/ruby_lsp/run_gem_rbi_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Tapioca
module RubyLsp
class RunGemRbiCheckSpec < SpecWithProject
FOO_RB = "module Foo; end"
BAR_RB = "module Bar; end"

before(:all) do
@project = mock_project
Expand All @@ -34,6 +35,7 @@ class RunGemRbiCheckSpec < SpecWithProject

describe "with git" do
before do
FileUtils.mkdir_p("#{@project.absolute_path}/sorbet/rbi/gems")
@project.write!("Gemfile", @project.tapioca_gemfile)
@project.bundle_install!
@project.exec("git init")
Expand Down Expand Up @@ -104,7 +106,6 @@ class RunGemRbiCheckSpec < SpecWithProject

it "deletes untracked RBI files" do
@project.bundle_install!
FileUtils.mkdir_p("#{@project.absolute_path}/sorbet/rbi/gems")
# Create an untracked RBI file
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/bar@0.0.1.rbi")

Expand All @@ -118,7 +119,6 @@ class RunGemRbiCheckSpec < SpecWithProject

it "restores deleted RBI files" do
@project.bundle_install!
FileUtils.mkdir_p("#{@project.absolute_path}/sorbet/rbi/gems")
# Create and delete a tracked RBI file
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/foo@0.0.1.rbi")
@project.exec("git add sorbet/rbi/gems/foo@0.0.1.rbi")
Expand All @@ -135,6 +135,51 @@ class RunGemRbiCheckSpec < SpecWithProject
# Clean-up commit
@project.exec("git reset --hard HEAD^")
end

it "cleans up orphaned RBIs when gems are not present in the lockfile diff" do
foo = mock_gem("foo", "0.0.1") do
write!("lib/foo.rb", FOO_RB)
end
bar = mock_gem("bar", "0.0.1") do
write!("lib/bar.rb", BAR_RB)
end
@project.require_mock_gem(foo)
@project.require_mock_gem(bar)
@project.bundle_install!

FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/foo@0.0.1.rbi")
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/bar@0.0.1.rbi")

assert_project_file_exist("sorbet/rbi/gems/foo@0.0.1.rbi")
assert_project_file_exist("sorbet/rbi/gems/bar@0.0.1.rbi")

@project.exec("git add *")
@project.exec("git commit -m 'Add foo and bar RBI'")

# Update both gems

foo.update("0.0.2")
bar.update("0.0.2")
@project.bundle_install!

::RubyLsp::Tapioca::RunGemRbiCheck.new(@project.absolute_path).run

refute_project_file_exist("sorbet/rbi/gems/foo@0.0.1.rbi")
refute_project_file_exist("sorbet/rbi/gems/bar@0.0.1.rbi")

assert_project_file_exist("sorbet/rbi/gems/foo@0.0.2.rbi")
assert_project_file_exist("sorbet/rbi/gems/bar@0.0.2.rbi")

# Downgrade foo gem back to 0.0.1

foo.update("0.0.1")
@project.bundle_install!

::RubyLsp::Tapioca::RunGemRbiCheck.new(@project.absolute_path).run

assert_project_file_exist("sorbet/rbi/gems/foo@0.0.1.rbi")
refute_project_file_exist("sorbet/rbi/gems/foo@0.0.2.rbi")
end
end
end
end
Expand Down

0 comments on commit 301b4aa

Please sign in to comment.