rails error, couldn't parse YAML

2019-01-02 14:32发布

After updating the gems I've got this:

/home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 182 column 9 (Psych::SyntaxError)
    from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:148:in `parse_stream'
    from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:119:in `parse'
    from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:106:in `load'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:6:in `<module:LATEX>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:3:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `each'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `block in require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `each'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler.rb:120:in `require'
    from /home/megas/Work/railscasts/config/application.rb:10:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:28:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:28:in `block in <top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:27:in `tap'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

ruby-1.9.2-p136 rails 3.0.3

Tried to reinstall gem RedCloth, didn't help, system wants to use only 4.2.3 version

Any idea how to fix it? Thanks

29条回答
谁念西风独自凉
2楼-- · 2019-01-02 15:11

Pshych parse is suck to the core. I am not sure if this is elegant solution but i manage to fix this problem by uninstalling it.

gem uninstall psych
查看更多
高级女魔头
3楼-- · 2019-01-02 15:12

For other people looking at this I found the issue in rerun.txt which was being called by config/cucumber.yml in a Rails app. rerun.txt was configured to store the most recent cucumber failing test and I had somehow entered weird characters for a cucumber test in the console.

That was hard to find. Wish I had seen Glenn Rempe's answer a while back.

查看更多
忆尘夕之涩
4楼-- · 2019-01-02 15:13

A slight tweak on Paul Raupach's answer which, when run from a directory, finds all *.yml files recursively in all sub-directories and tests the file. I ran it from my Rails root dir.

require 'yaml'

d = Dir["./**/*.yml"]
d.each do |file|
  begin
    puts "checking : #{file}"
    f =  YAML.load_file(file)
  rescue StandardError
    puts "failed to read #{file}: #{$!}"
  end
end
查看更多
栀子花@的思念
5楼-- · 2019-01-02 15:14

One of the possible causes is mapping values are not allowed in this context at line ...

Here is an incorrect YAML example (user: should not contain any value actually, because it contains children items some_key and some_other_key)

customer: Customer
user: User
  some_key: value
  some_other_key: value 2

It's not a trivial task to find such issue especially if you have a huge YAML file.

I've created a pretty simple regexp to detect such things. I checked it in RubyMine

^(\s+)['"\w]+:\s?['"\w]+.*\n\1\s\s

Be careful! It doesn't work correct with special chars like å ø æ etc.

Let me know in comments if it worked for you :)

查看更多
无色无味的生活
6楼-- · 2019-01-02 15:15

You have invalid YAML code somewhere. I mean invalid for Psych (the new ruby YAML parser).

If you cannot (or don't want to) fix your YAML code, try to load the old YAML parser (syck), adding this at the beginning of config/boot.rb

require 'yaml'
YAML::ENGINE.yamler = 'syck'

It's just a 'quick and dirty' fix, I know

查看更多
君临天下
7楼-- · 2019-01-02 15:16

For my case, it is not a Bundle issue: (Ruby 1.9 is assumed)

  • Ruby, by default, uses 'psych' (newer and maintained yaml library linking to the C library: libyaml) if libyaml is present
  • Otherwise, Ruby uses 'syck' (old and not maintained)
  • YAML::ENGINE.yamler= 'syck' will thus forces Ruby to use 'syck' on a machine where 'psych' is also installed

More info here: require "yaml" doesn't use psych as default

查看更多
登录 后发表回答