rails error, couldn't parse YAML
Asked Answered
J

29

76

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

Jefferson answered 12/2, 2011 at 21:57 Comment(7)
What are the contents of the YAML file in question? It says there is a syntax error, so the problem probably lies in your YAML file.Dogmatist
I don't work with any YAML file, so I assumed that is someone else's problem.Jefferson
which command are you running? are you using RVM?Scheme
it happens after either commands rails console and rails server. yes, im using rvmJefferson
see this commit github.com/rails/rails/commit/dc94d81Weaken
You should mark #4981377 as accepted...Devilkin
Might also want to be sure you have all spaces for indenting, no tabsNahshunn
A
165

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

Assentation answered 24/2, 2011 at 15:59 Comment(17)
Great, it works! Please write an explanation in here why these lines should be in project ( e.g. the file 'application.rb' already has the 'require 'yaml'', why it's not enough)Jefferson
Bundler 1.0.10 loads Psych as a YAML engine by default. Psych does not work fine with rails (I'm using rails 3.0.4, and v3.0.3 had same issue). See here. Solution: set another YAML engine explicitelyAssentation
The error is in the file RedCloth-4.2.2/lib/redcloth/formatters/latex_entities.yml. My understanding is that this has been fixed in more recent versions of RedCloth, however version 4.2.2 is still the current stable version.Generatrix
Adding this to boot.rb plus removing RedCloth from our Gemfile fixed the issue. Thanks!Shiverick
This fixed my problem with my YAML file not being properly loading anchor references.Firenze
I am using Rails 3.1 with ruby 1.9.2 on Ubuntu 11 and had this issue and the fix worked for me.Herbivore
This is just a quick fix and not a long term solution. Probably you have syntax errors in some of your yaml files that you should fix instead.Powered
yup - as vicvega said - 'fix the yaml'... but if you don't want to :)... I ran into this with resque_schedule issue where cron: * * * * * and needed to put quotes around cron: "* * * * *"Harwin
I had a similar problem and so ran my YAML through yamllint.com to see if there were problems. Although it reported that the YAML was valid, it reformatted the file. I replaced my old non-working YAML with this reformatted version and it worked. Problem solved.Umpteen
For all the naysayers, yes, fix your YAML, but this tip is invaluable, because if you have bad YAML serialized with syck, and you want to convert it to valid YAML serialized with psych, you need the ability to use both parsers during the same ruby execution.Aweigh
For everyone saying, "Fix your YAML" ... note that this error occurs when starting Rails, and it's not clear at all which YAML file is causing the problem.Hammerfest
One questions, which version of Ruby are you executing before and after solve this issue?Vanillic
@NeoRiddle: I was using ruby-1.9.2-p290Assentation
@CharlesRoper YamlLint made it for me :)Clarkin
Apparently version 1.3 of psych understands default values. Maybe :-) Solution: use "gem 'psych', '>=1.3.2'" in Gemfile.Macruran
a downside of yamllint.com is that content sequence isn't preserved for hash items. Understandable, but annoying.Ensheathe
Our delayed_job queue was not properly formatted and Heroku worker dynos were crashing. After hours of searching (and frantic customers), this answer saved me.Barstow
A
49

My regular Rails 3 App also had this problem, because I was using a localized yaml File for Date/Times.

As you can see in this commit https://github.com/rails/rails/commit/dc94d81 this can be easy "fixed" by placing the array in seperate lines.

         -    order: [ :year, :month, :day ]
    18  +    order:
    19  +      - :year
    20  +      - :month
    21  +      - :day
Azedarach answered 16/3, 2011 at 9:11 Comment(2)
Thanks, was looking for exactly that problem and it solved it, although this is different from the problem of the question above :)Ard
I had trouble with the rubber-postgresql.yml, went through with the editor fixing the references like this, it worked. The rest of the rubber configuration seemed fine.Multivalent
M
20

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
Melisa answered 24/3, 2012 at 0:59 Comment(0)
M
18

The root cause was described on many places and I will summarize it again.

There are two default yaml parser Psych is the new one, the one you should be using. Syck is the old one, its not maintained and dying, its currently used as fall back for when there is no libyaml present (non-linux systems usually).

The important thing is you have some invalid yaml somewhere. It is most probably in your translation files (I had unquoted strings strating with %). Just try loading all your yml files with YAML.load_file on the production box and you will see which one is the broken one.

Manna answered 2/6, 2011 at 17:59 Comment(3)
Downvoting this because you're wrong. psych does not support default blocks, which is technically closer to the yaml standard, but breaks existing and expected usage by (among other things) rails. IMO ruby should use a parser that is both maintained and supports existing usage, even if that usage is further from the spec. If only there were such a thing. Until there is, they should use syck.Arsenault
And I also agree with Sarah. This whole move to pysch has been nothing but a headache for no obvious value. My "invalid yaml" wasn't invalid, it was just using the "order: [ :year, :month, :day ]" syntax, which has always been fine as far as I can tell (or, care)Multivalent
Apparently version 1.3 of psych understands default values. Maybe :-) Solution: use "gem 'psych', '>=1.3.2'" in Gemfile.Macruran
K
13

I had this problem because i had used a tab instead of spaces

Kevel answered 12/11, 2011 at 17:43 Comment(2)
yeah it feels like we should be over that!Kevel
blind after staring at a yml file searching for a missing colon.Tiemannite
G
13

It's best to fix your YAML files

Here is how using irb so you don't need the rails console which is probably not working:

require 'yaml'
YAML::ENGINE.yamler = 'psych'
YAML.load_file('config/locales/xxx.en.yml')

you'll get a nice output telling you where the issue is:

Psych::SyntaxError: couldn't parse YAML at line 25 column 17
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse_stream'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:119:in `parse'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:106:in `load'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:205:in `load_file'
    from (irb):10
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
Gaiser answered 26/1, 2012 at 18:9 Comment(0)
C
7

Absolutely fix your yaml code don't just 'mask' the real problem by forcing YAMl to use 'syck'. I had this same problem and found malformed yml statements in my localization files. If you just force using the older parser then you will not get the benefits of all the work on the new parser elsewhere in your project.

Credit answered 29/5, 2011 at 6:28 Comment(1)
there are no benefits that I can detect with the new parser, only cryptic errors messages (I'm currently looking at "Error Unknown on line 1, col 16" of a file that has no col 16 in line 1) in apparently correct YAML files that parsed with the previous version of psych. Endless bug fixes and regressions are the order of the day. So don't get high and mighty, this is a endless headache.Multivalent
R
7

The problem with the original question was in RedCloth. I was experiencing the same issue and simply updating to the most recent version of the RedCloth gem (Currently 4.2.7) fixed the issue.

The above advice from Honza and FlyboyArt is sound and you should fix any custom YAML you have, but with RedCloth being as popular as it is, most users finding this question who also use RedCloth should make sure their GemFile has this line added:

gem 'RedCloth', ">= 4.2.7"
Rikkiriksdag answered 3/6, 2011 at 14:40 Comment(0)
H
4

For others reading this, I got this error after making a typo in my database configuration - /config/database.yml

Harman answered 28/1, 2012 at 23:45 Comment(1)
Typos include not adding a newline to the end of the file, apparently...!Katalin
A
3

It's a bundler 1.0.10 issue: details here

Try just to downdate bundler

Assentation answered 14/2, 2011 at 14:58 Comment(3)
im using 1.0.10, you mean wait for new version?Jefferson
I solved coming back to bundler 1.0.7. I hope this will be fixed in next versionAssentation
I'm using Bundler version 1.1.3 and it still does it.Philadelphia
S
2

What fixed it in my cause was indeed malformed YAML translation file in:

config/locales/bg.yml

Corrected the YAML mistake and it was all fine. :-)

Subroutine answered 23/2, 2011 at 14:44 Comment(1)
didn't help, maybe my error in another YAML file, how to find out where is mistaken YAML file?Jefferson
F
2

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

Flatiron answered 3/5, 2011 at 7:46 Comment(0)
A
2

For those pursuing this issue I have just found that my database.yml was triggering this error because it had no space between the password: keyword and the password. An almost invisible error, and with a database.yml that had worked without errors on an earlier version of rails.

Aculeus answered 30/11, 2011 at 7:8 Comment(0)
B
2

I got this error from trying to connect to a remote db with the password 'p@ssword' and figured out that psych doesn't like the '@' symbol. Changed the DB password and problem solved.

Babette answered 9/3, 2012 at 6:54 Comment(0)
G
2

I had this problem. My problem was I had an extra tab in my database.yml file.

Generatrix answered 14/5, 2012 at 16:43 Comment(1)
I forgot to put a tab between the Password: and 'my password'Cissiee
C
2

I came across this as I was using the r18n library in a Sinatra app I'm building, and in my translation file I had the following:

day: !!pl
  0: 0 days
  1: 1 day
  n: %1 days

which used to work just fine in an older project under Ruby 1.8.7, but which was failing under Ruby 1.9.3.

An answer by @SB gave me the clue I needed to work out my problem. The newer YAML was balking at the %1. Some quick digging and an experiment with irb and I now know that the newer version of the YAML parser requires you to put quotes around strings that start with %1, so I just changed my translation to be

day: !!pl
  0: 0 days
  1: 1 day
  n: "%1 days"

and voila - nasty error message vanished.

Celestyn answered 19/9, 2012 at 0:40 Comment(0)
A
2

I manage to fix this problem by installing gem psych inside group :development and :test.

gem 'psych'
Annis answered 26/6, 2013 at 7:50 Comment(0)
S
1

I had the same issue with ruby 1.9.2-p180 , going to 1.9.2-p290 solved this for me

Suction answered 21/9, 2011 at 14:53 Comment(0)
N
1

Though the answer given by @Vicvega may or may not work (Didn't test it) it goes against Rails and Ruby common principle "Convention over configuration" and should be treated with care (and even more in collaborative work),,, even though the "configuration" in this case is not great

so my vote goes (If i could vote) for those who proposed to eliminate the syntax errors in the YAML files.

now... to solve the error, for me it was kind of a newby error, I didn't have the locale file which I had defined to be the default in Config/application.rb in my Config/locales directory

happy coding

Nightwalker answered 13/10, 2011 at 17:3 Comment(0)
E
1

Need to check .yml files for error, I have found problem in my database.yml

Externalism answered 8/11, 2011 at 5:42 Comment(0)
B
1

I had a similar issue with a malformed YAML translation file. It used a variable before defining it. The following was wrong:

...
messages:
  ...
  <<: *errors_messages
...
messages: &errors_messages
...

It had to be changed to:

...
messages: &errors_messages
...
messages:
  ...
  <<: *errors_messages
...

Then it started working again.

Barmecide answered 8/3, 2012 at 11:5 Comment(0)
C
1

If you're like me and facing a project (inherited) with hundreds of fixtures a few lines of Ruby can save you hours:

require 'yaml'

d = Dir.new('test/fixtures/')
d.each do |file|
  begin
     f =  YAML.load_file('test/fixtures/' + file)
  rescue StandardError
     puts "failed to read test/fixtures/#{file}: #{$!}"
   end
 end

Just put it in your Rails root and run it, trash it when you're done.

Chacon answered 22/3, 2012 at 21:16 Comment(0)
R
1

In my case there were 2 issues.

  1. As mentioned by @stwienert, the array representation was an issue.
  2. One more thing was, if a String started with a %{var} I received a Parse exception. I had to change the strings accordingly to avoid beginning with %{var}

For example if the string was

%{user_name} welcome to %{application_name} -- This threw an error

To fix it I had to change it to

Hi, %{user_name} welcome to %{application_name}

Hope this helps someone.

Regards,

Shardul.

Rules answered 14/4, 2012 at 15:57 Comment(1)
You could have simply changed the YAML file to put quotes around your string, so hello: %{user_name} welcome to %{application_name} becomes hello: "%{user_name} welcome to %{application_name}" rather than having to change your sentence, which is the cart leading the horse.Celestyn
K
1

Well, just in case this helps...
What I did:
- select all and copy from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/es.yml into a new es.yml with notepad++
- tried to watch this new file with netBeans IDE text editor, I got a warning about safe load with utf8 (cannot recall the exact text). Hence did not open it with this text editor.
- switched the local thru configuration/application.rb i18n
- when I loaded a irb page I got "couldn't parse YAML at line 0 column 0" referring to Psych.
- Went to IRB and loaded the file with syck it was ok; switched to psych and got same error.

How I solved it:
- went back to copy the contents from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/es.yml but this time I pasted it in a newly created file with netBeans editor.
- restarted webRick.
- problem solved.
Best Regards,
Victor

Karl answered 29/7, 2012 at 21:37 Comment(0)
N
1

Remove unused databases from database.rb. If you use MySQL and there is no PostgreSQL then delete PG database code from databases.yml.

Nedra answered 28/9, 2012 at 9:56 Comment(0)
P
1

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
Prose answered 16/11, 2012 at 8:26 Comment(1)
This gives me the following error: gem "psych" cannot be uninstalled because it is a default gemSimpatico
D
1

I had a really, really strange problem because I had spaces after. E.g.:

title: "NASA"

Did not work, but

title:"NASA"

Did.

Darby answered 26/2, 2013 at 3:45 Comment(0)
K
0

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.

Kumiss answered 5/11, 2013 at 20:18 Comment(0)
G
0

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 :)

Gushy answered 20/2, 2014 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.