本文介绍了自定义Gem执行失败,并出现NoMethodError错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了我的第一个宝石(ruby noob alert!)。将它部署到我本地的gem目录中我运行:

  bundle install 
bundle exec rake install

此时我尝试从命令行运行我的gem。我得到一个神秘的错误:

  my_gem 
Gem文件位置:
C:/ ruby​​193 / lib / ruby / gems / 1.9.1 / gems / my_gem-0.0.1 / Gemfile
致命:不是git存储库(或任何父目录):.git
获取Gem.bin_path()
C:/ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:在`block in replace_bin_path'中:未定义的方法`name'for nil: NilClass(NoMethodError)$ C $ / b:C /ruby193/lib/ruby/gems/1.9.1/gems/my_gem-0.0.1/bin/my_gem:20:在< top(required)>'$ b:从C:/ ruby​​193 / bin / my_gem:23:在`load'
从C:/ ruby​​193 / bin / my_gem:23:在< main>'

当我运行bundle安装时,它用它自己的代码替换了bin / my_gem的内容。我没有添加修改这个文件稍微尝试调试。失败似乎发生在Gem.bin_path中。这里是bin / my_gem的内容:

 #!/ usr / bin / env ruby​​ 

#此文件由Bundler生成。

#应用程序'my_gem'是作为gem的一部分安装的,
#这个文件是为了方便运行它。


require'pathname'
ENV ['BUNDLE_GEMFILE'] || = File.expand_path(../../ Gemfile,
Pathname。 new(__ FILE __)。realpath)

putsGem File Location:
puts(ENV ['BUNDLE_GEMFILE'])

'rubygems'
需要'bundler / setup'

putsgetting gem.bin_path()
path = Gem.bin_path('my_gem','my_gem')#这是导致错误
putsGem.bin_path():#{path}

load Gem.bin_path('my_gem','my_gem')



我不知道下一步该做什么,只能从新鲜的宝石开始。任何想法?

解决方案

看起来你正遇到这个问题



因为Bundler会记住传递给 install 的标志,如果您以前曾经运行过 bundle install --binstubs 在随后的 bundle install 运行中生成binstub。您可以运行 bundle config --delete bin 删除该设置,然后恢复原始的可执行文件。


I've written my first gem (ruby noob alert!). To deploy it into my local gem directory I ran:

bundle install
bundle exec rake install

At this point I try to run my gem from the command line. I get a cryptic error:

my_gem
Gem File Location:
C:/ruby193/lib/ruby/gems/1.9.1/gems/my_gem-0.0.1/Gemfile
fatal: Not a git repository (or any of the parent directories): .git
getting Gem.bin_path()
C:/ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:in `block in replace_bin_path': undefined method `name' for nil:NilClass (NoMethodError)
    from C:/ruby193/lib/ruby/gems/1.9.1/gems/my_gem-0.0.1/bin/my_gem:20:in `<top (required)>'
    from C:/ruby193/bin/my_gem:23:in `load'
    from C:/ruby193/bin/my_gem:23:in `<main>'

When I ran bundle install it replaced the contents of bin/my_gem with it's own code. I did add modify this file slightly trying to debug. The failure seems to happen in Gem.bin_path. Here are the contents of bin/my_gem:

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'my_gem' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
   Pathname.new(__FILE__).realpath)

puts "Gem File Location: "
puts(ENV['BUNDLE_GEMFILE'])

require 'rubygems'
require 'bundler/setup'

puts "getting Gem.bin_path()"
path = Gem.bin_path('my_gem', 'my_gem') # this is the line that causes the error
puts "Gem.bin_path(): #{path}"

load Gem.bin_path('my_gem', 'my_gem')

I'm at a loss for what to do next other than start over with a fresh gem. Any ideas?

解决方案

It looks like you're running into this issue https://github.com/bundler/bundler/issues/2838

Because Bundler remembers flags passed to install, if you ever ran bundle install --binstubs in the past, it will re-generate the binstubs on subsequent runs of bundle install. You can run bundle config --delete bin to delete that setting and then restore your original executable file.

这篇关于自定义Gem执行失败,并出现NoMethodError错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 23:48