-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Tapioca Addon] Support gem RBI generation #2081
Open
alexcrocha
wants to merge
11
commits into
main
Choose a base branch
from
ar/gem-regeneration-git-status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+416
−2
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9e28e99
Create lockfile diff parser
alexcrocha 0e3e113
Add test for lockfile diff parser
andyw8 47d4943
Add Tapioca Addon gem RBI generation support
alexcrocha 7541f1d
Cleanup orphaned RBIs
alexcrocha 32306d7
Refactor how we detect git repo
alexcrocha ded1900
Extract gem rbi check logic into its own class
andyw8 f006568
Fix logging to work with addon notifications
alexcrocha 68fb331
Refactor RunGemRbiCheck for readability
alexcrocha 3dd5d4b
Prevent RBI deletion for gems that are still direct dependencies
alexcrocha 6b9f24f
wip test fix against CI
alexcrocha 068808a
Ensure tapioca gem command executes in a clean env
alexcrocha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# typed: true | ||
andyw8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# frozen_string_literal: true | ||
|
||
require "bundler" | ||
|
||
module RubyLsp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work on this! |
||
module Tapioca | ||
class LockfileDiffParser | ||
GEM_NAME_PATTERN = /[\w\-]+/ | ||
DIFF_LINE_PATTERN = /[+-](.*#{GEM_NAME_PATTERN})\s*\(/ | ||
ADDED_LINE_PATTERN = /^\+.*#{GEM_NAME_PATTERN} \(.*\)/ | ||
REMOVED_LINE_PATTERN = /^-.*#{GEM_NAME_PATTERN} \(.*\)/ | ||
|
||
attr_reader :added_or_modified_gems | ||
attr_reader :removed_gems | ||
|
||
def initialize(diff_content, direct_dependencies: nil) | ||
@diff_content = diff_content.lines | ||
@current_dependencies = direct_dependencies || | ||
Bundler::LockfileParser.new(Bundler.default_lockfile.read).dependencies.keys | ||
@added_or_modified_gems = parse_added_or_modified_gems | ||
@removed_gems = parse_removed_gems | ||
end | ||
|
||
private | ||
|
||
def parse_added_or_modified_gems | ||
@diff_content | ||
.filter_map { |line| extract_gem(line) if line.match?(ADDED_LINE_PATTERN) } | ||
.uniq | ||
end | ||
|
||
def parse_removed_gems | ||
@diff_content.filter_map do |line| | ||
next unless line.match?(REMOVED_LINE_PATTERN) | ||
|
||
gem = extract_gem(line) | ||
next if @current_dependencies.include?(gem) | ||
|
||
gem | ||
end.uniq | ||
end | ||
|
||
def extract_gem(line) | ||
line.match(DIFF_LINE_PATTERN)[1].strip | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "ruby_lsp/tapioca/lockfile_diff_parser" | ||
|
||
module RubyLsp | ||
module Tapioca | ||
class GemRbiCheckResult < T::Struct | ||
prop :stdout, String | ||
prop :stderr, String | ||
prop :status, T.nilable(Process::Status) | ||
end | ||
|
||
class RunGemRbiCheck | ||
extend T::Sig | ||
|
||
sig { void } | ||
def initialize | ||
@result = T.let( | ||
GemRbiCheckResult.new(stdout: "", stderr: "", status: nil), | ||
GemRbiCheckResult, | ||
) | ||
end | ||
|
||
attr_reader :result | ||
|
||
sig { params(project_path: String).returns(GemRbiCheckResult) } | ||
def run(project_path = ".") | ||
FileUtils.chdir(project_path) do | ||
if git_repo? | ||
lockfile_changed? ? generate_gem_rbis : cleanup_orphaned_rbis | ||
else | ||
log_message("Not a git repository") | ||
end | ||
end | ||
|
||
@result | ||
end | ||
|
||
private | ||
|
||
sig { returns(T::Boolean) } | ||
def git_repo? | ||
require "open3" | ||
|
||
_, status = Open3.capture2e("git rev-parse --is-inside-work-tree") | ||
T.must(status.success?) | ||
end | ||
|
||
sig { returns(T::Boolean) } | ||
def lockfile_changed? | ||
fetch_lockfile_diff | ||
!@lockfile_diff.empty? | ||
end | ||
|
||
sig { returns(String) } | ||
def fetch_lockfile_diff | ||
@lockfile_diff = File.exist?("Gemfile.lock") ? %x(git diff Gemfile.lock).strip : "" | ||
end | ||
|
||
sig { void } | ||
def generate_gem_rbis | ||
parser = Tapioca::LockfileDiffParser.new(@lockfile_diff) | ||
removed_gems = parser.removed_gems | ||
added_or_modified_gems = parser.added_or_modified_gems | ||
|
||
if added_or_modified_gems.any? | ||
log_message("Identified lockfile changes, attempting to generate gem RBIs...") | ||
execute_tapioca_gem_command(added_or_modified_gems) | ||
elsif removed_gems.any? | ||
remove_rbis(removed_gems) | ||
end | ||
end | ||
|
||
sig { params(gems: T::Array[String]).void } | ||
def execute_tapioca_gem_command(gems) | ||
Bundler.with_unbundled_env do | ||
# Resetting BUNDLE_GEMFILE to root folder to use the project's Gemfile instead of Ruby LSP's composed Gemfile | ||
stdout, stderr, status = T.unsafe(Open3).capture3( | ||
{ "BUNDLE_GEMFILE" => "Gemfile" }, | ||
"bundle", | ||
"exec", | ||
"tapioca", | ||
"gem", | ||
"--lsp_addon", | ||
*gems, | ||
) | ||
|
||
log_message(stdout) unless stdout.empty? | ||
log_message(stderr) unless stderr.empty? | ||
@result.status = status | ||
end | ||
end | ||
|
||
sig { params(gems: T::Array[String]).void } | ||
def remove_rbis(gems) | ||
FileUtils.rm_f(Dir.glob("sorbet/rbi/gems/{#{gems.join(",")}}@*.rbi")) | ||
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) | ||
|
||
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) } | ||
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}) } | ||
log_message("#{message}: #{files.join(", ")}") unless files.empty? | ||
end | ||
|
||
sig { params(message: String).void } | ||
def log_message(message) | ||
@result.stdout += "#{message}\n" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require "ruby_lsp/tapioca/run_gem_rbi_check" | ||
|
||
module Tapioca | ||
module RubyLsp | ||
class RunGemRbiCheckSpec < SpecWithProject | ||
FOO_RB = <<~RUBY | ||
module Foo | ||
end | ||
RUBY | ||
|
||
before do | ||
@project = mock_project | ||
end | ||
|
||
after do | ||
@project.destroy! | ||
end | ||
|
||
describe "without git" do | ||
before do | ||
@project.bundle_install! | ||
@project.tapioca("configure") | ||
end | ||
|
||
it "does nothing if there is no git repo" do | ||
foo = mock_gem("foo", "0.0.1") do | ||
write!("lib/foo.rb", FOO_RB) | ||
end | ||
@project.require_mock_gem(foo) | ||
|
||
@project.bundle_install! | ||
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new | ||
check.run(@project.absolute_path) | ||
|
||
assert check.result.stdout.include?("Not a git repository") | ||
end | ||
end | ||
|
||
describe "with git" do | ||
before do | ||
@project.bundle_install! | ||
@project.tapioca("configure") | ||
@project.exec("git init") | ||
@project.exec("git config commit.gpgsign false") | ||
@project.exec("git add .") | ||
@project.exec("git commit -m 'Initial commit'") | ||
end | ||
|
||
it "creates the RBI for a newly added gem" do | ||
foo = mock_gem("foo", "0.0.1") do | ||
write!("lib/foo.rb", FOO_RB) | ||
end | ||
@project.require_mock_gem(foo) | ||
@project.bundle_install! | ||
|
||
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new | ||
check.run(@project.absolute_path) | ||
|
||
assert_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
end | ||
|
||
it "regenerates RBI when a gem version changes" do | ||
foo = mock_gem("foo", "0.0.1") do | ||
write!("lib/foo.rb", FOO_RB) | ||
end | ||
@project.require_mock_gem(foo) | ||
@project.bundle_install! | ||
|
||
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new | ||
check.run(@project.absolute_path) | ||
|
||
assert_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
|
||
# Modify the gem | ||
update_gem foo, "0.0.2" | ||
@project.bundle_install! | ||
|
||
check.run(@project.absolute_path) | ||
|
||
assert_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
end | ||
|
||
it "removes RBI file when a gem is removed" do | ||
foo = mock_gem("foo", "0.0.1") do | ||
write!("lib/foo.rb", FOO_RB) | ||
end | ||
@project.require_mock_gem(foo) | ||
@project.bundle_install! | ||
|
||
check1 = ::RubyLsp::Tapioca::RunGemRbiCheck.new | ||
check1.run(@project.absolute_path) | ||
|
||
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) | ||
|
||
refute_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
end | ||
|
||
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/[email protected]") | ||
|
||
assert_project_file_exist("/sorbet/rbi/gems/[email protected]") | ||
|
||
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new | ||
check.run(@project.absolute_path) | ||
|
||
refute_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
end | ||
|
||
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/[email protected]") | ||
@project.exec("git add sorbet/rbi/gems/[email protected]") | ||
@project.exec("git commit -m 'Add foo RBI'") | ||
FileUtils.rm("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]") | ||
|
||
refute_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
|
||
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new | ||
check.run(@project.absolute_path) | ||
|
||
assert_project_file_exist("sorbet/rbi/gems/[email protected]") | ||
|
||
# Clean-up commit | ||
@project.exec("git reset --hard HEAD^") | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I noticed is we keep generating RBIs for updated gems. An ideal solution would be like
GemSync
where we determine that gem RBI is up to date and not generate. It may not be needed yet assuming restarts are rare and folks will stage their lockfile changes. Any thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, it's fine to move forward as is, but I also think it's worth making an attempt at not regenerating if it's not needed.
Maybe we can extract part of the logic from gem sync and check if there's any work to be done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good to me. I mentioned this in the issue as a future improvement.