gpt4 book ai didi

ruby - 无法加载从 git 源安装的 gem (LoadError : cannot load such file)

转载 作者:行者123 更新时间:2023-12-04 04:15:20 24 4
gpt4 key购买 nike

我在 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 install 之后用 bundle list 验证了 gem 是否成功安装:

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

这是 repo 结构:

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

应用.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_pa​​ths 属性指定:

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

关于ruby - 无法加载从 git 源安装的 gem (LoadError : cannot load such file),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com