Skip to content
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

Make Rails engine path check more specific #1925

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tapioca/static/symbol_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def payload_symbols
sig { params(gem: Gemfile::GemSpec).returns(T::Set[String]) }
def engine_symbols(gem)
gem_engine = engines.find do |engine|
gem.contains_path?(engine.config.root.to_s)
gem.full_gem_path == engine.config.root.to_s
end

return Set.new unless gem_engine
Expand Down
57 changes: 57 additions & 0 deletions spec/tapioca/cli/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,63 @@ class Application < Rails::Application
assert_empty_stderr(result)
assert_success_status(result)
end

it "properly filters eager loaded Rails engines when gem is installed from git source" do
# When Rails is installed through a release version, each gem contained inside `rails` is placed in a
# different sibling directory. However, when Rails is installed through a git source, all gems are placed
# nested under the Rails directory. When we check if an engine belongs to a gem, we need to take that into
# account to avoid placing symbols that do not belong to the Rails RBI inside of it
#
# Example of path for git installed Rails:
# /Users/me/.gem/ruby/3.3.0/bundler/gems/rails-e3ea4c74124f/
# /Users/me/.gem/ruby/3.3.0/bundler/gems/rails-e3ea4c74124f/activestorage
# /Users/me/.gem/ruby/3.3.0/bundler/gems/rails-e3ea4c74124f/actionmailbox
#
# The engines defined by `activestorage` and `actionmailbox` should not be placed in the Rails RBI. They
# belong in their own respective RBIs.
#
# Note that this problem only happens if another gem somehow eager loads the engines. By default, Rails
# would've not loaded those classes and they would have not been placed in the Rails RBI.

@project.write_gemfile!(<<~GEMFILE, append: true)
gem("rails", git: "https://github.com/rails/rails", branch: "main")
GEMFILE

# Create a gem that eager loads the ActionMailbox engine
gem = mock_gem("eager_loader", "1.0.0") do
write!("lib/eager_loader.rb", <<~RUBY)
require "action_mailbox/engine"

module EagerLoader
end
RUBY
end
@project.require_mock_gem(gem)

install_result = @project.bundle_install!
assert_predicate(install_result, :status, "Bundle install failed\n\n#{install_result.err}")

result = @project.tapioca("gem rails")
assert_empty_stderr(result)
assert_success_status(result)

assert_stdout_includes(result, "Compiled rails")
rails_rbi = T.must(Dir.glob("#{@project.absolute_path}/sorbet/rbi/gems/rails@*.rbi").first)

expected_rbi = <<~RBI
# typed: true

# DO NOT EDIT MANUALLY
# This is an autogenerated file for types exported from the `rails` gem.
# Please instead update this file by running `bin/tapioca gem rails`.


# THIS IS AN EMPTY RBI FILE.
# see https://github.com/Shopify/tapioca#manually-requiring-parts-of-a-gem
RBI

assert_equal(expected_rbi, File.read(rails_rbi))
end
end

describe "verify" do
Expand Down
Loading