捆绑包执行程序是否需要和“捆绑包/设置”等效? [英] Are bundle exec and require 'bundler/setup' equivalent?

查看:98
本文介绍了捆绑包执行程序是否需要和“捆绑包/设置”等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些功能是否完全相同?

Do these things accomplish exactly the same?


  • 使用 bundle exec ruby​​ foo.rb启动ruby进程。

  • 具有要求打包程序/设置 作为 foo的第一行.rb

  • starting a ruby process with bundle exec ruby foo.rb
  • having require "bundler/setup" as the first line of foo.rb

推荐答案

在您的特定示例可以认为它们是相同的,但是实际上它们并不相同。

In your specific example they can be considered the same, however in reality they are not the same.

bundle exec bundler / setup 的环境进行了一些更改$ c>没有。如果您的 foo.rb 从不运行子shell,或者从不尝试在子shell中运行其他ruby可执行文件,则这两个版本都是等效的(它们都将正确加载捆绑的gem并正常工作

bundle exec makes some changes to the environment that bundler/setup does not make. If your foo.rb never runs a subshell, or never tries to run other ruby executables in subshells, then both versions are equivalent (they will both load bundled gems correctly and work exactly the same).

使用 bundle exec 的整个想法是使您能够运行可执行文件 最初设计时并未考虑捆绑器。像 rspec rails rackup 。如果您自己的应用程序( foo.rb )没有尝试运行可能依赖于捆绑软件的可执行文件,则这两种方法都没有区别。既然您要确保使用捆绑程序,就是加载正确的gem,并且对于 bundler / setup 而言,它可以完全按照您的情况工作。

The whole idea with bundle exec is to enable you to run executables that were not originally designed with bundler in mind. Like rspec, rails, rackup. If your own app (foo.rb) does not try to run such executables that might be dependent on your bundles, then it makes no difference either way. Since all you want to make sure with bundler is that you load the correct gems, and for that bundler/setup works exactly as expected in your case.

在谈论运行ruby系统可执行文件时,来自捆绑程序文档

From the bundler docs when talking about running ruby system executables:


在某些情况下,如果可执行文件恰好安装在您的系统中并且没有引入与您的软件包相冲突的gem,则运行不带软件包exec的可执行文件可能会起作用。

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.

但是,这是不可靠的,并且是造成巨大痛苦的根源。即使看起来像它可以工作,将来也可能无法在另一台机器上工作。

However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.

然后从bundle exec的联机帮助,您可以获得有关 bundle exec 实际上是:

Then from the manpage of bundle exec you can get some additional clues as to what bundle exec actually does:


环境修改


  • 确保仍然有可能从捆绑执行(使用$ BUNDLE_BIN_PATH)调用的命令中将其打包为外壳

  • 放入包含可执行文件的目录(例如$ PATH上的包的rails,rspec,rackup)

  • 确保如果在子shell中调用bundler,它会使用相同的Gemfile(通过设置BUNDLE_GEMFILE)

  • 将-rbundler / setup添加到$ RUBYOPT中,以确保在子外壳中调用的Ruby程序可以看到捆绑中的宝石

  • make sure that it's still possible to shell out to bundle from inside a command invoked by bundle exec (using $BUNDLE_BIN_PATH)
  • put the directory containing executables (like rails, rspec, rackup) for your bundle on $PATH
  • make sure that if bundler is invoked in the subshell, it uses the same Gemfile (by setting BUNDLE_GEMFILE)
  • add -rbundler/setup to $RUBYOPT, which makes sure that Ruby programs invoked in the subshell can see the gems in the bundle

因此,如果您使用考虑到捆绑程序支持,那么您就不需要 bundle exec 您的应用了。

So if you build your app with bundler support in mind, then you never need to bundle exec your app.

但是如果您需要使用其他工具来加载应用程序代码,这些工具可能会在之前加载宝石,应用代码(然后可能会插入错误的非捆绑gem),那么您需要使用 bundle exec

But if you need to use other tools that load your app code that might load gems before they load your app code (which then might pull in a wrong non-bundled gem), then you need to use bundle exec.

这篇关于捆绑包执行程序是否需要和“捆绑包/设置”等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆