【问题标题】:Unable to load gem installed from git source (LoadError: cannot load such file)无法加载从 git 源安装的 gem(LoadError:无法加载此类文件)
【发布时间】:2020-03-22 10:13:09
【问题描述】:

我在 monorepo 结构中创建了一个名为 registry_client 的 gem。希望我可以在我的frontend-app 内部使用它。但是,当我尝试使用 bundler bundle exec ruby app.rb 运行它时遇到了这个错误:

LoadError: cannot load such file -- registry_client

frontend-app 中的 Gemfile:

source 'https://rubygems.org'

gem 'registry_client', git: 'https://github.com/.../some_repo.git', branch: 'master', glob: 'registry-client/*.gemspec'

我在bundle installbundle list 之后验证了gem 安装成功:

Gems included by the bundle:
  ...
  * registry_client (0.1.1 f93f5bd)
  ...

这是回购结构:

├── frontend-app
│   ├── Gemfile
│   ├── Gemfile.lock
│   ├── Rakefile
│   ├── app.rb
│   ├── config
│   ├── config.ru
│   ├── test
│   └── views
└── registry-client
    ├── Gemfile
    ├── lib
    ├── registry_client.gemspec
    └── test

app.rb

require 'registry_client'

...

registry_client.gemspec

# frozen_string_literal: true

require_relative 'lib/version'

Gem::Specification.new do |spec|
  spec.name          = 'registry_client'
  spec.summary       = 'Registry client for using Redis as Service Registry.'
  spec.authors       = ['Author']
  spec.version       = RegistryClient::VERSION
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')

  # Specify which files should be added to the gem when it is released.
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  end
  spec.bindir        = 'exe'
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ['lib']
end

编辑:添加 gemspec 文件

【问题讨论】:

  • 运行gem environment,获取INSTALLATION DIRECTORY的值。然后将/gems 添加到安装目录并使用ls 命令列出其内容。你在那儿看到你的宝石了吗?
  • @PetrGazarov 目录中缺少 gem,但我相信可能是预期的行为,来自捆绑程序文档:Note that because RubyGems lacks the ability to handle gems from git, any gems installed from a git repository will not show up in gem list. They will, however, be available after running Bundler.setup.
  • @PetrGazarov 这是bundle info registry_client下显示的内容:* registry_client (0.1.1 f93f5bd) Summary: Registry client for using Redis as Service Registry. Path: /Users/julianho/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/bundler/gems/service-api-example-impl-f93f5bd49c46/registry-client
  • 当你ls /Users/julianho/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/bundler/gems/service-api-example-impl-f93f5bd49c46/registry-client时,你看到registry_client.gemspec了吗?
  • 是的。 total 24 drwxr-xr-x 7 julianho staff 224B Mar 22 16:13 . drwxr-xr-x 10 julianho staff 320B Mar 22 16:13 .. -rw-r--r-- 1 julianho staff 115B Mar 22 16:13 Gemfile -rw-r--r-- 1 julianho staff 270B Mar 22 16:13 Gemfile.lock drwxr-xr-x 6 julianho staff 192B Mar 22 16:13 lib -rw-r--r-- 1 julianho staff 832B Mar 22 16:13 registry_client.gemspec drwxr-xr-x 3 julianho staff 96B Mar 22 16:13 test

标签: ruby git rubygems bundle


【解决方案1】:

require 方法查看$LOAD_PATH 中的目录并尝试查找名称与参数registry_client 匹配的文件。 gemspec 中的spec.required_paths 属性指定:

激活此 gem 时添加到 $LOAD_PATH 的 gem 中的路径。默认值为“lib”

因此,您的lib 目录必须有一个名为registry_client 的文件(在您的情况下,它将具有.rb 扩展名)。 更多信息在这里:https://guides.rubygems.org/patterns/#loading-code

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2016-04-26
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多