After generating a new custom gem in Ruby with Bundler, the error “cannot load such file” may be encountered when building and testing the new library locally.
There is a common reason why this occurs, and it can be found in the gemspec file:
# 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('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
The comment provides an explanation: files are included via the git ls-files
command, which will only include files that git is aware of.
Therefore if a new file has been created and not added to the index/staging area, it won’t be included in the build.
The solution is to ensure that any new files are staged with git add
.