【问题标题】:How can I avoid bundlers warning about multiple sources when I have all gems in my .gemspec?当我的 .gemspec 中包含所有 gem 时,如何避免打包程序警告多个来源?
【发布时间】:2015-05-11 22:06:11
【问题描述】:

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

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

gemspec

我的.gemspec 的所有依赖项都列为add_dependencyadd_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 规范中找不到任何关于源选项的信息。

【问题讨论】:

  • 您是否尝试过使用像 example 这样的源代码块?
  • 问题是,我没有在 Gemfile 中列出我的 gem 依赖项。它们都列在 .gemspec 中。我必须在 Gemfile 中复制它们吗?那么引用 gemspec 有什么意义呢?
  • @ChristophPetschnig Here 是一篇关于Gemfile.gemspec 持有的角色的好文章。
  • @engineersmnky 谢谢。尽管如此,它还是谈到了避免重复。使用源代码块(这很有意义)意味着重复。此外,我对 .gemspec 中的add_development_dependency 越来越怀疑。我相信这来自预捆绑器时代,那里的宝石应该属于 Gemfile。

标签: ruby rubygems bundler gemspecs


【解决方案1】:

要详细说明the bundler issue 上的讨论,正如之前的答案所述,您必须在您的Gemfile 中包含宝石。但是,您只需要在 .gemspec 中指定 gem 的版本。如果您更改版本的频率高于私有依赖项,这不是一个糟糕的解决方案。

Gemfile中引用没有版本的gem:

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

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

gemspec

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

# 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】:

    有点难过,但必须将其移至 Gemfile :-(

    宝石文件:

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

    但是,如果您的某些宝石应该包含在 :development:test 组中,则可以使用以下方法

    宝石文件:

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

    【讨论】:

    • 但是 gem 编译呢?这些宝石将包含在版本中吗?
    【解决方案3】:

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

    问题是gemspec 格式不支持为每个gem 指定来源,因此如果不将它们复制到Gemfile 中,就无法指定每个来源的哪些gem。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2017-10-22
    • 2023-03-09
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多