bundler Rails在开发中需要不同的gem

时间:2021-01-15 00:22:02

I'm trying to set up a develoment environment for developing a Rails gem. I would like to load the gem from a local source in development mode and via rubygems in production. I would like to do something like the following:

我正在尝试建立一个开发Rails gem的开发环境。我想在开发模式下从本地源加载gem并在生产中加载rubygems。我想做类似以下的事情:

Gemfile

group :production do
  gem 'my_gem', '1.0.0'
end

group :development do
  gem 'my_gem', :path => '~/apps/my_gem'
end

When I run the bundle command, bundler complains that you can't load the same gem twice. Is there a way to require different versions of a gem depending on the Rails environment?

当我运行bundle命令时,bundler会抱怨你无法加载两次相同的gem。有没有办法根据Rails环境要求不同版本的gem?

3 个解决方案

#1


10  

Doing this defeats the purpose of using Bundler. The whole point is that the dependencies you're using are consistent no matter where your application is loaded, deliberately trying to circumvent that goal is just going to cause you problems.

这样做会破坏使用Bundler的目的。重点是,无论您的应用程序在何处加载,您使用的依赖关系都是一致的,故意试图规避该目标只会导致您出现问题。

What happens when your local version of that gem is different than the one released in Rubygems (perhaps because you forgot to release a new version?)? Your app may blow up and you won't be able to reproduce it in development, which is horrible.

当您的本地版本的gem与Rubygems中发布的版本不同时(或许是因为您忘记发布新版本?)会发生什么?您的应用程序可能会爆炸,您将无法在开发中重现它,这太可怕了。

As for why this isn't even conceivable to achieve with Bundler (at least now): what happens if the dependency versions for the Gem are different in the Rubygems version vs. the local version are different? Now your entire Gemfile.lock needs to have two completely different dependency graphs, and you're potentially introducing countless more points of failure in production that wouldn't exist in development.

至于为什么用Bundler(至少现在)实现这个甚至是不可想象的:如果在Rubygems版本和本地版本中Gem的依赖版本不同,会发生什么?现在,您的整个Gemfile.lock需要有两个完全不同的依赖关系图,并且您可能会在开发中不存在的生产中引入无数更多的失败点。

That said, it's okay to temporarily change your Gemfile to the local version while making changes to the gem, but you should change it back and release a new version of the gem, then bundle update my_gem to update the Gemfile.lock accordingly.

也就是说,可以在更改gem时暂时将Gemfile更改为本地版本,但是您应该将其更改回来并发布新版本的gem,然后捆绑更新my_gem以相应地更新Gemfile.lock。

#2


20  

I've had the same problem and solved like this:

我有同样的问题并解决了这样的问题:

if ENV["RAILS_ENV"] == "development"
  gem 'my_gem', :path => '~/apps/my_gem'
else
  gem 'my_gem', '1.0.0'
end

then you can run RAILS_ENV=development bundle on your local machine and run any environment related command through RAILS_ENV=development bundle exec

然后你可以在本地机器上运行RAILS_ENV =开发包,并通过RAILS_ENV =开发包exec运行任何与环境相关的命令

#3


-5  

it's probably that you have put gem 'my_gem' in somewhere else also, double check it

可能你已经将gem'my_gem'放在其他地方,仔细检查一下

#1


10  

Doing this defeats the purpose of using Bundler. The whole point is that the dependencies you're using are consistent no matter where your application is loaded, deliberately trying to circumvent that goal is just going to cause you problems.

这样做会破坏使用Bundler的目的。重点是,无论您的应用程序在何处加载,您使用的依赖关系都是一致的,故意试图规避该目标只会导致您出现问题。

What happens when your local version of that gem is different than the one released in Rubygems (perhaps because you forgot to release a new version?)? Your app may blow up and you won't be able to reproduce it in development, which is horrible.

当您的本地版本的gem与Rubygems中发布的版本不同时(或许是因为您忘记发布新版本?)会发生什么?您的应用程序可能会爆炸,您将无法在开发中重现它,这太可怕了。

As for why this isn't even conceivable to achieve with Bundler (at least now): what happens if the dependency versions for the Gem are different in the Rubygems version vs. the local version are different? Now your entire Gemfile.lock needs to have two completely different dependency graphs, and you're potentially introducing countless more points of failure in production that wouldn't exist in development.

至于为什么用Bundler(至少现在)实现这个甚至是不可想象的:如果在Rubygems版本和本地版本中Gem的依赖版本不同,会发生什么?现在,您的整个Gemfile.lock需要有两个完全不同的依赖关系图,并且您可能会在开发中不存在的生产中引入无数更多的失败点。

That said, it's okay to temporarily change your Gemfile to the local version while making changes to the gem, but you should change it back and release a new version of the gem, then bundle update my_gem to update the Gemfile.lock accordingly.

也就是说,可以在更改gem时暂时将Gemfile更改为本地版本,但是您应该将其更改回来并发布新版本的gem,然后捆绑更新my_gem以相应地更新Gemfile.lock。

#2


20  

I've had the same problem and solved like this:

我有同样的问题并解决了这样的问题:

if ENV["RAILS_ENV"] == "development"
  gem 'my_gem', :path => '~/apps/my_gem'
else
  gem 'my_gem', '1.0.0'
end

then you can run RAILS_ENV=development bundle on your local machine and run any environment related command through RAILS_ENV=development bundle exec

然后你可以在本地机器上运行RAILS_ENV =开发包,并通过RAILS_ENV =开发包exec运行任何与环境相关的命令

#3


-5  

it's probably that you have put gem 'my_gem' in somewhere else also, double check it

可能你已经将gem'my_gem'放在其他地方,仔细检查一下