问题 当我在.gemspec中拥有所有宝石时,如何避免捆绑者警告多个来源?


在我自己的宝石中,我有一个 Gemfile 看起来基本上是这样的:

source 'https://my.gemserver.com'
source 'https://rubygems.org'

gemspec

我的 .gemspec 将所有依赖项列为 add_dependency 和 add_development_dependency

截至Bundler 1.8,我收到警告:

Warning: this Gemfile contains multiple primary sources. Using `source` more than
once without a block is a security risk, and may result in installing unexpected gems.
To resolve this warning, use a block to indicate which gems should come from the
secondary source. To upgrade this warning to an error,
run `bundle config disable_multisource true`.

有没有办法解决此警告(没有通过捆绑配置静音)?我在Rubygems规范中找不到任何关于源选项的内容。


7373
2018-03-10 12:52


起源

您是否尝试过使用这样的源块 例? - O-I
问题是,我没有在Gemfile中列出我的gem依赖项。它们都列在.gemspec中。我是否必须在Gemfile中复制它们?那么指的是gemspec是什么意思? - Christoph Petschnig
@ChristophPetschnig 这里 是关于角色的好文章 Gemfile 和 .gemspec 保持。 - engineersmnky
@engineersmnky谢谢。它仍然谈到避免重复。使用源块(这很有意义)意味着重复。而且,我越来越怀疑 add_development_dependency 在.gemspec中。我相信这来自一个捆绑前时代,那里的宝石应该属于Gemfile。 - Christoph Petschnig


答案:


不,您需要将警告静音或将源块添加到您的 Gemfile 使用您希望来自私人服务器的特定宝石。没有必要复制来自的那些 rubygems.org (或者你可以反过来做,如果你依赖更多私人宝石而不是公共宝石,而你的私人宝石本身并不依赖于公共宝石)。

问题是, gemspec format不支持为每个gem指定源代码,因此不需要将它们复制到 Gemfile,没有办法指定每个来源的宝石。


6
2018-03-11 05:13



哪个是应该解决的错误! (恕我直言) - Ich
仅供参考,关于其原因的进一步讨论是在这个问题上: github.com/bundler/bundler/issues/3576 - jwadsack


有点伤心,但一个人必须把它移到Gemfile :-(

的Gemfile:

source 'https://my.gemserver.com' do
  your_gem1
  your_gem2
  #...
end

source 'https://rubygems.org'

gemspec

但是,如果你的某些宝石应该包括在内 :development 要么 :test 小组,可以使用以下

的Gemfile:

your_gem1, :source => 'https://my.gemserver.com'
#...
group :development do
  your_gem2, :source => 'https://my.gemserver.com'
  #...
end

source 'https://rubygems.org'

gemspec

5
2018-02-10 11:32



但是宝石编译怎么样?这个宝石将包含在发布中? - Bruno Casali


详细讨论 bundler 问题,正如之前的答案所说,你 必须 包括你的宝石 Gemfile。但是,您只需要在您的指定中指定gem的版本 .gemspec。如果你比私有依赖项更频繁地更改版本,这不是一个糟糕的解决方案。

引用没有版本的gem Gemfile

# Gemfile
source 'https://rubygems.org'

source 'https://xxx@gem.fury.io/me/' do
  gem 'my-private-dependency'
end

gemspec

引用带有版本规范的gem .gemspec

# my-gem.gemspec
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
  spec.add_dependency 'my-private-dependency', '~> 0.1.5'
end

2
2018-04-25 16:23