reedstonefood
reedstonefood

Reputation: 131

Ruby cannot load such file - but can load similar files in the same directory

There are many "cannot load such file" questions around but I can't find one that fits my scenario.

I am building a gem, using the normal bundler method. Development and testing has been completed to a satisfactory point. All the actual files are in the /lib/my_gem folder. Then I built a CLI, called /lib/my_gem/cli.rb. Like all my other files in that folder, they are all in the Module MyGem.

I changed the file /lib/my_gem to read like this. Names changed to protect the innocent.

require "my_gem/version"
require "my_gem/file1"
require "my_gem/file2"
require "my_gem/file3"
require "my_gem/file4"
require "my_gem/file5"
require "my_gem/file6"
require "my_gem/cli"

module MyGem
  # Nothing
end

Then I built the gem using rake build, and installed the gem using gem install. However when I then put require 'my_gem' into another ruby file, and run it, I get....

C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kerel_require.rb:55 in 'require' cannot load such file - my_gem/cli (LoadError)

But - if I comment out line 8 in the above code segment, it all works just fine. I can't understand why require works fine for the first 7 files but not the 8th?

Upvotes: 0

Views: 317

Answers (1)

akuhn
akuhn

Reputation: 27793

Files must be whitelisted in the gemspec file.

A common practice is to defer to git for the list of files.

You commented that your gemspec looks like this

spec.files = git ls-files -z.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe" 
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 
spec.require_paths = ["lib"] 

Maybe your file is not committed to git? Is it listed with git ls-files?

Upvotes: 1

Related Questions