Skip to content

Commit

Permalink
Make SourceFinder ignore binary sources (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored Jan 8, 2024
1 parent 452b543 commit 73b35bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def find_source(signature, super_level = 0)
return unless receiver.respond_to?(method, true)
file, line = method_target(receiver, super_level, method, "receiver")
end
if file && line && File.exist?(file)
# If the line is zero, it means that the target's source is probably in a binary file, which we should ignore.
if file && line && !line.zero? && File.exist?(file)
Source.new(file: file, first_line: line, last_line: find_end(file, line))
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/irb/cmd/test_show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,24 @@ class Bar

assert_match(%r[#{@ruby_file.to_path}:5\s+class Bar\r\n end], out)
end

def test_show_source_ignores_binary_source_file
write_ruby <<~RUBY
# io-console is an indirect dependency of irb
require "io/console"
binding.irb
RUBY

out = run_ruby_file do
# IO::ConsoleMode is defined in io-console gem's C extension
type "show_source IO::ConsoleMode"
type "exit"
end

# A safeguard to make sure the test subject is actually defined
refute_match(/NameError/, out)
assert_match(%r[Error: Couldn't locate a definition for IO::ConsoleMode], out)
end
end
end

0 comments on commit 73b35bb

Please sign in to comment.