Skip to content

Commit af3c5bd

Browse files
committed
Fallback to Backtrace#path if absolute_path is nil
1 parent 8256efe commit af3c5bd

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/tapioca/runtime/reflection.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,17 @@ def resolve_loc(locations)
210210
end
211211
return unless resolved_loc
212212

213+
resolved_loc_path = resolved_loc.absolute_path || resolved_loc.path
214+
213215
# Find the location of the last frame in this file to get the most accurate line number.
214-
resolved_loc = locations.find { |loc| loc.absolute_path == resolved_loc.absolute_path }
216+
resolved_loc = locations.find { |loc| loc.absolute_path == resolved_loc_path }
215217
return unless resolved_loc
216218

217219
# If the last operation was a `require`, and we have no more frames,
218220
# we are probably dealing with a C-method.
219221
return if locations.first&.label == "require"
220222

221-
file = resolved_loc.absolute_path || ""
223+
file = resolved_loc.absolute_path || resolved_loc.path || ""
222224

223225
SourceLocation.from_loc([file, resolved_loc.lineno])
224226
end

spec/tapioca/gem/pipeline_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,6 +4112,50 @@ module Container::FooModule; end
41124112
assert_equal(output, compile)
41134113
end
41144114

4115+
it "handles class_eval created methods" do
4116+
add_ruby_file("container.rb", <<~'RUBY')
4117+
class Foo
4118+
class_eval <<~EOF
4119+
def foo; end
4120+
def bar; end
4121+
EOF
4122+
4123+
class_eval <<~EOF, __FILE__, __LINE__ + 1
4124+
def baz; end
4125+
def qux; end
4126+
EOF
4127+
4128+
# Somehow defining methods in a loop triggers a different behavior
4129+
# in backtrace locations where the absolute path ends up being `nil`.
4130+
%w[string integer float boolean date datetime decimal money].each do |attr_type|
4131+
class_eval <<-EOV, __FILE__, __LINE__ + 1
4132+
def #{attr_type}
4133+
end
4134+
EOV
4135+
end
4136+
end
4137+
RUBY
4138+
4139+
output = template(<<~RBI)
4140+
class Foo
4141+
def bar; end
4142+
def baz; end
4143+
def boolean; end
4144+
def date; end
4145+
def datetime; end
4146+
def decimal; end
4147+
def float; end
4148+
def foo; end
4149+
def integer; end
4150+
def money; end
4151+
def qux; end
4152+
def string; end
4153+
end
4154+
RBI
4155+
4156+
assert_equal(output, compile)
4157+
end
4158+
41154159
it "includes comment documentation from sources when doc is true" do
41164160
add_ruby_file("foo.rb", <<~RUBY)
41174161
# frozen_string_literal: true

0 commit comments

Comments
 (0)