From 1b06142f50099827c208968258f08688a816467c Mon Sep 17 00:00:00 2001 From: KBS Date: Mon, 28 Sep 2026 21:00:05 +0900 Subject: [PATCH] Load a .rb file only once in irb_require irb_load returns the value of eval_input, which is nil, so the 'if irb_load(file_name)' branch never recorded the file and fell through to Kernel#require, which loaded it a second time. Call irb_load without the check, as the no-extension branch does. --- lib/irb/command/load.rb | 7 +++---- test/irb/test_command.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/irb/command/load.rb b/lib/irb/command/load.rb index 1efe23243..4792785c5 100644 --- a/lib/irb/command/load.rb +++ b/lib/irb/command/load.rb @@ -51,10 +51,9 @@ def execute_internal(file_name = nil) case file_name when /\.rb$/ begin - if irb_load(file_name) - $".push file_name - return true - end + irb_load(file_name) + $".push file_name + return true rescue LoadError end when /\.(so|o|sl)$/ diff --git a/test/irb/test_command.rb b/test/irb/test_command.rb index 1a99bc7eb..8fb20a22b 100644 --- a/test/irb/test_command.rb +++ b/test/irb/test_command.rb @@ -495,6 +495,21 @@ def test_irb_require_skips_file_already_loaded_by_require assert_match(/prefix_loaded/, out) end + def test_irb_require_loads_file_with_rb_extension_once + File.write("#{@tmpdir}/irb_require_rb_ext.rb", "$irb_require_rb_ext_count += 1\n") + + out, err = execute_lines( + "$LOAD_PATH.unshift '#{@tmpdir}'\n", + "$irb_require_rb_ext_count = 0\n", + "irb_require 'irb_require_rb_ext.rb'\n", + "irb_require 'irb_require_rb_ext'\n", + "$irb_require_rb_ext_count\n", + ) + + assert_empty(err) + assert_match(/=> 1\n\z/, out) + end + def test_irb_require_falls_back_to_kernel_require_when_not_in_load_path code = <<~RUBY p 'fallback_loaded'