'Recent' updates
[pwan.org.git] / content / hints / count-your-yaml-dashes.rst
1 Count your YAML dashes
2 ######################
3
4 :date: 2014-08-31
5 :tags: hints,ruby,yaml
6 :category: hints
7 :author: Jude N
8
9 I spent way too long the other day thrying to figure out why some Hiera variable wasn't available in some puppet manifest.
10
11 It turns out there was a typo in the YAML file where the first line of the file only had two dashes instead of three.
12
13 In this cases, the Ruby 1.8.7 yaml parser corrupts the first entry in the yaml file, adding the two dashes to the beginning of the key, instead of throwing a parser error.
14
15 .. code-block:: irb
16
17 irb(main):001:0> require 'yaml'
18 => true
19 irb(main):002:0> a=YAML.load_file("only-two-dashes.yaml")
20 => {"-- first"=>1, "second"=>2}
21 irb(main):003:0> b=YAML.load_file("correctly-including-three-dashes.yaml")
22 => {"first"=>2, "first"=>1}
23
24 I couldn't find an existing bug for this, but I didn't look to harrd since this has been fixed in Ruby 1.9
25
26 .. code-block:: irb
27
28 irb(main):001:0> require 'yaml'
29 => true
30 irb(main):002:0> a=YAML.load_file("only-two-dashes.yaml")
31 Psych::SyntaxError: (t1.yaml): couldn't parse YAML at line 1 column 1
32 from /usr/lib/ruby/1.9.1/psych.rb:154:in `parse'
33 from /usr/lib/ruby/1.9.1/psych.rb:154:in `parse_stream'
34 from /usr/lib/ruby/1.9.1/psych.rb:125:in `parse'
35 from /usr/lib/ruby/1.9.1/psych.rb:112:in `load'
36 from /usr/lib/ruby/1.9.1/psych.rb:229:in `load_file'
37 from (irb):2
38 from /usr/bin/irb1.9.1:12:in `<main>'.
39
40 So if you're using Ruby 1.8.7, and it looks like the first item in your YAML file is being dropped for some
41 reason, check the first line of the file has 3 dashes.
42
43
44
45