Невозможно загрузить gem, установленный из git источника (LoadError: не может загрузить такой файл) - PullRequest
2 голосов
/ 22 марта 2020

Я создал камень с именем registry_client в структуре монорепо. С надеждой, что я смогу использовать это внутренне в моем frontend-app. Тем не менее, я столкнулся с этой ошибкой, когда я пытался запустить его с помощью bundler bundle exec ruby app.rb:

LoadError: cannot load such file -- registry_client

Gemfile в frontend-app:

source 'https://rubygems.org'

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

Я подтвердил, что гем успешно установлен после bundle install с bundle list:

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.gemspe c

# 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

Редактировать: добавлен gemspe c file

1 Ответ

1 голос
/ 23 марта 2020

Метод require просматривает каталоги в $LOAD_PATH и пытается найти файл с именем, совпадающим с аргументом registry_client. Атрибут spec.required_paths в gemspe c указывает:

Пути в самоцвете, добавляемые в $ LOAD_PATH при активации этого самоцвета. Значением по умолчанию является «lib»

Следовательно, в вашем каталоге lib должен быть файл с именем registry_client (в вашем случае он будет иметь расширение .rb). Подробнее об этом здесь: https://guides.rubygems.org/patterns/#loading -код

...