Skip to content

Commit 1b8d4a3

Browse files
committed
Constants defined in C extensions might be attributed other the gems
Some constants like `ERB::Escape` that are defind in C extension components of gems are currently failing the `defined_in_gem?` look up. This happens for `ERB::Escape` because, instead of `erb/lib/erb/utils.rb` loading the shared-object for `escape`, we have `erubi` loading it. Thus, when we do the backtrace scan to find the file that is loading the constant we attribute it to the `erubi` gem. This is because there are no frames in the backtrace for the class definition coming from a C extension, thus we miss the actual file that does the `ERB::Escape` definition. Since we can look up the source location of constants now, it makes more sense to include that in the list of file candidate locations. Which makes it resilient against these kinds of mistakes.
1 parent 22141ae commit 1b8d4a3

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

lib/tapioca/runtime/reflection.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,16 @@ def resolve_loc(locations)
213213

214214
#: (T::Module[top] constant) -> Set[String]
215215
def file_candidates_for(constant)
216-
relevant_methods_for(constant).filter_map do |method|
216+
# Grab all source files for (relevant) methods defined on the constant
217+
candidates = relevant_methods_for(constant).filter_map do |method|
217218
method.source_location&.first
218219
end.to_set
220+
221+
# Add the source file for the constant definition itself, if available.
222+
source_location_candidate = const_source_location(name_of(constant).to_s)&.file
223+
candidates.add(source_location_candidate) if source_location_candidate
224+
225+
candidates
219226
end
220227

221228
#: (T::Module[top] constant) -> untyped

0 commit comments

Comments
 (0)