From f13e3b8e2ab071aa5b4c5b11c40d5cca2bde9c95 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Mon, 25 Dec 2023 14:23:47 +0100 Subject: [PATCH] Remove .irbrc file on rubies >= 3.3 It is no longer necessary, since the enabled extensions have been made defaults in ruby core. So ruby-3.3 uses the irb_predefiner to cleanup a previously generated .irbrc from the users home directory, so that the error output due to changed require paths is avoided. Previously the outdated .irbrc led to an error like so: $ irb Error loading RC file 'C:/Users/Administrator/.irbrc': :127:in `require': cannot load such file -- irb/ext/save-history (LoadError) Did you mean? irb/ext/eval_history from :127:in `require' from C:/Users/Administrator/.irbrc:1:in `' from C:/Ruby33-x64/lib/ruby/3.3.0/irb/init.rb:399:in `load' --- recipes/sandbox/20-extend-irb.rake | 4 ++++ resources/files/irbrc_predefiner.rb | 32 +++++++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/recipes/sandbox/20-extend-irb.rake b/recipes/sandbox/20-extend-irb.rake index 0a6b13e66..1255e4bba 100644 --- a/recipes/sandbox/20-extend-irb.rake +++ b/recipes/sandbox/20-extend-irb.rake @@ -1,3 +1,7 @@ +# This adds some logic to define a .irbrc to enable history saving and completion. +# These features are enabled by default since ruby-3.3 and require paths have been changed. +# Therefore the job on later versions is inverted to remove the .irbrc when unchanged. + self.import_files.merge!({ "resources/files/irbrc_predefiner.rb" => "lib/ruby/site_ruby/#{package.rubylibver}/irbrc_predefiner.rb", }) diff --git a/resources/files/irbrc_predefiner.rb b/resources/files/irbrc_predefiner.rb index 3c8bc3c5b..6f603b801 100644 --- a/resources/files/irbrc_predefiner.rb +++ b/resources/files/irbrc_predefiner.rb @@ -2,20 +2,30 @@ irbrc_file = IRB.enum_for(:rc_file_generators).first.call(IRB::IRBRC_EXT) -if irbrc_file && !File.exist?(irbrc_file) && File.exist?(File.dirname(irbrc_file)) - File.write irbrc_file, <<-EOT +irbrc_text = <<-EOT require 'irb/ext/save-history' require 'irb/completion' IRB.conf[:SAVE_HISTORY] = 200 - EOT -end +EOT + +if RUBY_VERSION < "3.3" + # Create a .irbrc file with default options on older rubies + if irbrc_file && !File.exist?(irbrc_file) && File.exist?(File.dirname(irbrc_file)) + File.write irbrc_file, irbrc_text + end -# Try to convert .irb_history from locale to UTF-8, if it isn't encoded properly. -# This is for transition from CP* encodings of RbReadline to UTF-8 of Reline. -history_file = IRB.rc_file("_history") -if File.exist?(history_file) && !(hist=File.read(history_file, encoding: 'utf-8')).valid_encoding? - hist = hist.encode('utf-8', Encoding.find("locale")) - if hist.valid_encoding? - File.write(history_file, hist) + # Try to convert .irb_history from locale to UTF-8, if it isn't encoded properly. + # This is for transition from CP* encodings of RbReadline to UTF-8 of Reline. + history_file = IRB.rc_file("_history") + if File.exist?(history_file) && !(hist=File.read(history_file, encoding: 'utf-8')).valid_encoding? + hist = hist.encode('utf-8', Encoding.find("locale")) + if hist.valid_encoding? + File.write(history_file, hist) + end + end +else + # Remove the now unnecessary .irbrc file when unchanged on newer rubies + if irbrc_file && File.exist?(irbrc_file) && File.read(irbrc_file) == irbrc_text + File.unlink irbrc_file end end