Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SourceFinder ignore binary sources #836

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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