Skip to content

Commit 8f4eda5

Browse files
st0012matzbot
authored andcommitted
[ruby/irb] Make SourceFinder ignore binary sources
(ruby/irb#836) ruby/irb@73b35bb7f4
1 parent 41e2d18 commit 8f4eda5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/irb/source_finder.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def find_source(signature, super_level = 0)
3434
return unless receiver.respond_to?(method, true)
3535
file, line = method_target(receiver, super_level, method, "receiver")
3636
end
37-
if file && line && File.exist?(file)
37+
# If the line is zero, it means that the target's source is probably in a binary file, which we should ignore.
38+
if file && line && !line.zero? && File.exist?(file)
3839
Source.new(file: file, first_line: line, last_line: find_end(file, line))
3940
end
4041
end

test/irb/cmd/test_show_source.rb

+19
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,24 @@ class Bar
300300

301301
assert_match(%r[#{@ruby_file.to_path}:5\s+class Bar\r\n end], out)
302302
end
303+
304+
def test_show_source_ignores_binary_source_file
305+
write_ruby <<~RUBY
306+
# io-console is an indirect dependency of irb
307+
require "io/console"
308+
309+
binding.irb
310+
RUBY
311+
312+
out = run_ruby_file do
313+
# IO::ConsoleMode is defined in io-console gem's C extension
314+
type "show_source IO::ConsoleMode"
315+
type "exit"
316+
end
317+
318+
# A safeguard to make sure the test subject is actually defined
319+
refute_match(/NameError/, out)
320+
assert_match(%r[Error: Couldn't locate a definition for IO::ConsoleMode], out)
321+
end
303322
end
304323
end

0 commit comments

Comments
 (0)