Unable to load gem installed from git source (LoadError: cannot load such file)
Unable to load gem installed from git source (LoadError: cannot load such file)
我在 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 list
:
在 bundle install
之后验证了 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 文件
require
方法查看 $LOAD_PATH
中的目录并尝试查找名称与参数 registry_client
匹配的文件。 gemspec 中的 spec.required_paths
属性指定:
Paths in the gem to add to $LOAD_PATH when this gem is activated. The default value is "lib"
因此,您的 lib
目录必须有一个名为 registry_client
的文件(在您的情况下,它将具有 .rb
扩展名)。
更多相关信息:https://guides.rubygems.org/patterns/#loading-code
我在 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 list
:
bundle install
之后验证了 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 文件
require
方法查看 $LOAD_PATH
中的目录并尝试查找名称与参数 registry_client
匹配的文件。 gemspec 中的 spec.required_paths
属性指定:
Paths in the gem to add to $LOAD_PATH when this gem is activated. The default value is "lib"
因此,您的 lib
目录必须有一个名为 registry_client
的文件(在您的情况下,它将具有 .rb
扩展名)。
更多相关信息:https://guides.rubygems.org/patterns/#loading-code