From 975fd746dbc97c7740a8f068c7b8748ca2444b53 Mon Sep 17 00:00:00 2001 From: Damien Robert Date: Mon, 1 Oct 2018 06:18:28 +0200 Subject: [PATCH] PluginManager#load_cli_options: use the realpath (#1762) * PluginManager#load_cli_options: use the realpath Since ruby 2.5, `require 'foo'` will use the realpath of the file corresponding to foo.rb. However `require '/absolute/path/to/foo.rb'` won't use the realpath. So when $GEM_HOME contains a symlink (ie it is not the realpath), when a pry plugin is loaded, by `activate!`: require gem_name if !active? the real_path of `gem_name` is used. But then in load_cli_options: cli_options_file = File.join(spec.full_gem_path, "lib/#{spec.name}/cli.rb") require cli_options_file if File.exist?(cli_options_file) since the path given is absolute, it will be used directly without realpath. This means that cli.rb may potentially be required twice (once via its realpath if the plugin requires it, the other via its $GEM_HOME path when required by `load_cli_options`), which could raise some errors. Fix this by using the realpath in load_cli_options too. Revision r59984 in ruby 2.5 introduced the use of realpath for load paths, and it was backported to version 2.4 in the minor revision 2.4.4. So only use the realpath ourselves for ruby versions above or equal to 2.4.4. --- lib/pry/plugins.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pry/plugins.rb b/lib/pry/plugins.rb index b4fdadf4..525ac712 100644 --- a/lib/pry/plugins.rb +++ b/lib/pry/plugins.rb @@ -35,6 +35,7 @@ class Pry # Load the Command line options defined by this plugin (if they exist) def load_cli_options cli_options_file = File.join(spec.full_gem_path, "lib/#{spec.name}/cli.rb") + cli_options_file = File.realpath(cli_options_file) if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4.4") require cli_options_file if File.exist?(cli_options_file) end # Activate the plugin (require the gem - enables/loads the