From 3503c94af501e38164613ef8347174a27346828a Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 18 Mar 2021 09:44:42 -0400 Subject: [PATCH] [ruby/irb] Avoid loading files' local variables [Bug #17623] https://github.com/ruby/irb/commit/b12f0cb8e2 --- lib/irb/workspace.rb | 2 ++ libexec/irb | 1 + test/irb/test_workspace.rb | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 78d434d106..8157e21644 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -57,6 +57,8 @@ EOF __FILE__, __LINE__ - 3) when 4 # binding is a copy of TOPLEVEL_BINDING (default) + # Note that this will typically be IRB::TOPLEVEL_BINDING (see exe/irb) + # This is to avoid RubyGems' local variables (see issue #17623) @binding = TOPLEVEL_BINDING.dup end end diff --git a/libexec/irb b/libexec/irb index c64ee85fbd..7cd24482da 100755 --- a/libexec/irb +++ b/libexec/irb @@ -8,4 +8,5 @@ require "irb" +IRB::TOPLEVEL_BINDING = binding IRB.start(__FILE__) diff --git a/test/irb/test_workspace.rb b/test/irb/test_workspace.rb index a39bea2050..61653486a1 100644 --- a/test/irb/test_workspace.rb +++ b/test/irb/test_workspace.rb @@ -80,6 +80,16 @@ module TestIRB assert_equal(nil, workspace.code_around_binding) end + + def test_toplevel_binding_local_variables + bug17623 = '[ruby-core:102468]' + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + status = assert_in_out_err(bundle_exec + ['-W0', '-e', <<~RUBY , '--', '-f', '--'], 'binding.local_variables', /\[:_\]/, [], bug17623) + version = 'xyz' # typical rubygems loading file + load('./exe/irb') + RUBY + end + private def with_script_lines