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

show_source can't find some sources defined in the session #974

Open
amomchilov opened this issue Jun 17, 2024 · 1 comment
Open

show_source can't find some sources defined in the session #974

amomchilov opened this issue Jun 17, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@amomchilov
Copy link

amomchilov commented Jun 17, 2024

Description

irb can find the source of a method defined during the repl session, but not its parent class.

irb(main):001* class Foo
irb(main):002*   def x = "Hello, world!"
irb(main):003> end
=> :x
irb(main):004> Foo.instance_method(:x).source_location
=> ["(irb)", 2]
irb(main):005> show_source Foo#x # Works ✅

From: (irb):2

def x = "Hello, world!"

=> nil
irb(main):006> show_source Foo # Not found ❌
Error: Couldn't locate a definition for Foo
=> nil

A similar thing happens if you execute a script from standard input

$ ruby
class Foo
  def x = "Hello, world!"
end

binding.irb
< I press ctrl-d to send EOF >
irb(main):001> Foo.instance_method(:x).source_location
=> ["-", 2]
irb(main):002> show_source Foo#x # Not found ❌
Error: Couldn't locate a definition for Foo#x # Not found ❌
=> nil
irb(main):003> show_source Foo # Not found ❌
Error: Couldn't locate a definition for Foo # Not found ❌
=> nil

Notes

This does work if running from a saved script:

# example_script.rb
class Foo
  def x = "Hello, world!"
end

binding.irb
ruby ./example_script.rb

From: ./example_script.rb @ line 5 :

    1: class Foo
    2:   def x = "Hello, world!"
    3: end
    4:
 => 5: binding.irb

irb(main):001> show_source Foo

From: ./example_script.rb:1

class Foo
  def x = "Hello, world!"
end

nil

Result of irb_info

irb(main):001> irb_info
Ruby version: 3.3.0
IRB version: irb 1.13.1 (2024-05-05)
InputMethod: RelineInputMethod with Reline 0.5.8
Completion: Autocomplete, RegexpCompletor
RUBY_PLATFORM: x86_64-linux
LANG env: en_US.UTF-8
LC_ALL env: en_US.UTF-8
East Asian Ambiguous Width: 1

## Terminal Emulator

What's your terminal emulator? **iTerm 2.**

## Setting Files

Are you using `~/.irbrc` and `~/.inputrc`? **Nope.**
@st0012 st0012 added the bug Something isn't working label Jun 18, 2024
@tompng
Copy link
Member

tompng commented Jun 23, 2024

If the source file exist, source is available.
Even if the file does not exist, IRB can show method source defined in IRB because RubyVM::AbstractSyntaxTree.keep_script_lines is enabled in IRB session.

if you execute a script from standard input

It is impossible now. keep_script_lines can't be enabled before evaluating script from standard input.

show_source Foo # Not found

Constant source is not available from AST.
We need to add an identifier to the source file '(irb)' like '(irb[session_id])' and store all input lines, which I avoid and use keep_script_lines = true instead in #862.

$ ruby -e "loop{binding.irb}"
irb(main):001> A=1 # defined in line 1
irb(main):002> exit
irb(main):001> B=1 # also defined in line 1. source file string should be different to distinguish B from A
irb(main):002> Object.const_source_location(:A) == Object.const_source_location(:B)
#=> true # should be false to implement constant source defined in irb session

@tompng tompng added enhancement New feature or request and removed bug Something isn't working labels Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

3 participants