Skip to content

Commit

Permalink
Merge pull request #2050 from Shopify/emily/flipper-bug
Browse files Browse the repository at this point in the history
Properly attribute RBIs created by `ActiveSupport.on_load`
  • Loading branch information
KaanOzkan authored Oct 30, 2024
2 parents 954a09f + 0e6e73e commit 68bfb68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/tapioca/runtime/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,19 @@ def descendants_of(klass)
end

# Examines the call stack to identify the closest location where a "require" is performed
# by searching for the label "<top (required)>". If none is found, it returns the location
# by searching for the label "<top (required)>" or "block in <class:...>" in the
# case of an ActiveSupport.on_load hook. If none is found, it returns the location
# labeled "<main>", which is the original call site.
sig { params(locations: T.nilable(T::Array[Thread::Backtrace::Location])).returns(String) }
def resolve_loc(locations)
return "" unless locations

resolved_loc = locations.find { |loc| REQUIRED_FROM_LABELS.include?(loc.label) }
resolved_loc = locations.find do |loc|
label = loc.label
next unless label

REQUIRED_FROM_LABELS.include?(label) || label.start_with?("block in <class:")
end
return "" unless resolved_loc

resolved_loc.absolute_path || ""
Expand Down
17 changes: 17 additions & 0 deletions spec/tapioca/cli/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,23 @@ class Secret; end
assert_success_status(result)
end

it "must not include code from an ActiveSupport.on_load hook in ActiveRecord RBIs" do
@project.require_real_gem("rails", "7.1.0")
# Flipper adds functionality to ActiveRecord via an ActiveSupport.on_load hook
@project.require_real_gem("flipper-active_record", "1.3.1")

@project.bundle_install!
result = @project.tapioca("gem")

assert_empty_stderr(result)
assert_success_status(result)

activerecord_rbi_file = T.must(
Dir.glob("#{@project.absolute_path}/sorbet/rbi/gems/activerecord@7.1.0.rbi").first,
)
refute_includes(File.read(activerecord_rbi_file), "class Flipper")
end

it "must generate multiple gem RBIs" do
foo = mock_gem("foo", "0.0.1") do
write!("lib/foo.rb", FOO_RB)
Expand Down

0 comments on commit 68bfb68

Please sign in to comment.