Skip to content

Respect parent command line arguments in nested IRB scopes - #848

Closed
nunosilva800 wants to merge 3 commits into
ruby:masterfrom
nunosilva800:625-irbrc-file-loaded-always
Closed

nunosilva800 wants to merge 3 commits into
ruby:masterfrom
nunosilva800:625-irbrc-file-loaded-always

Conversation

@nunosilva800

@nunosilva800 nunosilva800 commented Jan 31, 2024 •

Copy link
Copy Markdown
Contributor

When binding.irb is called, the new session is setup without command line arguments:

irb/lib/irb.rb

Lines 1516 to 1517 in a641746

# Setup IRB with the current file's path and no command line arguments
IRB.setup(source_location[0], argv: [])

This means that the child session does not respect the parent command line arguments.
Therefore when using $ irb -f (to ignore RC file), and opening a child session with biding.irb, the new session will load the RC file (if it exists).

See more in:

The solution I present here is to hold a copy of ARGV in @CONF, and use that to setup child sessions.

Closes #625

Comment thread lib/irb/init.rb
# option analyzing
def IRB.parse_opts(argv: ::ARGV)
def IRB.parse_opts
argv = @CONF[:ARGV].dup || []

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problems in tests:

  1. assert_equal(['-f'], argv)
  2. assert_equal([], argv)

Both of these assert that the ARGV array passed as argument is mutated. Is this really a behaviour that is important? Isn't it better to assert that the IRB.conf[...] values are properly set?

@nunosilva800
nunosilva800 force-pushed the 625-irbrc-file-loaded-always branch from d1e2203 to d735dac Compare January 31, 2024 12:57
Comment thread lib/irb.rb
Comment on lines +1516 to +1517
# Setup IRB with the current file's path and current session command line arguments
IRB.setup(source_location[0], argv: IRB.conf[:ARGV])

@nunosilva800 nunosilva800 Jan 31, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really know how to test this, other than manually steps:

$ mkdir -p tmp
$ echo "puts 'irbrc file loaded'" > tmp/.irbrc 
$ bundle exec rake install
$ cd tmp && echo "binding.irb" | irb -f && cd ..

Before:

Switch to inspect mode.
binding.irb
irbrc file loaded
Switch to inspect mode.

nil

After:

Switch to inspect mode.
binding.irb
Switch to inspect mode.

nil

I thought about doing this test:

    def test_new_session_inherits_parent_config
      write_rc <<~RUBY
        puts 'irbrc file loaded'
      RUBY

      write_ruby <<~'RUBY'
        require 'irb'
        IRB.start
      RUBY

      output = run_ruby_file(["-f"]) do
        type "binding.irb"
        type "exit!"
        type "exit!"
      end

      refute output.include?("irbrc file loaded")
    end

And change the helper method ruby_ruby_file to:

    def run_ruby_file(cmd_args=[], &block)
      cmd = [EnvUtil.rubybin, "-I", LIB, @ruby_file.to_path] + cmd_args

But the test times-out. I think it's because safe_gets or writing to the command in PTY.spawn is not working this way :/

@nunosilva800

Copy link
Copy Markdown
Contributor Author

Closing in favour of

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IRBRC file is loaded every time ruby reaches binding.irb

1 participant