新手问题 [已解决] what's 'bundle exec' used for ?

xiaoronglv · August 24, 2013 · Last by willmouse replied at August 24, 2013 · 9186 hits

编译 assets 时,为什么前面要加一个 bundle exec,用途是什么?

当我运行 bundle exec rake assets:precompile --trace

** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Execute assets:precompile

当我运行 rake assets:precompile --trace

** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Execute assets:precompile


加 bundle exec 后你程序用的 gem 就都是 Gemfile 指定的了

stackoverflow 上有个不错的回答

http://stackoverflow.com/questions/6588674/what-does-bundle-exec-rake-mean

You're running bundle exec on a program. The program's creators wrote it when certain versions of gems were available. The program Gemfile specifies the versions of the gems the creators decided to use. That is, the script was made to run correctly against these gem versions.

#1 楼 @jjym

gem 版本号不同,导致编译资源异常,最常见于那种情况?

#3 楼 @xiaoronglv 貌似不是这么的常见..还没见过..

#3 楼 @xiaoronglv 如果你用 gem install rake 安装了 10.1.0 版本的 rake(假设是最新的),当你直接使用调用 rake 时,使用的会是这个最新版本的 rake。

如果项目的 Gemfile 中指定的版本是 0.9.6(或者是 Gemfile.lock 中是 0.9.6)的话,你如果不加 bundle exec,将会用 rake 10.1.0 的版本去执行本来应该由 0.9.6 版本的 rake 写出的 Rake task。

会不会出问题?可能会,可能不会。因为很有可能原作者使用 0.9.6 版本的 rake 写的 Rake task 中没有什么被废弃的部分,10.1.10 也能正确执行。但是不兼容的情况也会发生。

bundle exec 就是为了解决这样的问题而存在的:在目前的 Bundle 环境里执行某个操作,这样对于不同的人来说,不论系统里是什么版本的 Gem,总是会使用该项目 Gemfile 中指定的版本来执行某个操作。

You need to Sign in before reply, if you don't have an account, please Sign up first.