英文:
Debugger statement inside `rubygems_plugin.rb` is not reached when running `bundle install`
问题
当阅读Rbenv的rubygems_plugin.rb
文件时,我遇到了以下这行代码:
if defined?(Bundler::Installer) && Bundler::Installer.respond_to?(:install) && !Bundler::Installer.respond_to?(:install_without_rbenv_rehash)
查看这行代码的git历史,我发现这行代码的原始版本是在2015年的此PR中添加的,其目标是确保rbenv rehash
只在gem安装过程结束时运行一次。而rbenv rehash
的目标是生成~/.rbenv/shims
目录下包含终端命令的任何Ruby gem的shim文件。
根据原始的PR和讨论,似乎这段代码将在包含Gemfile的项目中运行bundle install
命令时执行。我想逐步了解这个过程是如何进行的,所以我进行了以下操作:
- 我使用
rbenv install 3.1.4
安装了一个全新版本的Ruby(v3.1.4
)。 - 我通过
gem install rails
安装了rails
gem。 - 我通过
rails new foobar
生成了一个新的Rails项目。 - 为了简化事情,我注释掉了除了第一个(
gem 'rails', '~> 5.2.8', '>= 5.2.8.1'
)和我添加的一个(gem 'wisper'
)之外的所有gem
调用。 - 我在
rubygems_plugin.rb
内部的上述if
检查下面添加了一个调试器语句。 - 我运行了
bundle install
。
然而,我没有触发我的调试器语句。我还尝试在if
语句之外放置了第二个调试器语句,并重新运行bundle install
,但是这个调试器也被跳过了。
我最好的猜测是我可能做错了什么,我的理解可能有些偏差,这可能导致我无法触发我的调试器语句。如果不行,我还想到了一种(小概率)情况:
- Bundler曾经在执行过程中运行RubyGems插件(因此有了来自PR的讨论),但是...
- ...这在PR日期和今天之间的某个时候不再成立了。
有人能否看出我的思考出了什么问题?
英文:
While reading through RBENV's rubygems_plugin.rb
file, I encountered the following line of code:
if defined?(Bundler::Installer) && Bundler::Installer.respond_to?(:install) && !Bundler::Installer.respond_to?(:install_without_rbenv_rehash)
Reviewing this line's git history, I saw that the original version of this line was added in this PR from 2015, and its goal was to ensure that rbenv rehash
is only run once, at the end of the gem installation process. The goal of rbenv rehash
, in turn, is to generate shim files inside ~/.rbenv/shims
for any Ruby gem which includes a terminal command.
Based on the original PR and discussion, it appeared that this code would be executed when the bundle install
command is run inside a project which includes a Gemfile. I wanted to step through this process as it happened, to learn more about Bundler, so I did the following:
- I installed a fresh version of Ruby (
v3.1.4
) usingrbenv install 3.1.4
. - I installed the
rails
gem viagem install rails
. - I generated a new Rails project via
rails new foobar
. - To simplify things, I commented out all the invocations of
gem
except for the first one (gem 'rails', '~> 5.2.8', '>= 5.2.8.1'
) and one that I added (gem 'wisper'
). - I added a debugger statement just below the aforementioned
if
check insiderubygems_plugin.rb
. - I ran
bundle install
However, I didn't hit my debugger statement. I also tried placing a 2nd debugger statement outside the if
statement and re-running bundle install
, but that debugger was also skipped.
My best guess is that I'm simply doing something wrong and my understanding is off somehow, and that this is preventing me from reaching my debugger statements. Failing that, I also thought there's a (small) chance that:
- Bundler used to run RubyGems plugins as part of its execution (hence the discussion from the PR), but that...
- ...this stopped being true sometime between the PR's date and today.
Can anyone spot where my thinking has gone awry?
答案1
得分: 0
看起来,这个PR 对Bundler代码库进行了更改,将Gem.load_env_plugins
替换为 Gem.load_plugins
。后者在不同的目录中搜索插件,这意味着RBENV的 rubygems_plugins.rb
不再被找到或加载。
我通过将 Gem.load_env_plugins
添加回我的Bundler安装的 lib/bundler/cli/install.rb
文件中的源代码,并重新运行 bundle install
来确认了这一点。这导致我成功触发了我添加到RBENV的 rubygems_plugin.rb
文件中的断点。
英文:
It looks like this PR to the Bundler codebase introduced a change, such that Gem.load_env_plugins
was replaced with Gem.load_plugins
. The latter searches for plugins in different directories than the former, which means that RBENV's rubygems_plugins.rb
is no longer found or loaded.
I confirmed this by adding Gem.load_env_plugins
back into the source code of my Bundler installation's lib/bundler/cli/install.rb
file, and re-running bundle install
. This resulted in my successfully hitting the breakpoints I had added to RBENV's rubygems_plugin.rb
file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论