There's only one error I get when installing my custom gem: File not found: lib ERROR: While generating documentation
The install continues and says gem installed successfully. However, when I require 'boxes' I get LoadError: no such file to load -- boxes and when I look into the gem directory I notice that there is no lib/ folder in the installed gem. I'm not sure why this is or how to debug it. Any pointers? Below is my gemspec file.
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'boxes/version'
Gem::Specification.new do |gem|
gem.authors = ["Name"]
gem.email = ["http://somedomain.com"]
gem.description = %q{Boxes, a Sass framework}
gem.summary = %q{Boxes is a modular Sass-based framework that is heavily configurable, extremely modular and works as a Sass and a CSS Framework}
gem.homepage = "http://somedomain.com"
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "boxes"
gem.require_paths = ["lib"]
gem.version = Boxes::VERSION
gem.license = "MIT"
gem.add_dependency "sass", [">= 3.2.0"]
gem.add_development_dependency "rake"
end
I'm new at ruby, and I'm just creating my first gem here, so please bear with me.
One thing I noticed is that on line 1, you're defining "lib" as "../lib", though in gem.require_paths "lib" is in the current directory. Is that true?
Another thing to look for is that lib is not in .gitignore, as you're using git ls-files for defining gem.files
edit
Noticed in your gemfile that you're using ruby 1.8.7, make sure you require 'rubygems' before you require your gem
Related
I created a gem and installed it on my system. The gem has 2 rb files, Myfile.rb and file_two.rb, one requires another, both are in lib folder. When I'm trying to load it in irb like require 'Myfile', I get an error
- LoadError: cannot load such file -- /usr/local/lib/ruby/gems/2.0.0/gems/Myfile-0.1.0/lib/file_two
In gem folder I see only 1 file. I believe the problem is in the following snippet of gemspec:
spec.files = Dir['Rakefile', '{bin,lib}/**/*',
'README*', 'LICENSE*'] & `git ls-files -z`.split("\0")
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
Please, advice how to rewrite it correctly, so both my files get into gem package.
Thanks in advance!
I am developing a gem called I19. It is supposed to have a CLI. So in bin/i19 and I require "i19".
And from the file lib/i19.rb I do require files files placed in lib/i19/.
If I do bundle console everything works, but when I try to execute the binary file (I'm doing rake install; i19 help) I get this error: require': cannot load such file -- i19/scanners/pattern_scanner (LoadError)
This is how my files look like:
# bin/i19
#!/usr/bin/env ruby
require "thor"
require "i19"
module I19
class CLI < Thor
end
end
I19::CLI.start
# lib/i19.rb
require "i19/version"
require "i19/commands"
require "i19/scanners/pattern_scanner"
require "i19/scanners/pattern_with_scope_scanner"
module I19
end
# lib/scanners/pattern_scanner.rb
require 'i19/scanners/base_scanner'
module I19::Scanners
class PatternScanner < BaseScanner
# ...
end
end
I don't understand why it works from the console but it doesn't from the command line.
Ok I found the answer myself. The problem was not with the ruby code but the way the gem gets compiled.
My gemspec file looks like this
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'i19/version'
Gem::Specification.new do |spec|
# ...
spec.files = `git ls-files`.split($/)
# ...
end
the problem is that git ls-files is not listing untracked files. Once I did git add lib/i19/scanners/pattern_scanner.rb it worked.
It makes me then think that my workflow of manually testing the CLI might not be the best, ie rake install; i19 update . Is there a better way?
I am not clear on what certain specifications in the .gemspec file are doing. Specifically,
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
Can someone explain how these relate to the functionality of a Ruby Gem and why they are necessary?
executables:
Executables included in the gem. For example, the rake gem has rake as an executable. These files must be executable Ruby files.
files:
Files included in the gem. These are the files that will be included in your gem when it is built.
require_paths:
Contains an Array of directories and files which should be added to the $LOAD_PATH on gem activation. By default it is ["lib"].
test_files
Test files included in the gem.
I created a gem with the following gemspec file.
$:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = 'SomeToken'
s.version = '0.0.0'
s.date = '2013-08-04'
s.summary = "A gem for use with SomeToken."
s.description = "A gem for use with SomeToken."
s.authors = ["Jason Tanner"]
s.email = 'jasontanner328#gmail.com'
s.files = Dir.glob '**/*'
s.homepage = ''
s.license = ''
end
Then in my terminal I run
gem build sometoken.gemspec
Then,
gem install ./SomeToken-0.0.0.gem
The gem is successfully installed, so when I run irb and I run...
require 'SomeToken'
Which responds with the error
LoadError: cannot load such file -- SomeToken
from /Users/jason/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
from /Users/jason/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
from (irb):1
from /Users/jason/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
I've tried changing the casing for the string, in numerous combinations but still get the same error. What's wrong with my gem and how can I fix it?
Try require 'some_token'.
Using require in general
require takes the name of a ruby file, not the name of a gem. For example, if you have the following directory structure
- foo.rb
- main.rb
Then in main.rb, you can use require 'foo' to use stuff from foo.rb.
Using require with gems
Notice that the first line of your gemspec has $:.push File.expand_path("../lib", __FILE__). This adds the lib directory of your gem to the search path. Thus, if you have
lib/
some_token.rb
then you should use require 'some_token'.
I don't know if Ruby 2.0.0 have this 'bug', but some Ruby versions must have a
require 'rubygems'
before you require any gem.
Guess it's worth a try :)
I'm trying to build my first ruby gem that contains some classes I want to use across projects - for the sake of this question it is called test_service_api.
I have written and built the gem using "gem build test_service_api.spec". I have then done a "gem install test_service_api-0.0.2.gem".
The spec file is:
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/test_service_api/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Tyler Power"]
gem.email = ["tyler#xxx.com"]
gem.description = %q{}
gem.summary = %q{}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "test_service_api"
gem.require_paths = ["lib"]
gem.version = TestServiceApi::VERSION
end
I'm now trying to use the gem in a little test project, in the test projects Gemfile I have specified a dependency on the gem I built:
source 'https://rubygems.org'
gem 'sinatra'
gem 'test_service_api'
I can then do a "bundle install" and the bundler correctly finds all my dependencies and puts them in vendor/cache:
Tylers-MacBook-Pro:Test tyler$ bundle install
Using rack (1.4.1)
Using rack-protection (1.2.0)
Using test_service_api (0.0.2)
Using tilt (1.3.3)
Using sinatra (1.3.2)
Using bundler (1.1.3)
Updating .gem files in vendor/cache
Your bundle is complete! It was installed into ./vendor/bundle
So that all looks good to me so far, the issue I have is that when I try to require the gem in a source file in the test project it can't find it:
require 'rubygems'
require 'sinatra'
require 'test_service_api'
get '/' do
res = ''
ENV.each do |k, v|
res << "#{k}: #{v}<br/>"
end
res
end
My IDE complains of "No file to load" and if I try and run the app I get:
./app.rb:3:in `require': no such file to load -- test_service_api (LoadError)
from ./app.rb:3
I'm close to pulling my hair out... Does anyone have any ideas on how to resolve this?
What about trying this:
require "rubygems"
require "bundler/setup"
instead of just rubygems?