为什么在 Gemfile 中包含 gem 可以解决 railtie 问题,即使 gem 已经包含在 Gemfile.lock 中?

Why does including a gem in Gemfile resolve a railtie issue, even though this same gem is already included in Gemfile.lock?

我正在努力思考为什么我一直在努力解决的问题现在神奇地得到了解决。

我正在构建一个使用 Spotify OAuth(通过 rspotify gem)的 Rails 应用程序并卡住了 on the exact issue described here. After spinning my wheels, I finally came across this comment,建议我明确添加 omniauth gem 到我的 Gemfile.

现在,这个 omniauth gem 已经是 Gemfile.lock 中专门针对 omniauth-oauth2 的依赖项。按照 linked comment 的建议,我将 omniauth 包含在我的 Gemfile 中,现在我的问题似乎已解决,但我真的不知道为什么。

这与 gems 如何被打包器加载有关。 Bundler.require 需要 Gemfile 中列出的 gem,但不需要它的依赖性。它取决于图书馆 require/load 它的依赖性。

omniauth 未明确添加到 Gemfile 时会出现上述问题,因此捆绑程序不需要它。

但是由于 omniauth-rails_csrf_protection 假设 ominauth 已经是必需的,当用户只添加 omniauth-rails_csrf_protection 而没有添加 omniauth 到 Gemfile 时会出错。

我已经为这个问题创建了一个可能的修复程序 https://github.com/cookpad/omniauth-rails_csrf_protection/pull/13

更新:此修复已合并到 gem 存储库中。