bundle install导致“在此上下文中不允许映射值”错误

时间:2016-03-10 15:38:01

标签: ruby-on-rails rubygems yaml bundler

我有一个已经工作了一段时间的rails 4项目。我尝试安装taps但收到错误消息:

$ gem install taps

/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse': (<unknown>): mapping values are not allowed in this context at line 2 column 29 (Psych::SyntaxError)
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:318:in `parse'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:245:in `load'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/config_file.rb:333:in `load_file'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/config_file.rb:198:in `initialize'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/gem_runner.rb:74:in `new'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/gem_runner.rb:74:in `do_configuration'
    from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/gem_runner.rb:39:in `run'
    from /usr/local/rvm/rubies/ruby-2.2.1/bin/gem:21:in `<main>'

尝试运行gem listbundle update时收到相同的错误消息。我searched for similar problems,似乎原因通常是格式不正确的YAML文件。我的database.yml文件和Gemfile最近未更新,因此我不知道何时引入此错误。

我找到了描述此错误的rails issue on Github,有人建议使用irb解析YAML文件以隔离问题。我尝试解析database.ymlGemfile

配置/ database.yml的

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3
    
$ irb -ryaml
2.2.1 :001 > YAML.load_file 'config/database.yml'
 => {"default"=>{"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000}, "development"=>{"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/development.sqlite3"}, "test"=>{"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/test.sqlite3"}, "production"=>{"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/production.sqlite3"}} 

的Gemfile

source 'https://rubygems.org'

gem 'rails',                   '4.2.2'
gem 'faker',                   '1.4.2'
gem 'will_paginate',           '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass',          '3.2.0.0'
gem 'sass-rails',              '5.0.2'
gem 'uglifier',                '2.5.3'
gem 'coffee-rails',            '4.1.0'
gem 'jquery-rails',            '4.0.3'
gem 'turbolinks',              '2.3.0'
gem 'jbuilder',                '2.2.3'
gem 'devise',                  '3.5.6'

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

group :test do
  gem 'minitest-reporters', '1.0.5'
  gem 'mini_backtrace',     '0.1.3'
  gem 'guard-minitest',     '2.3.1'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end
$ irb -ryaml
2.2.1 :001 > YAML.load_file 'Gemfile'
 => "source 'https://rubygems.org'\n\ngem 'rails',                   '4.2.2' gem 'faker',                   '1.4.2' gem 'will_paginate',           '3.0.7' gem 'bootstrap-will_paginate', '0.0.10' gem 'bootstrap-sass',          '3.2.0.0' gem 'sass-rails',              '5.0.2' gem 'uglifier',                '2.5.3' gem 'coffee-rails',            '4.1.0' gem 'jquery-rails',            '4.0.3' gem 'turbolinks',              '2.3.0' gem 'jbuilder',                '2.2.3' gem 'devise',                  '3.5.6'\ngroup :development, :test do gem 'sqlite3',     '1.3.9' gem 'byebug',      '3.4.0' gem 'web-console', '2.0.0.beta3' gem 'spring',      '1.1.3' end\ngroup :test do gem 'minitest-reporters', '1.0.5' gem 'mini_backtrace',     '0.1.3' gem 'guard-minitest',     '2.3.1' end\ngroup :production do gem 'pg',             '0.17.1' gem 'rails_12factor', '0.0.2' end" 

两个文件都正确解析,所以我被卡住了。我不知道为什么命令会一直失败,因为错误消息无法识别有问题的文件。

1 个答案:

答案 0 :(得分:4)

我终于能够找到此错误的来源。

<强>的rubygems / config_file.rb:

SYSTEM_WIDE_CONFIG_FILE = File.join SYSTEM_CONFIG_PATH, 'gemrc'
...
system_config = load_file SYSTEM_WIDE_CONFIG_FILE

我尝试解析我的.gemrc文件但失败了:

$ irb -ryaml
2.2.1 :001 > YAML.load_file '/home/ubuntu/.gemrc'
Psych::SyntaxError: (/home/ubuntu/.gemrc): mapping values are not allowed in this context at line 2 column 29
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse'
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:318:in `parse'
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:245:in `load'
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:464:in `block in load_file'
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:464:in `open'
        from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/psych.rb:464:in `load_file'
        from (irb):1
        from /usr/local/rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'

不知何故,我的〜/ .gemrc加入了两行:

install: --no-rdoc --no-ri
update: --no-rdoc --no-rigem: --no-rdoc --no-ri

将它们分开以解决问题:

install: --no-rdoc --no-ri
update: --no-rdoc --no-ri
gem: --no-rdoc --no-ri

由于我的.gemrc文件不在我的项目的修订控制之下,无论我检查哪个历史提交,我都会看到错误。