YAML::dump creates strings YAML::load cannot parse
This case seems to occur whenever
- the string begins with a newline, and
- the last line doesn't end in whitespace (except, perhaps, newlines), and
- the second line (after the initial whitespace) begins with more whitespace than some later line
Here are a couple example strings:
- "\n a\nb"
- "\n a\n b\nc"
- "\n a\n b"
YAML::load throws an ArgumentError with a syntax error on the line that begins with less leading
- YAML.load YAML.dump("\n a\nb") #=> ArgumentError: syntax error on line 3, col 0: `b'
- YAML.load YAML.dump("\n a\n b\nc") #=> ArgumentError: syntax error on line 4, col 0: `c'
- YAML.load YAML.dump("\n a\n b") #=> ArgumentError: syntax error on line 3, col 1: ` b'
This has been observed in ruby-1.8.6p0 and ruby-1.8.7p0.
Here is another observation of this same bug: http://blog.smartlogicsolutions.com/2008/09/04/ruby-patch-to-fix-broken-yamldump-for-multi-line-strings-stringto_yaml/
Updated by daz (Dave B) over 10 years ago
Good reports, thanks.
I've found a workaround but I'm not sure enough about the YAML spec to guarantee that it's the correct fix.
The patch is mild - it just forces one style over another in the same way as the existing code and allows the new tests to pass along with the rest.
Two patches to 1.9.2dev - these files don't change often ;)
* test/yaml/test_yaml.rb: add new tests and comment old Test::Unit::TestSuite code to enable them to run as $0
Updated by coatl (caleb clausen) almost 10 years ago
- File test_yaml2.patch test_yaml2.patch added
- File syck_scan_scalar2.patch syck_scan_scalar2.patch added
I have also been bitten by this same bug. In addition to the cases described above, this problem also occurs when the input contains just a single newline (in either unix or dos style). The patch supplied by Dave B fixes the case of a string containing just a single unix newline, but fails to fix a string with just a single dos newline (and probably the other cases initially reported, if the unix newlines are converted to dos newlines). I'm attaching slightly a slightly modified patches, which addresses the dos newline issue, and tests single dos and unix newlines.
This problem also occurs in ruby 1.9.
Updated by weppos (Simone Carletti) over 9 years ago
Here's an other test case for a similar issue.
c = File.read("yaml-error.txt")
'rgumentError: syntax error on line 34, col 0:
from /Users/weppos/.rvm/ruby-1.8.7-p248/lib/ruby/1.8/yaml.rb:133:in `load'
Updated by coatl (caleb clausen) over 9 years ago
Yusuke Endoh and I have verified that all of the test cases reported here are fixed when using psych. For proof of most, see [ruby-core:29323]. And I just now verified the last test case is now ok, submitted by Simone Carletti. (No console dump, sorry.)