From 37ffdc6b1997c8e13515534df28026a53a8a9fb3 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 18 Dec 2023 14:36:55 +0000 Subject: [PATCH] Warn users about errors in loading RC files (#817) 1. Because `IRB.rc_file` always generates an rc file name, even if the file doesn't exist, we should check the file exists before trying to load it. 2. If any type of errors occur while loading the rc file, we should warn the user about it. --- lib/irb/init.rb | 18 ++++++++---------- test/irb/test_init.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/irb/init.rb b/lib/irb/init.rb index d9676fd9a..66e7b6146 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -389,18 +389,16 @@ def IRB.parse_opts(argv: ::ARGV) $LOAD_PATH.unshift(*load_path) end - # running config + # Run the config file def IRB.run_config if @CONF[:RC] begin - load rc_file - rescue LoadError, Errno::ENOENT - rescue # StandardError, ScriptError - print "load error: #{rc_file}\n" - print $!.class, ": ", $!, "\n" - for err in $@[0, $@.size - 2] - print "\t", err, "\n" - end + file = rc_file + # Because rc_file always returns `HOME/.irbrc` even if no rc file is present, we can't warn users about missing rc files. + # Otherwise, it'd be very noisy. + load file if File.exist?(file) + rescue StandardError, ScriptError => e + warn "Error loading RC file '#{file}':\n#{e.full_message(highlight: false)}" end end end @@ -418,7 +416,7 @@ def IRB.rc_file(ext = IRBRC_EXT) end case rc_file = @CONF[:RC_NAME_GENERATOR].call(ext) when String - return rc_file + rc_file else fail IllegalRCNameGenerator end diff --git a/test/irb/test_init.rb b/test/irb/test_init.rb index b6a8f5529..7d11b5b50 100644 --- a/test/irb/test_init.rb +++ b/test/irb/test_init.rb @@ -218,4 +218,44 @@ def with_argv(argv) ARGV.replace(orig) end end + + class InitIntegrationTest < IntegrationTestCase + def test_load_error_in_rc_file_is_warned + write_rc <<~'IRBRC' + require "file_that_does_not_exist" + IRBRC + + write_ruby <<~'RUBY' + binding.irb + RUBY + + output = run_ruby_file do + type "'foobar'" + type "exit" + end + + # IRB session should still be started + assert_includes output, "foobar" + assert_includes output, 'cannot load such file -- file_that_does_not_exist (LoadError)' + end + + def test_normal_errors_in_rc_file_is_warned + write_rc <<~'IRBRC' + raise "I'm an error" + IRBRC + + write_ruby <<~'RUBY' + binding.irb + RUBY + + output = run_ruby_file do + type "'foobar'" + type "exit" + end + + # IRB session should still be started + assert_includes output, "foobar" + assert_includes output, 'I\'m an error (RuntimeError)' + end + end end