Skip to content

Commit

Permalink
Fix chdir issue
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jan 29, 2025
1 parent 8aac6f1 commit 29cf4ef
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def file_updated?(change, path)

sig { void }
def run_gem_rbi_check
gem_rbi_check = RunGemRbiCheck.new
gem_rbi_check = RunGemRbiCheck.new(T.must(@global_state).workspace_path)
gem_rbi_check.run

T.must(@outgoing_queue) << Notification.window_log_message(
Expand Down
48 changes: 32 additions & 16 deletions lib/ruby_lsp/tapioca/run_gem_rbi_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ class RunGemRbiCheck
attr_reader :stderr
attr_reader :status

sig { void }
def initialize
sig { params(project_path: String).void }
def initialize(project_path)
@project_path = project_path
@stdout = T.let("", String)
@stderr = T.let("", String)
@status = T.let(nil, T.nilable(Process::Status))
end

sig { params(project_path: String).void }
def run(project_path = ".")
FileUtils.chdir(project_path) do
return log_message("Not a git repository") unless git_repo?
sig { void }
def run
return log_message("Not a git repository") unless git_repo?

lockfile_changed? ? generate_gem_rbis : cleanup_orphaned_rbis
end
lockfile_changed? ? generate_gem_rbis : cleanup_orphaned_rbis
end

private

attr_reader :project_path

sig { returns(T::Boolean) }
def git_repo?
_, status = Open3.capture2e("git rev-parse --is-inside-work-tree")
T.must(status.success?)
!!system("git rev-parse --is-inside-work-tree", chdir: project_path)
end

sig { returns(T::Boolean) }
Expand All @@ -45,7 +45,11 @@ def lockfile_changed?

sig { returns(String) }
def fetch_lockfile_diff
@lockfile_diff = File.exist?("Gemfile.lock") ? %x(git diff Gemfile.lock).strip : ""
@lockfile_diff = if File.exist?(File.join(project_path, "Gemfile.lock"))
execute_in_project_path("git", "diff", "Gemfile.lock").strip
else
""
end
end

sig { void }
Expand All @@ -72,6 +76,7 @@ def execute_tapioca_gem_command(gems)
"gem",
"--lsp_addon",
*gems,
chdir: project_path,
)

log_message(stdout) unless stdout.empty?
Expand All @@ -82,35 +87,46 @@ def execute_tapioca_gem_command(gems)

sig { params(gems: T::Array[String]).void }
def remove_rbis(gems)
FileUtils.rm_f(Dir.glob("sorbet/rbi/gems/{#{gems.join(",")}}@*.rbi"))
FileUtils.rm_f(Dir.glob("sorbet/rbi/gems/{#{gems.join(",")}}@*.rbi", base: project_path))
log_message("Removed RBIs for: #{gems.join(", ")}")
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 = execute_in_project_path(
"git",
"ls-files",
"--others",
"--exclude-standard",
"sorbet/rbi/gems/",
).lines.map(&:strip)
deleted_files = execute_in_project_path("git", "ls-files", "--deleted", "sorbet/rbi/gems/").lines.map(&:strip)

delete_files(untracked_files, "Deleted untracked RBIs")
restore_files(deleted_files, "Restored deleted RBIs")
end

sig { params(files: T::Array[String], message: String).void }
def delete_files(files, message)
files.each { |file| File.delete(file) }
FileUtils.rm(files.map { |file| File.join(project_path, file) })
log_message("#{message}: #{files.join(", ")}") unless files.empty?
end

sig { params(files: T::Array[String], message: String).void }
def restore_files(files, message)
files.each { |file| %x(git checkout -- #{file}) }
files.each { |file| execute_in_project_path("git", "checkout", "--", file) }
log_message("#{message}: #{files.join(", ")}") unless files.empty?
end

sig { params(message: String).void }
def log_message(message)
@stdout += "#{message}\n"
end

def execute_in_project_path(*parts)
stdout_and_stderr, _status = T.unsafe(Open3).capture2e(*parts, chdir: project_path)
stdout_and_stderr
end
end
end
end
30 changes: 15 additions & 15 deletions spec/tapioca/ruby_lsp/run_gem_rbi_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class RunGemRbiCheckSpec < SpecWithProject
@project.require_mock_gem(foo)

@project.bundle_install!
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
check.run(@project.absolute_path)
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new(@project.absolute_path)
check.run

assert check.stdout.include?("Not a git repository")
end
Expand Down Expand Up @@ -55,8 +55,8 @@ class RunGemRbiCheckSpec < SpecWithProject
@project.require_mock_gem(foo)
@project.bundle_install!

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

assert_project_file_exist("sorbet/rbi/gems/[email protected]")
end
Expand All @@ -68,16 +68,16 @@ class RunGemRbiCheckSpec < SpecWithProject
@project.require_mock_gem(foo)
@project.bundle_install!

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

assert_project_file_exist("sorbet/rbi/gems/[email protected]")

# Modify the gem
foo.update("0.0.2")
@project.bundle_install!

check.run(@project.absolute_path)
check.run

assert_project_file_exist("sorbet/rbi/gems/[email protected]")
end
Expand All @@ -89,15 +89,15 @@ class RunGemRbiCheckSpec < SpecWithProject
@project.require_mock_gem(foo)
@project.bundle_install!

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

assert_project_file_exist("sorbet/rbi/gems/[email protected]")

@project.exec("git restore Gemfile Gemfile.lock")

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

refute_project_file_exist("sorbet/rbi/gems/[email protected]")
end
Expand All @@ -110,8 +110,8 @@ class RunGemRbiCheckSpec < SpecWithProject

assert_project_file_exist("/sorbet/rbi/gems/[email protected]")

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

refute_project_file_exist("sorbet/rbi/gems/[email protected]")
end
Expand All @@ -127,8 +127,8 @@ class RunGemRbiCheckSpec < SpecWithProject

refute_project_file_exist("sorbet/rbi/gems/[email protected]")

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

assert_project_file_exist("sorbet/rbi/gems/[email protected]")

Expand Down

0 comments on commit 29cf4ef

Please sign in to comment.